标签:class ext get int string com
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操作数据库的方法,以及返回数据的处理,布布扣,bubuko.com
标签:class ext get int string com
原文地址:http://www.cnblogs.com/wangshixin/p/3712526.html