几种通过JDBC操作数据库的方法,以及返回数据的处理

1.SQL TO String :只返回一个查询结果

  例如查询某条记录的总数

           rs = stmt.executeQuery(replacedCommand);
             if (rs != null && rs.next()) // rs only contains one row and one column
             {
                    String tempStr = rs.getString(1);
                    if (tempStr == null)
                    {
                        result = "";
                    } else
                    {
                        result = rs.getString(1);
                    }

    }

2.SQL TO Object :返回的是类似某个对象的记录,即很多条字段

  例如,查询某个用户的所有订单,并反射到对象中去

  className 为你要映射的对象的名字

           Document xmlContentDoc = null;
           OracleXMLQuery xmlQuery;
             rs = stmt.executeQuery(replacedCommand);

             xmlQuery = new OracleXMLQuery(conn, rs);
             xmlQuery.setRowTag(className);
              xmlContentDoc = xmlQuery.getXMLDOM();
            checkDocumentErrors(xmlContentDoc, xmlQuery.ERROR_TAG);
           
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer=null;
            DOMSource source=null;
            StreamResult result=null;
            transformer = tFactory.newTransformer();
              
            //get all tags with class name equal to "className"
            NodeList rows=xmlContentDoc.getElementsByTagName(className);
        
            //loop on the row and make objects that map to the selected row elements
            for(int i=0;i<rows.getLength();i++)
            {
                Element row = (Element)rows.item(i);
                row.removeAttribute("num");             
                StringWriter sw=new StringWriter();
                source = new DOMSource(row);
                result = new StreamResult(sw);
                transformer.transform(source, result);
                String xmlString=sw.toString();
                sw.close();               
                Object inputObj=Class.forName(className).newInstance();
                Converter converter=Converter.getInstance();       
                Object OutputObj=converter.convertToObject(xmlString,inputObj);
                outputResult.add(OutputObj);
            }

3.SQL TO Map:这种查询的是2个字段,其中一个作为key,另一个字段作为value

              rs = stmt.executeQuery(replacedCommand);
              if(rs != null)
              {                                      
                    ResultSetMetaData metadata;
                    int coloumnNo = 0;                   
                    metadata = rs.getMetaData();
                    coloumnNo = metadata.getColumnCount();
                    Object tempKey,tempValue;
                    while(rs.next())
                    {
                        //if the number of coloumns =1 make the in the hashtable the key and value are the same                     
                        if(coloumnNo == 1)
                        {          
                            tempKey=rs.getObject(1);                           
                            if(tempKey==null)   tempKey="";
                            tempValue=tempKey;
                            tempKey= (tempKey instanceof CLOB) ? rs.getString(1) : tempKey.toString().trim();
                            tempValue=tempKey;
                            result.put(tempKey,tempValue);                           
                        }else
                        {                           
                            tempKey=rs.getObject(1);
                            tempValue=rs.getObject(2);
                            if(tempKey==null)   tempKey="";
                            if(tempValue==null)   tempValue="";
                            tempKey=(tempKey instanceof CLOB) ? rs.getString(1) : tempKey.toString().trim();
                            tempValue=(tempValue instanceof CLOB) ? rs.getString(2) : tempValue.toString().trim();
                            result.put(tempKey,tempValue);
                        }//else                   
                    }//while              
              }

4.  明天待续!       

  

 

 

转载于:https://www.cnblogs.com/wangshixin/p/3712526.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值