Results 1 to 9 of 9
  1. #1
    user78 is offline Member
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Default connection Java/MySQL

    Hi all, I'm writing my first java program on Ubuntu system.
    I installed on Ubuntu system the libmysql-java package, I writed my program and then I compiled it, but I have the following message in running it:

    Java Code:
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    	at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    	at java.lang.Class.forName0(Native Method)
    	at java.lang.Class.forName(Class.java:186)
    	at Primo.main(Primo.java:9)
    My program is the following:

    Java Code:
    import java.sql.*;
    
    class Primo {
    
      public static void main(String[] args) {
        try {
          Class.forName("com.mysql.jdbc.Driver");
          Connection conn = DriverManager.getConnection("dbname", "root", "mypasswd");
          Statement cmd = conn.createStatement();
          String query = "SELECT * from mytable;";
          ResultSet res = cmd.executeQuery(query);
          while (res.next()) {
            System.out.println(res.getString("Codice"));
          }
        }
        catch (ClassNotFoundException e) {
          e.printStackTrace();
        }
        catch (SQLException e) {
          e.printStackTrace();
        }
      }
    
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: connection Java/MySQL

    You need the MySQL JDBC Driver (in classpath) first
    MySQL :: Download Connector/J

  3. #3
    aljassi is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default Re: connection Java/MySQL

    download mysql driver, and right click on your java project-->Build path-->configure Build Path---> library----> add external JARS. and select your mysql driver you downloaded. click on ok and its done.

  4. #4
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: connection Java/MySQL

    1st you have to set classpath.
    after classpath
    you have to provide the connection
    the connection is
    String url = "jdbc:mysql://localhost:3306/";
    String db = "myDB"; //it is ur database name
    Connection con = DriverManager.getConnection(url+db,"root", "mypasswd");

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: connection Java/MySQL

    Quote Originally Posted by chandrasekhar.melam View Post
    String db = "myDB"; //it is ur database name
    Just so you know - it is "your", not "ur". Note how you also write "you" and not "u".

  6. #6
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: connection Java/MySQL

    Quote Originally Posted by gimbal2 View Post
    Just so you know - it is "your", not "ur". Note how you also write "you" and not "u".
    ur is the short form of Your okay....

  7. #7
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: connection Java/MySQL

    Quote Originally Posted by chandrasekhar.melam View Post
    ur is the short form of Your okay....
    What gimbal2 try to say is that we have to use a real word in the forum to make everything clear.

  8. #8
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: connection Java/MySQL

    Quote Originally Posted by user78 View Post
    Java Code:
          Class.forName("com.mysql.jdbc.Driver");
    Using the latest JDBC API the driver will be automatically loaded. So you don't need to register the driver manually.

  9. #9
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: connection Java/MySQL

    Quote Originally Posted by wsaryada View Post
    Using the latest JDBC API the driver will be automatically loaded. So you don't need to register the driver manually.
    Sir, im new to jdbc,upto now i run the jdbc programs by registering drivers manually. Could you send a jdbc program without registering the driver manually please?

    thank you in advance

Similar Threads

  1. Replies: 1
    Last Post: 02-19-2011, 11:34 PM
  2. connection my mysql
    By pranjul137 in forum New To Java
    Replies: 5
    Last Post: 04-06-2010, 09:26 PM
  3. TWO IPs FOR MY MYSQL CONNECTION
    By tugalsan in forum JDBC
    Replies: 2
    Last Post: 03-21-2010, 09:46 PM
  4. Java-mysql connection
    By Kligham in forum New To Java
    Replies: 16
    Last Post: 11-25-2009, 02:28 PM
  5. java mysql connection
    By sysout in forum New To Java
    Replies: 5
    Last Post: 10-31-2009, 11:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •