Hey guys,
I'm trying to use MySQL and I have a server setup and ready to go and I'm 100% sure it works.

I read a tutorial page and tried to find more but I couldn't. What I did was drag the .jar file that came in the Connector/J.zip (3.0) into "C:\Program Files\Java\jre6\lib\ext" I tried to add the package to my program using import but that didn't work and from the examples they gave in the tutorial I shouldn't have to!

=D
PO.

here is the tutorial http://www.developer.com/java/data/article.php/3417381

explain "didn't work"

First you could check whether your MySQL installation really works, try telnet <IP-Address-of-machine-which-has-MySQL-server> 3306 on your console, if this gives you a "Connection Refused" error then it means your MySQL server has not been installed correctly (or is running on a different port, 3306 is just the default).

Now to use the Type 4 JDBC driver (from MySQL) for MySQL all you need to do is :-

  1. Ensure that the mysql-connector jar file is in your classpath, But since you have pasted it in your "$JRE_HOME/lib/ext" it by default will come into your classpath without you having to specify anything from the command line, All you have to do is verify that you are using the correct JRE installation.
  2. In your code you need to load the driver using Class.forName("com.mysql.jdbc.Driver"); , Apart from that everything else is the same as mentioned in this tutorial here.Note that this tutorial uses Apache Derby hence they load the driver using the following statement:- Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); .

Note if you get a ClassNotFoundException it means that Java cannot find the mysql-driver i.e your mysql-connector jar file is not in the classpath.
However in case you get any other SQLException, try to print out the SQL state (using getSQLState()) and compare it with the last column of this table to figure out where your SQL Statement is failing.

Also note that we do not normally need to use any package from the mysql-connector apart from loading the Driver, the ResultSet, Statement, PreparedStatement etc should be used from the java.sql package.

Okay i will show you my code and the error I am getting.
My Code

public class Jdbc12 {
  public static void main(String args[]){
    System.out.println(
                  "Copyright 2004, R.G.Baldwin");
    try {
      Statement stmt;
      Class.forName("com.mysql.jdbc.Driver");
      String url = "jdbc:mysql://mysql24.ixwebhosting.com:3306/mysql";
      Connection con = DriverManager.getConnection(url,"USERNAME", "PASSWORD");
      System.out.println("URL: " + url);
      System.out.println("Connection: " + con);
      stmt = con.createStatement();
      stmt.executeUpdate(
              "GRANT SELECT,INSERT,UPDATE,DELETE," +
              "CREATE,DROP " +
              "ON JunkDB.* TO 'auser'@'localhost' " +
              "IDENTIFIED BY 'drowssap';");
      con.close();
    }catch( Exception e ) {
      e.printStackTrace();
    }//end catch
  }//end main
}//end class Jdbc11

and my error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
	DriverManager cannot be resolved
	Type mismatch: cannot convert from Statement to Statement

	at Jdbc12.main(jConnector.java:11)

And I put the jar in the correct folder, i didn't rename it or anything just copied it straight in as well I tested my server and connected fine.

Hope this can clear it up a little. Maybe it's my syntax but I don't think it should be I got it straight from the tutorial.

Thanks for your time

Well that you showed was the Runtime error which your JRE was giving you an it clearly states that your code has not yet compiled correctly, You need to show your import statements, chances are you are using the "Statement", from com.mysql.jdbc package, when it fact as I had mentioned in my previous post you should use them from the java.sql package.
Also you have not imported the DriverManager class. Obviously you are not going through the tutorial properly.

EDIT:
You should also check what your compiler is throwing up at you because clearly your code has not compiled, and you definitely cannot run your program without compiling it first.

And I put the jar in the correct folder, i didn't rename it or anything just copied it

Also renaming the JAR would not make any difference.

Two comments
1) You may want to get latest Connector/J 5.1
2) Adding something to JDK or JRE installation is not the best thing to do and untidy, what if you update/reinstall Java. Better to keep separated folder with JAR files you often use and through your IDE to either link whole folder to general settings or just link up wanted JAR with your current project

Yeah sorry I didn't really read your post extremely closely.

here is what I have now:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class something {

  public static void main(String args[]) {
    Connection con = null;

    try {
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      con = DriverManager.getConnection("jdbc:mysql:##.###.###.##:3306/dbName",
        "USERNAME", "PASSWORD");

      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) {}
    }
  }
}

here is the error I am getting

Exception: No suitable driver found for jdbc:mysql:##.###.###.##:3306/dbName


And here is a screen shot of my IDE

I am indeed confused. Why is this error coming up? What did I do wrong? My IDE is detecting the package!

Please keep in mind I start learning JAVA late september I am a newbie!

Thanks PO

You have mistake here, missing 2 x back slash

con = DriverManager.getConnection("jdbc:mysql://##.###.###.##:3306/dbName",
        "USERNAME", "PASSWORD");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.