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