1.SQL to String: Returns only one query result
For example, the total number of queries for a record
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: Returns a record that resembles an object, that is, a number of fields
For example, querying all orders for a user and reflecting them to the object.
ClassName the name of the object you want to map
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 this 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: This query is 2 fields, one as key and the other as 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 is the 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. Be continued tomorrow!