0

I'm trying to setup a mySQL connector and I'm not sure what's going wrong. Here is the error:

SQLException: No suitable driver found for jdbc:mysql://localhost/db?user=root&password=

Here is my java:

    //      ESTABLISH DRIVER INSTANCE
    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
    }


    //      ESTABLISH DB CONNECTION
    try {
        conn = DriverManager.getConnection("jdbc:mysql://localhost/db?" +
                               "user="+user+"&password="+pass);

        // Do something with the Connection
        //
        //

    } catch (SQLException ex) {
        // handle any errors
        System.out.println("SQLException: " + ex.getMessage());
        System.out.println("SQLState: " + ex.getSQLState());
        System.out.println("VendorError: " + ex.getErrorCode());
    }

Here is my build.xml:

<project>

<target name="run">

    <javac srcdir="." destdir=".">

         <classpath>
               <pathelement path="./mysql-connector-java-5.1.38i/mysql-connector-java-5.1.38-bin.jar"/>
               <pathelement location="./DB_Test.class"/>
         </classpath>

    </javac>

    <java classname="DB_Test">

          <classpath>
               <pathelement path="./mysql-connector-java-5.1.38i/mysql-connector-java-5.1.38-bin.jar"/>
               <pathelement location="."/>
          </classpath>

    </java>

</target>

</project>

I think the issue is likely to be in the build.xml because I am very inexperienced with ant. I'm not sure if both of those "pathelement path"s are necessary or if either one of them works. I'm wondering if I need to resort to eclipse just for this one jar.

I also think there could be an issue with my connection url syntax. Is "db" the name of the connection? Or the schema? Do I need a port number after my localhost?

Nate Schultz
  • 181
  • 1
  • 14
  • Can't help on the global solution, but on url syntax : "db" is the database to connect to and if you do not supply a port number, it is default to 3306 (see https://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html). – P.A. Cros Feb 16 '16 at 09:43

2 Answers2

0

maybe you can change it to

Connection   conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/XX","root","yourpassword" 
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
0

My issue was that I didn't add the jar to the classpath correctly. I tried it command line, with the -cp argument, and it worked. I found this very helpful.

If you're doing it "plain vanilla" in the command console, then you need to specify the path to the JAR file in the -cp or -classpath argument when executing your Java application.

java -cp .;/path/to/mysql-connector.jar com.example.YourClass

The . is just there to add the current directory to the classpath as well so that it can locate com.example.YourClass and the ; is the classpath separator as it is in Windows. In Unix and clones : should be used.

Community
  • 1
  • 1
Nate Schultz
  • 181
  • 1
  • 14