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. apt-get 与 yum 的区别

    一般来说著名的 linux 系统基本上分两大类: RedHat系列:Redhat.CentOS.Fedora等 Debian系列:Debian.Ubuntu等 RedHat 系列 1 常见的安装包格式 ...

  2. Netscape HTTP Cooke File Parser In PHP

    http://www.hashbangcode.com/blog/netscape-http-cooke-file-parser-php I recently needed to create a f ...

  3. PBOC~PPT-补充A(转)

    qPBOC简介PBOC 3.0非接交易包括:非接PBOC和qPBOC.非接PBOC流程与接触式无异,仅命令交互方式改变,故不再赘述. qPBOC - 快速借记/贷记,交易特点:目录选择PPSE使用“2 ...

  4. C# 实现 Excel文件的数据导入

    前台 <asp:FileUpload ID="fuFile" runat="server" /> 后台 public string GetExcel ...

  5. git pull 然后 ahead of origin/master * commit 消失

    本来显示 your branch is ahead origin/master * commit后来也许在master merge 这个分支后, 然后git pull, 就显示Your branch ...

  6. java对国际化的支持

    国际化的英文为Internationalization,这个也太长了,所以它又称为I18n(英文单词 internationalization的首末字符i和n,18为中间的字符数). 除了i18n还有 ...

  7. Configure Apache Virtual Hosts - CentOS 7

    Difficulty: 2Time: 15 minutes Want to host websites on your server? Using Apache? Great. This articl ...

  8. TensorFlow之Varibale 使用方法

    ------------------------------------------- 转载请注明: 来自博客园 xiuyuxuanchen 地址:http://www.cnblogs.com/gre ...

  9. mysql数据库的导入导出

    当我们在操作数据库的时候,难免会遇到数据导入导出的一些操作,今天突然学到了这个知识点,特意来给大家分享. 我用的是data的这条数据 1.使用数据 mysql> use data; Databa ...

  10. Codeforces Round #375 (Div. 2) F. st-Spanning Tree

    传送门 分析:构造题.可以这么想:先把s,t两个点去掉,把剩下的点先并查集合并.这样会出现个集合:, , 个剩余集合.那么个集合中先把只能与或中一个相连的连起来,如果这样已经超出了要求,那么就不能构造 ...