几种通过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 ...
随机推荐
- c++ is_space函数
C库函数int isspace(int c)检查传递的字符是否是空白. 标准空白字符: ' ' (0x20) space (SPC) ' ' (0x09) horizontal tab (TAB) ' ...
- arm cache line,PLD指令
C中嵌入汇编PLD指令:asm("PLD [%0,#128]": :"r" (psrc) ); copy自官方文档: 4.2.7. PLD.PLDW 和 PLI ...
- 阿里im即时通讯 h5 demo
适合不想装后台环境的同学,用nodejs搭建服务器. 以下是官网提供的node 请求示例: 找到了一个ali-top-sdk 代替topClient 于是请求示例代码如下: TopClient = r ...
- django表单
一.主要内容 1.服务端获取HttpRequest信息 1)url相关信息 2)HttpRequest.META中包含的键值对 3)HttpRequest中用 ...
- Oracle转MySQL
1. to_date 直接去掉 例如 select log.id from CM_LOGINLOG log where log.orgid =? and log.isAuto =? and lo ...
- java/Android 接口调用的几种写法
虽然Handler用的地方比较普遍,但是接口也有他的独特之处,比较直观,然后降低了耦合性 如有一接口,需要将数据传给使用的activity中,接口如下 public interface PushVal ...
- angularjs自定义指令
my-directive为指令名称,thisdata为绑定的数据 <span ng-repeat="act in move.casts" style="positi ...
- acm系统开发笔记
时间: 2016/2/29 遇到的困难: 数据库配置的mysql和java(Date)不一致,出现下面错误 Date date = new Date(); SimpleDateFormat ...
- 声明变量,一定要用 var!
public static T TryGet<T>(Func<T> func, T ifError = default(T)) { try { return func(); } ...
- 《Linux内核设计与实现》课本第十八章自学笔记——20135203齐岳
<Linux内核设计与实现>课本第十八章自学笔记 By20135203齐岳 通过打印来调试 printk()是内核提供的格式化打印函数,除了和C库提供的printf()函数功能相同外还有一 ...