jdbc connect to mysql database

Discussion in 'Java' started by Marcus Krieger, Dec 10, 2004.

  1. I am using JBuilder and I would like to connect to a mysql database.
    I was able to connect to a oracle database, however it does not work with
    mysql!
    I downloaded the newest drivers from the mysql webseite and put them into my
    java/bin directory.
    As I don't know exactly which java folder Jbuilder is using I have put them
    in both folder - my java folder and the java folder in jbuilder.
    Additionally, I tried to import the file from Jbuilder, and put them into
    "Project", "User Home" and "Jbuilder".

    Whatever that exactly does, with oracle it had worked.
    Using mysql, I get the following error message:

    driver:com.mysql.jdbc.Driver url:jdbc:mysql:
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    java.sql.SQLException: No suitable driver
    java.sql.SQLException: No suitable driver

    That's my code:
    [..]
    jdbc_driver="com.mysql.jdbc.Driver";
    db_login = "root";
    db_password = "";
    db_url = "jdbc:mysql://localhost:3306/dbname";
    try
    {
    Class.forName(jdbc_driver);
    }
    [..]
    DriverManager.getConnection(db_url, db_login, db_password);

    Any ideas, what I am doing wrong here?

    Thanks,

    Marcus
     
    Marcus Krieger, Dec 10, 2004
    #1
    1. Advertising

  2. Marcus Krieger wrote:
    > driver:com.mysql.jdbc.Driver url:jdbc:mysql:
    > java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    > java.sql.SQLException: No suitable driver
    > java.sql.SQLException: No suitable driver


    Most likely the connector jar is not on your classpath. If you cannot
    figure out where to place it, it SHOULD work if you place it in your
    %SYSTEM_JRE%/lib/endorsed directory.
     
    Collin VanDyck, Dec 10, 2004
    #2
    1. Advertising

  3. Marcus Krieger

    Ian T Guest

    Marcus Krieger wrote:

    If you Read the first page of The Fine Documentation that comes with the
    official jdbc driver (www.mysql.com), you will find a code example that
    will work immediately. My java implementation would only work with the
    newInstance code below.

    Something like:

    try {
    // The newInstance() call is a work around for some
    // broken Java implementations
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    }
    catch (Exception ex) { // handle the error }
    java.sql.Connection c =
    DriverManager.getConnection("jdbc:mysql://localhost/recruitment?user=root&password=");
    java.sql.ResultSet r = s.executeQuery("SELECT * FROM Candidates");
    while ( r.next())
    {
    System.out.println(r.getString("fullname"));
    System.out.println(r.getString("phone"));
    }
    s.close();
    c.close();


    Ian

    > I am using JBuilder and I would like to connect to a mysql database.
    > I was able to connect to a oracle database, however it does not work with
    > mysql!
    > I downloaded the newest drivers from the mysql webseite and put them into my
    > java/bin directory.
    > As I don't know exactly which java folder Jbuilder is using I have put them
    > in both folder - my java folder and the java folder in jbuilder.
    > Additionally, I tried to import the file from Jbuilder, and put them into
    > "Project", "User Home" and "Jbuilder".
    >
    > Whatever that exactly does, with oracle it had worked.
    > Using mysql, I get the following error message:
    >
    > driver:com.mysql.jdbc.Driver url:jdbc:mysql:
    > java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    > java.sql.SQLException: No suitable driver
    > java.sql.SQLException: No suitable driver
    >
    > That's my code:
    > [..]
    > jdbc_driver="com.mysql.jdbc.Driver";
    > db_login = "root";
    > db_password = "";
    > db_url = "jdbc:mysql://localhost:3306/dbname";
    > try
    > {
    > Class.forName(jdbc_driver);
    > }
    > [..]
    > DriverManager.getConnection(db_url, db_login, db_password);
    >
    > Any ideas, what I am doing wrong here?
    >
    > Thanks,
    >
    > Marcus
    >
    >
    >
     
    Ian T, Dec 11, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chow
    Replies:
    2
    Views:
    736
  2. dilip
    Replies:
    4
    Views:
    13,405
  3. kevin Liu
    Replies:
    19
    Views:
    725
    Firefox 32
    Oct 7, 2008
  4. Allen Marshall
    Replies:
    1
    Views:
    218
    Gunnar Hjalmarsson
    Jan 24, 2004
  5. Replies:
    2
    Views:
    330
Loading...

Share This Page