Java Reference
In-Depth Information
ResultSetMetaData md = rs.getMetaData();
int nColumns = md.getColumnCount();
Element root = document.getDocumentElement();
while(rs.next()){
Element record=(Element)document.createElement("CUSTOMER");
root.appendChild (record);
for(int i=1;i<=nColumns;i++){
String fName = md.getColumnLabel(i);
String data = rs.getString(i);
if(fName.equals("CUSTOMER_NUMBER")){
record.setAttribute("CUSTOMER_NUMBER",String.valueOf(data));
}else{
Element fld = (Element)document.createElement(fName);
record.appendChild(fld);
fld.appendChild(document.createTextNode(data));
}
}
}
con.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}
The SQLQueryBeanTest code shown in Listing 17-8 is nothing more than a modified version of Listing
17-5 , with the SQLQueryBean plugged in place of the SystemTimeBean . This also serves to illustrate
how simple it is to use Xbeans to process XML data.
Listing 17-8: Using the SQLQueryBean
package JavaDatabaseBible.ch17.Xbeans;
import java.io.*;
import java.beans.Beans;
import JavaDatabaseBible.ch17.Xbeans.*;
public class SQLQueryBeanTest {
static public void main(String args[]) {
String databaseName = "SQLServerContacts";
String tableName = "CUSTOMERS";
Search WWH ::




Custom Search