What is the fault in this code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Joshepmichel
    New Member
    • Feb 2008
    • 7

    What is the fault in this code

    Code:
    package librarySystemV5;
    import java.sql.*;
    import java.util.*;
             
    public class DatabaseTableViewer {
       private static final String DB = "mytestdb",
                                   TABLE_NAME = "MEMBER",
                                   HOST = "jdbc:mysql://db_host:3306/",
                                   ACCOUNT = "dbadmin",
                                   PASSWORD = "dbadmin",
                                   DRIVER = "jdbc:mysql://localhost:3306/";
             
       public static void main (String[] args) {
          try {
    
             // authentication properties
             Properties props = new Properties();
             props.setProperty("user", ACCOUNT);
             props.setProperty("password", PASSWORD);
                   
             // load driver and prepare to access
    		 Class.forName(DRIVER).newInstance();
             Connection con = DriverManager.getConnection(
                HOST + DB, props);
             Statement stmt = con.createStatement();
             
             // execute select query
             String query = "SELECT * FROM " + TABLE_NAME + ";";
             ResultSet table = stmt.executeQuery(query);
                
             // determine properties of table
             ResultSetMetaData meta = table.getMetaData();
             String[] colNames = new String[meta.getColumnCount()];
             Vector[] cells = new Vector[colNames.length];
             for( int col = 0; col < colNames.length; col++) {
                colNames[col] = meta.getColumnName(col + 1);
                cells[col] = new Vector();
             } 
    
             // hold data from result set
             while(table.next()) {
                for(int col = 0; col < colNames.length; col++) {
                   Object cell = table.getObject(colNames[col]);
                   cells[col].add(cell);
                }
             }
             
             // print column headings
             for(int col = 0; col < colNames.length; col++)
                System.out.print(colNames[col].toUpperCase() + "\t");
             System.out.println();
    
             // print data row-wise
             while(!cells[0].isEmpty()) {
                for(int col = 0; col < colNames.length; col++) 
                   System.out.print(cells[col].remove(0).toString() 
                                       + "\t");
                System.out.println();
             }
          }
    
          // exit more gently
          catch(Exception e) {
             e.printStackTrace();
          }
       }
    }

    I got erorr like this

    [HTML]
    java.lang.Class NotFoundExcepti on: jdbc:mysql://localhost:3306/
    at java.lang.Class .forName0(Nativ e Method)
    at java.lang.Class .forName(Unknow n Source)
    at librarySystemV5 .DatabaseTableV iewer.main(Data baseTableViewer .java:22)


    [/HTML]



    I used Eclipse IDE,

    Please any body help me, This is for reading the data from table and print the out put on console witht the propper table styles.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Joshepmichel
    [HTML]
    java.lang.Class NotFoundExcepti on: jdbc:mysql://localhost:3306/
    at java.lang.Class .forName0(Nativ e Method)
    at java.lang.Class .forName(Unknow n Source)
    at librarySystemV5 .DatabaseTableV iewer.main(Data baseTableViewer .java:22)


    [/HTML]

    I used Eclipse IDE,

    Please any body help me, This is for reading the data from table and print the out put on console witht the propper table styles.
    Please don't double post; I have removed your other identical thread. About your
    problem: as the error diagnostic message indicates: the jvm can't find the class
    "jdbc:mysql ://localhost:3306" . That is not a class name, it's a URL used to
    establish a connection. You have to load the driver class first. Check your
    documentation.

    kind regards,

    Jos (mod)

    Comment

    • Joshepmichel
      New Member
      • Feb 2008
      • 7

      #3
      Thanks Joe, while I working time my net connection failed, So I post another time too.

      I need to print the console out put witht he tabular alignment, mean to put the out put each column should be come under the line by line

      Like that, But My Screen Out does not coming on those way, all the values are intermixing.

      So, Please to help me.
      Regards,
      Joshep

      Comment

      • Joshepmichel
        New Member
        • Feb 2008
        • 7

        #4
        Originally posted by Joshepmichel
        Thanks Joe, while I working time my net connection failed, So I post another time too.

        I need to print the console out put witht he tabular alignment, mean to put the out put each column should be come under the line by line

        Like that, But My Screen Out does not coming on those way, all the values are intermixing.

        So, Please to help me.
        Regards,
        Joshep

        I couldnt add my actual format on here, because it is also showing as my mixxing outpt like.
        TablCol1 TableCol2
        Value 1 1 Value2 2

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by Joshepmichel
          I couldnt add my actual format on here, because it is also showing as my mixxing outpt like.
          TablCol1 TableCol2
          Value 1 1 Value2 2
          So your first problem is automagically solved? For this little problem add spaces
          to the column names and values so they neatly line up when printed.

          kind regards,

          Jos

          Comment

          • Joshepmichel
            New Member
            • Feb 2008
            • 7

            #6
            Originally posted by JosAH
            So your first problem is automagically solved? For this little problem add spaces
            to the column names and values so they neatly line up when printed.

            kind regards,

            Jos

            Yes, JosAH

            You got my view.

            Comment

            • Joshepmichel
              New Member
              • Feb 2008
              • 7

              #7
              Originally posted by Joshepmichel
              Yes, JosAH

              You got my view.

              Thank you I got it

              Using like that code.
              int N=2;
              System.out.form at("% 5d", N);

              Comment

              Working...