几种通过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. 明天待续!
几种通过JDBC操作数据库的方法,以及返回数据的处理的更多相关文章
- 封装JDBC操作数据库的方法
自己动手封装java操作数据库的方法: 一:目录结构 二:所需依赖的第三方jar包 这里只需引入mysql-connector-java-5.1.8-bin.jar,mysql数据库驱动jar包 三: ...
- JDBC操作数据库的三种方式比较
JDBC(java Database Connectivity)java数据库连接,是一种用于执行上sql语句的javaAPI,可以为多种关系型数据库提供统一访问接口.我们项目中经常用到的MySQL. ...
- JDBC操作数据库的学习(1)
单单对数据库的操作,比如说MySQL,我们可以在命令行窗口中执行,但是一般是应用程序要操作数据库,因此我们应该在程序中的代码上体现对数据库的操作,那么使用程序应用如何操作数据库呢?那就要使用到数据库的 ...
- Java笔记(第七篇 JDBC操作数据库)
JDBC是连接数据库和java程序的桥梁,通过JDBC API可以方便地实现对各种主流数据库的操作.学习java语言,必须学习JDBC技术,因为JDBC技术实在java语言中被广泛使用的一种操作数据库 ...
- JDBC操作数据库的学习(2)
在上一篇博客<JDBC操作数据库的学习(1)>中通过对例1,我们已经学习了一个Java应用如何在程序中通过JDBC操作数据库的步骤流程,当然我们也说过这样的例子是无法在实际开发中使用的,本 ...
- Spark Streaming通过JDBC操作数据库
本文记录了学习使用Spark Streaming通过JDBC操作数据库的过程,源数据从Kafka中读取. Kafka从0.10版本提供了一种新的消费者API,和0.8不同,因此Spark Stream ...
- 用ADO操作数据库的方法步骤(ZT)
http://www.cppblog.com/changshoumeng/articles/113437.html 学习ADO时总结的一些经验 用ADO操作数据库的方法步骤 ADO接口简介 ADO库包 ...
- 摘:用ADO操作数据库的方法步骤
用ADO操作数据库的方法步骤 ADO接口简介 ADO库包含三个基本接口:_ConnectionPtr接口._CommandPtr接口和_RecordsetPtr接口. _ConnectionPtr接口 ...
- 用ADO操作数据库的方法步骤
用ADO操作数据库的方法步骤 学习ADO时总结的一些经验 - 技术成就梦想 - 51CTO技术博客 http://freetoskey.blog.51cto.com/1355382/989218 ...
随机推荐
- python 3次登录
#!/usr/bin/env python #-*- encoding: utf- -*- import sys import os import getpass import platform # ...
- angular中ng-repeat ng-if 中的变量的值控制器中为什么取不到
这个问题的本质是:v-repeat会产生子scope,这时你在控制器里拿值,相当于父scope里面取子scope的值,因为Angular.js中作用域是向上查找的,所以取不到. 操作过程如下: 相关代 ...
- django一对多关系的小例题
urls.py from django.conf.urls import urlfrom django.contrib import adminfrom son1.views import * url ...
- Failed: error processing document #281: unexpected EOF,往MongoDB当中插入json文件时出现的错误。
往MongoDB当中插入json文件时提示的错误(我的操作系统是win10): 当时的执行命令是:mongoimport -d test -c restaurants d://primer-datas ...
- RedHat下Bugzilla的安装和配置
Bugzilla 是一个开源的缺陷跟踪系统(Bug-Tracking System). OS:RedHat Linux 软件类型:开源 架构:B/S server端模块开发语言:perl(c/c++) ...
- ActiveReports 11 新特性速递
又到了一年一度,翘首期盼的ActiveReports11 即将发布,ActiveReports 10 表控件横空出世,成为中国式复杂报表的救星后,ActiveReports11 又会有哪些令人惊奇的新 ...
- SPSS数据分析—典型相关分析
我们已经知道,两个随机变量间的相关关系可以用简单相关系数表示,一个随机变量和多个随机变量的相关关系可以用复相关系数表示,而如果需要研究多个随机变量和多个随机变量间的相关关系,则需要使用典型相关分析. ...
- 【Fiddler】改写返回数据功能
方法一:打断点 1.在手机上设置好代理后,随便进入一个APP:之后选择Rules->Automatic Breakpoints->After Responses,在返回值处打断点 2.重新 ...
- 以forin的方式遍历数组时进行删除操作的注意点
今天在修改某项需求的时候,需要在遍历的时候将匹配项移除掉,采用的时forin的方式遍历,然后运行的时候却crash掉了 for (NSString*str in self.btnArray) { if ...
- WebService的两种方式Soap和Rest比较
我的读后感:由于第一次接触WebService,对于很多概念不太理解,尤其是看到各个OpenAPI的不同提供方式时,更加疑惑.如google map api采用了AJAX方式,通过javascript ...