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

  

几种通过JDBC操作数据库的方法,以及返回数据的处理的更多相关文章

  1. 封装JDBC操作数据库的方法

    自己动手封装java操作数据库的方法: 一:目录结构 二:所需依赖的第三方jar包 这里只需引入mysql-connector-java-5.1.8-bin.jar,mysql数据库驱动jar包 三: ...

  2. JDBC操作数据库的三种方式比较

    JDBC(java Database Connectivity)java数据库连接,是一种用于执行上sql语句的javaAPI,可以为多种关系型数据库提供统一访问接口.我们项目中经常用到的MySQL. ...

  3. JDBC操作数据库的学习(1)

    单单对数据库的操作,比如说MySQL,我们可以在命令行窗口中执行,但是一般是应用程序要操作数据库,因此我们应该在程序中的代码上体现对数据库的操作,那么使用程序应用如何操作数据库呢?那就要使用到数据库的 ...

  4. Java笔记(第七篇 JDBC操作数据库)

    JDBC是连接数据库和java程序的桥梁,通过JDBC API可以方便地实现对各种主流数据库的操作.学习java语言,必须学习JDBC技术,因为JDBC技术实在java语言中被广泛使用的一种操作数据库 ...

  5. JDBC操作数据库的学习(2)

    在上一篇博客<JDBC操作数据库的学习(1)>中通过对例1,我们已经学习了一个Java应用如何在程序中通过JDBC操作数据库的步骤流程,当然我们也说过这样的例子是无法在实际开发中使用的,本 ...

  6. Spark Streaming通过JDBC操作数据库

    本文记录了学习使用Spark Streaming通过JDBC操作数据库的过程,源数据从Kafka中读取. Kafka从0.10版本提供了一种新的消费者API,和0.8不同,因此Spark Stream ...

  7. 用ADO操作数据库的方法步骤(ZT)

    http://www.cppblog.com/changshoumeng/articles/113437.html 学习ADO时总结的一些经验 用ADO操作数据库的方法步骤 ADO接口简介 ADO库包 ...

  8. 摘:用ADO操作数据库的方法步骤

    用ADO操作数据库的方法步骤 ADO接口简介 ADO库包含三个基本接口:_ConnectionPtr接口._CommandPtr接口和_RecordsetPtr接口. _ConnectionPtr接口 ...

  9. 用ADO操作数据库的方法步骤

    用ADO操作数据库的方法步骤 学习ADO时总结的一些经验 - 技术成就梦想 - 51CTO技术博客 http://freetoskey.blog.51cto.com/1355382/989218   ...

随机推荐

  1. js平滑滚动到顶部,底部,指定地方

    [原文链接] 采用锚点进行页面中的跳转的确很方便,但是要想增加网页的效果,可以使用jquery中的animate,实现滚动的一个动作,慢慢的滚动到你想跳转到的位置,从而看起来会非常高大上. [示例演示 ...

  2. commons-io ProxyInputStream,ProxyOutputStream,ProxyReader,ProxyWriter

    1.ProxyInputStream: A Proxy stream which acts as expected, that is it passes the method calls on to ...

  3. Android文件存储

    文件存储是Android中最基本的一种数据存储方式,它不读存储的内容进行任何的格式化处理,所有数据原封不动的保存在文件之中.如果想用文件存储的方式保存一些较为复杂的数据,就需要定义一套自己的格式规范, ...

  4. phantomjs+selenium实现爬取动态网址

    之前使用 selenium + firefox驱动浏览器来实现爬取动态网址,但是firefox经常更新,更新后时常会导致webdriver启动不来,所以改用phantomjs+selenium来改善一 ...

  5. jquery获得option的值和对option进行操作

    Query获取Select元素,并选择的Text和Value: $("#select_id").change(function(){//code...}); //为Select添加 ...

  6. restore database

    RESTORE DATABASE CTSDW FROM DISK = '\\detego-ctsetl\Backup\CTSDW\CTSDW_backup_20160722110003_Full.ba ...

  7. 利用Spring中同名Bean相互覆盖的特性,定制平台的类内容。

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  8. pc端页面在移动端显示问题

    1.pc端页面在移动端显示,默认视口宽度是980px(也就是body宽度是980px),可通过meta标签设置为需要的尺寸,比如页面中元素最大宽度是1220px,则如下所示 <meta name ...

  9. Starting MySQL... ERROR! The server quit without updating PID file 解决办法

    来源:http://blog.rekfan.com/articles/186.html 我使用了第4条解决了问题 1.可能是/usr/local/mysql/data/rekfan.pid文件没有写的 ...

  10. PC小技巧

    一.如何在office 2010中安装 Microsoft Office Document Imaging 我用的是office 2010版本,如下操作可以把照片转换成文本:第一步:使用Microso ...