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.  明天待续!

  

最新文章

  1. Bookshop(一)数据库连接
  2. LINQ to SQL语句(16)之对象标识
  3. Leetcode H-index
  4. Linux Overflow Vulnerability General Hardened Defense Technology、Grsecurity/PaX
  5. 在IE6、IE7中实现块元素的inline-block效果
  6. CentOS7搭建NAS,包括NFS、ISCSI
  7. 整理的sql sever一些数据库查询面试题
  8. POJ 1556 - The Doors 线段相交不含端点
  9. ASP.NET属性之AssociatedControlID
  10. 服务确定(服务类收货ML81N)
  11. nodeJS中读写文件方法的区别
  12. Python进阶_类与实例
  13. 5分钟解决google play上架App设置隐私政策声明问题
  14. ibatisNet MERGE INTO ORA-00911: 无效字符
  15. 求100之内的素质并输出(最优算法)-PHP面试题
  16. CustomScrollView + slivers + SliverAppBar
  17. Pandas透视表(pivot_table)详解
  18. Runtime 解读
  19. 关于虚拟机下centOS版linux系统ifconfig只显示inet6ip,不显示inet4ip的问题
  20. 关于UNITY学习,给新生建议

热门文章

  1. 【IOS 开发】Object - C 入门 之 数据类型详解
  2. Memcached的使用
  3. spring boot给http添加正向代理
  4. 使用Let`s encrypt 免费的https 证书
  5. el: 在jsp页面内使用函数判断子字符串
  6. ws_ webpack+reactjs+redux+nodejs认识
  7. ios基础操作
  8. webservice 实现json模式
  9. 深入springMVC------文件上传源码解析(上篇)
  10. MVC5+EF6 入门完整教程九