Generate XML file from Database using JDBC


XML & Web services: Generate XML file from Database using JDBC

  1. Generate XML file from Database using JDBC (2 messages)

    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 */
    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 ="";
      public static final String SQL = "select customerid, customerfirstname, customerlastname from customers";
      public static String OUTPUTFILE = "c:

      public static void main(String[] args) {


        /** Step 1 : Making a JDBC Connection with database" **/
        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());

      /*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");

        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*/

        /* Adding the firstname and lastname elements to the Customer Element*/

        /* Appending Customer to the Root Class*/
      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);




  2. 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

  3. looks like you missed out the oracle driver zip file : in the classpath.