Write a program to create a XML document from a telephone directory database. Dec08_4a

import java.sql.*;
public class Dec08_4a
{
  public static void main(String ar[])throws Exception
  {
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   Connection con = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=database.mdb");
    //Connection con = DriverManager.getConnection("jdbc:odbc:mydsn");
   Statement st = con.createStatement();
   ResultSet rs = st.executeQuery("select * from phoneDirectory");
   System.out.println("<phone-directory>");
   while (rs.next())
   {
    System.out.println("    <customer>");
    String s1 = rs.getString(1);
    System.out.println("          <customer-name>"+s1+"</customer-name>");
    String s2 = rs.getString(2);
    System.out.println("          <address>"+s2+"</address>");
    String s3 = rs.getString(3);
    System.out.println("          <phone-number>"+s3+"</phone-number>");
    String s4 = rs.getString(4);
    System.out.println("          <bill-summary>"+s4+"</bill-summary>");
    System.out.println("    </customer>");
   }
   System.out.println("</phone-directory>");
  }
}



OUTPUT

<phone-directory>
    <customer>
          <customer-name>abc</customer-name>
          <address>sdfsad</address>
          <phone-number>234243</phone-number>
          <bill-summary>343</bill-summary>
    </customer>
    <customer>
          <customer-name>sdf</customer-name>
          <address>sdfsdf</address>
          <phone-number>2345234</phone-number>
          <bill-summary>345</bill-summary>
    </customer>
    <customer>
          <customer-name>hhhhhhhh</customer-name>
          <address>dddd</address>
          <phone-number>44444</phone-number>
          <bill-summary>333</bill-summary>
    </customer>
    <customer>
          <customer-name>sssss</customer-name>
          <address>3333fff</address>
          <phone-number>3333</phone-number>
          <bill-summary>444</bill-summary>
    </customer>
</phone-directory>

No comments:

Popular Posts