I have downloaded the latest version of xerces (xml parser).
I am trying out an example from Beginning Java Databases book.
I have set the classpath as c:\Xerces-J-bin.1.4.4\xerces-1_4_4\xerces.jar;
The file below compiles but I am not able to run it. It says that No Class Defination is found. What am I missing in the classpath?
import java.sql.*; /* JDBC Classes*/
import java.io.*; /* Java IO */
import org.w3c.dom.*; /* W3C Interfaces */
import org.apache.xerces.dom.*; /* Xerces DOM Classes */
import org.apache.xml.serialize.*; /* Xerces Serializer */
public class databasexml {
public static final String JDBCURL = "oracle.jdbc.driver.OracleDriver";
public static final String JDBCDRIVER ="jdbc:oracle:thin:@wcsuds1.ctstateu.edu:1521:wprac";
public static final String SQL = "select customerid, customerfirstname, customerlastname from customers";
public static String OUTPUTFILE = "c:
customers.xml";
public static void main(String[] args) {
try{
/** Step 1 : Making a JDBC Connection with database" **/
Class.forName(JDBCDRIVER).newInstance();
Connection conn = DriverManager.getConnection(JDBCURL);
/** Step 2 : Retrieve the customer data from database **/
Statement statement = conn.createStatement();
ResultSet customerRS = statement.executeQuery(SQL);
/** Step 3 : Build customer XML DOM **/
Document xmlDoc = buildCustomerXML(customerRS);
/** Step 4 : Write output to a file **/
File outputFile = new File(OUTPUTFILE);
printDOM(xmlDoc, outputFile);
conn.close(); /*Connection close*/
}
catch(Exception e)
{
System.out.println("Really poor exception handling " +e.toString());
}
}//Main
/*Build XML DOcument from database. The XML object is returned to main method where it is written to flat file.*/
private static Document buildCustomerXML(ResultSet _customerRS) throws Exception
{
Document xmlDoc = new DocumentImpl();
/* Creating the root element */
Element rootElement = xmlDoc.createElement("CUSTOMERS");
xmlDoc.appendChild(rootElement);
while(_customerRS.next())
{
Element customer = xmlDoc.createElement("CUSTOMER");
/* Build the CustomerId as a Attribute*/
customer.setAttribute("customerid", _customerRS.getString("customerid"));
/* Creating elements within customer DOM*/
Element firstName = xmlDoc.createElement("FIRSTNAME");
Element lastName = xmlDoc.createElement("LASTNAME");
/* Populating Customer DOM with Data*/
firstName.appendChild(xmlDoc.createTextNode(_customerRS.getString("customerfirstname")));
lastName.appendChild(xmlDoc.createTextNode(_customerRS.getString("customerlastname")));
/* Adding the firstname and lastname elements to the Customer Element*/
customer.appendChild(firstName);
customer.appendChild(lastName);
/* Appending Customer to the Root Class*/
rootElement.appendChild(customer);
}
return xmlDoc;
}
/* printDOM will write the contents of xml document passed onto it out to a file*/
private static void printDOM(Document _xmlDoc, File _outputFile) throws Exception
{
OutputFormat outputFormat = new OutputFormat("XML","UTF-8",true);
FileWriter fileWriter = new FileWriter(_outputFile);
XMLSerializer xmlSerializer = new XMLSerializer(fileWriter, outputFormat);
xmlSerializer.asDOMSerializer();
xmlSerializer.serialize(_xmlDoc.getDocumentElement());
}
}
-
Generate XML file from Database using JDBC (2 messages)
- Posted by: priyanka shah
- Posted on: August 08 2002 17:40 EDT
Threaded Messages (2)
- Generate XML file from Database using JDBC by raghunandana pv on August 09 2002 04:11 EDT
- Generate XML file from Database using JDBC by Bhagvan K on August 09 2002 04:11 EDT
-
Generate XML file from Database using JDBC[ Go to top ]
- Posted by: raghunandana pv
- Posted on: August 09 2002 04:11 EDT
- in response to priyanka shah
add the directory in which you have your class file to the classpath. I think it is not able to find ur databasexml.class file -
Generate XML file from Database using JDBC[ Go to top ]
- Posted by: Bhagvan K
- Posted on: August 09 2002 04:11 EDT
- in response to priyanka shah
looks like you missed out the oracle driver zip file : classes12.zip in the classpath.