package oracle.otnsamples.jdbc;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.sql.SQLException;
import oracle.jdbc.rowset.OracleWebRowSet;
public class GetWebRowSet {
private final String sqlQuery =
"SELECT product_id, product_name, catalog_url, "
+"product_status, list_price, min_price "
+"FROM product_information WHERE "
+" product_id BETWEEN 3210 AND 3247 AND"
+" list_price IS NOT NULL AND min_price IS NOT NULL AND"
+" catalog_url IS NOT NULL AND product_status"
+" IS NOT NULL ORDER BY product_id ";
public GetWebRowSet() {
System.setProperty("org.xml.sax.driver", "oracle.xml.parser.v2.SAXParser");
}
public String createXMLProductInfo() throws SQLException, Exception {
StringWriter writer = new StringWriter();
String xmlString = null;
OracleWebRowSet wset = new OracleWebRowSet();
try {
wset.setDataSourceName("jdbc/OracleCoreWRS");
wset.setCommand(sqlQuery);
wset.setUsername("oe");
wset.setPassword("oe");
wset.execute();
wset.writeXml(writer);
xmlString = writer.toString();
writer.close();
} catch (SQLException sqlex) {
throw new Exception("Problem while connecting and querying the " +
"database table: " + sqlex.toString());
} catch (IOException ioex) {
throw new Exception("Could not construct a FileWriter: "+
ioex.toString());
} finally {
try {
if (wset != null) wset.close();
} catch (Exception ex) {
throw new Exception("UNKNOWN EX in finally block of"+
" createXMLProductInfo() method of given status: "
+ex.toString());
}
}
return xmlString;
}
public void updateXMLProductInfo(int prodId, String catUrl,
String prodStatus, int prodListPr,
int prodMinPr, String xmlString)
throws SQLException, Exception {
OracleWebRowSet wsetRdr = new OracleWebRowSet();
StringReader strRdr = new StringReader(xmlString);
try {
wsetRdr.readXml(strRdr);
wsetRdr.setCommand(sqlQuery);
wsetRdr.setUsername("oe");
wsetRdr.setPassword("oe");
int id = 0;
wsetRdr.beforeFirst();
while (wsetRdr.next()) {
id = wsetRdr.getInt(1);
if (id == prodId) {
wsetRdr.updateString(3,catUrl); wsetRdr.updateString(4,prodStatus); wsetRdr.updateInt(5,prodListPr); wsetRdr.updateInt(6,prodMinPr);
wsetRdr.updateRow();
wsetRdr.acceptChanges();
}
}
} catch (SQLException sqlex) {
throw new Exception("Problem while connecting and querying the" +
" database table: " + sqlex.toString());
} catch (Exception ex) {
throw new Exception("Problem while connecting to the database in the "+
"method updateXMLProductInfo() of given status: "+
ex.toString());
} finally {
if (wsetRdr != null) wsetRdr.close();
}
}
public String getXMLProductInfo(int prodId, String xmlString)
throws SQLException, Exception {
OracleWebRowSet wsetRdr = new OracleWebRowSet();
String productString = null;
StringReader strRdr = new StringReader(xmlString);
try {
wsetRdr.readXml(strRdr);
int id = 0;
StringBuffer strBuf = new StringBuffer();
wsetRdr.beforeFirst();
while (wsetRdr.next()) {
id = wsetRdr.getInt(1);
if (id == prodId) {
strBuf.append(wsetRdr.getString(2)).append("##").
append(wsetRdr.getString(3)).append("##").
append(wsetRdr.getString(4)).append("##").
append(wsetRdr.getString(5)).append("##").
append(wsetRdr.getString(6));
productString = strBuf.toString();
}
}
} catch (SQLException sqlex) {
throw new Exception("Problem while connecting and querying the" +
" database table: " + sqlex.toString());
} catch (Exception ex) {
throw new Exception("Problem while connecting to the database: "+
ex.toString());
} finally {
if (wsetRdr != null) wsetRdr.close();
}
return productString;
}
}
|