Several methods of manipulating databases through JDBC, and processing of returned data

Source: Internet
Author: User

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!

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.