JDBC connection - how to do that ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pjerald
    New Member
    • Oct 2007
    • 77

    JDBC connection - how to do that ?

    I have written a small code to get connection with mysql database. But it throws exception
    Code:
    No suitable driver
    .
    this is my code...
    Code:
    public static void main(String args[]) {
            Connection con = null;
    
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                con = DriverManager.getConnection("jdbc:/home/pjerald/Installs/mysql-standard-4.1.16-pc-linux-gnu-i686/data:///test","pjerald", "5623");
    
                if(!con.isClosed())
                    System.out.println("Successfully connected to " +
                            "MySQL server using TCP/IP...");
    
            } catch(Exception e) {
                System.err.println("Exception: " + e.getMessage());
            } finally {
                try {
                    if(con != null)
                        con.close();
                } catch(SQLException e) {}
            }
        }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by pjerald
    I have written a small code to get connection with mysql database. But it throws exception
    Code:
    No suitable driver
    .
    this is my code...
    Code:
    public static void main(String args[]) {
            Connection con = null;
    
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                con = DriverManager.getConnection("jdbc:/home/pjerald/Installs/mysql-standard-4.1.16-pc-linux-gnu-i686/data:///test","pjerald", "5623");
    
                if(!con.isClosed())
                    System.out.println("Successfully connected to " +
                            "MySQL server using TCP/IP...");
    
            } catch(Exception e) {
                System.err.println("Exception: " + e.getMessage());
            } finally {
                try {
                    if(con != null)
                        con.close();
                } catch(SQLException e) {}
            }
        }
    You need to have the MySQL driver in your classpath.

    Comment

    • pjerald
      New Member
      • Oct 2007
      • 77

      #3
      Originally posted by r035198x
      You need to have the MySQL driver in your classpath.
      I have copied the jar mysql-connector-java-5.0.8-bin.jar to my ~/jdk1.5.0_07/lib directory. Is there any version problem ?
      Thanks,
      P.Jerald

      Comment

      • pjerald
        New Member
        • Oct 2007
        • 77

        #4
        Hi friends please help..

        I got class not found error first.

        Code:
        [$ ~]$ javac JdbcExample2.java -d .; java test.JdbcExample2
        Exception: com.mysql.jdbc.Driver
        java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
                at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
                at java.security.AccessController.doPrivileged(Native Method)
                at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
                at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
                at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
                at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
                at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
                at java.lang.Class.forName0(Native Method)
                at java.lang.Class.forName(Class.java:164)
                at test.JdbcExample2.main(JdbcExample2.java:13)

        Then i set my class path.


        And again i ran the program. Now it throws,


        Code:
        [$ ~]$ export CLASSPATH=$CLASSPATH:/home/pjerald/jdk1.5.0_07/lib/mysql-connector-java-5.0.8-bin.jar
        [$ ~]$ javac JdbcExample2.java -d .; java test.JdbcExample2
        Exception: No suitable driver
        java.sql.SQLException: No suitable driver
                at java.sql.DriverManager.getConnection(DriverManager.java:545)
                at java.sql.DriverManager.getConnection(DriverManager.java:171)
                at test.JdbcExample2.main(JdbcExample2.java:15)

        Comment

        • pjerald
          New Member
          • Oct 2007
          • 77

          #5
          First it said class not found exception. Then i set my class path.
          Code:
          [$ ~]$ javac JdbcExample2.java -d .; java test.JdbcExample2
          Exception: com.mysql.jdbc.Driver
          java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
                  at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
                  at java.security.AccessController.doPrivileged(Native Method)
                  at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
                  at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
                  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
                  at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
                  at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
                  at java.lang.Class.forName0(Native Method)
                  at java.lang.Class.forName(Class.java:164)
                  at test.JdbcExample2.main(JdbcExample2.java:13)

          Then it throws


          Code:
          [$ ~]$ export CLASSPATH=$CLASSPATH:/home/pjerald/jdk1.5.0_07/lib/mysql-connector-java-5.0.8-bin.jar
          [$ ~]$ javac JdbcExample2.java -d .; java test.JdbcExample2
          Exception: No suitable driver
          java.sql.SQLException: No suitable driver
                  at java.sql.DriverManager.getConnection(DriverManager.java:545)
                  at java.sql.DriverManager.getConnection(DriverManager.java:171)
                  at test.JdbcExample2.main(JdbcExample2.java:15)
          
          s

          Comment

          Working...