1

I imagine this is a simple syntax error on my part. I bought a web-hosting plan a while back and have some empty MySQL databases that I simply want to practice connecting to and passing some data.

I've found numerous posts on how to do this, but all involve "localhost" as the IP. I have gathered this syntax from others posts, but still get the error message

Unable to connect to database error: java.sql.SQLException: No suitable driver found for jdbc:mysql://ipAddress:port/dbName

Everywhere I've looked states this is a URL issue, or that the driver wasn't loaded. I've downloaded and placed the .jar in Classpath within IntelliJ, but couldn't find anything additional about loading the driver file. This is my code, the IP Address is the IP of my website.

Connection connection = null;

        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();

        } catch (Exception err){
            System.out.println(err.getMessage());
        }

        //Establish connection using DriverManager
        try {
            String host = "jdbc:mysql://ipAddress:3306/dbName";
            String uName = "userName";
            String uPass= "userPass";
            connection = DriverManager.getConnection(host, uName, uPass);
        } catch (SQLException err) {
            System.out.println("Unable to connect to database error: "+err);
        }

I've tried changing the 'host' around to some different things I had seen around the web:

jdbc:mysql:ipAddress:3306/dbName
jdbc:mysql://ipAddress/dbName

I've followed this as well, but my problem mainly lies in the URL I think. http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=%2Frzaha%2Fdb2drivr.htm

Any assistance would be appreciated!

EDIT: I downloaded that .jar and didn't know where to place it so I just put it in this directory, then I linked this directory as a Classpath. //Library/Java/JavaVirtualMachines/jdk1.7.0_12.jdk/Contents/MacOS/mysql-connector-java-5.1.22-bin.jar!/ From this link: JConnector

I run this from my machine and want it to connect to my web-hosting providers database. The program is a simple Java Application that gets input from a text boxes within a JFrame.

9
  • Have you checked if the database server allows remote connections? Commented Jan 30, 2013 at 15:33
  • 2
    This is a classpath issue. What kind of application are you developing? A webapp? If so, is the mysql driver jar file in the WEB-INF/lib directory of the deployed webapp? Commented Jan 30, 2013 at 15:33
  • Are you trying to connect from code running in your IDE (IntelliJ) to remote mySQL server with your hosting or to a local mySQL Server on your dev machine? Commented Jan 30, 2013 at 15:35
  • @NightWhisper Yes, I forgot to mention this. I allowed my machine's IP address through "Remote MySQL". Commented Jan 30, 2013 at 16:27
  • @JBNizet It's a simple JFrame that inputs text (on my machine) and I want to connect to the database and pass that data (ultimately, but for now I am just trying to connect). Commented Jan 30, 2013 at 16:28

4 Answers 4

2

Your specific error message:

Unable to connect to database error: java.sql.SQLException: 
No suitable driver found for jdbc:mysql://ipAddress:port/dbName

Will occur when the executing JVM is not able to find the your application is not able to find the MySQL Connector/J JAR in its classpath.

Within IntelliJ, you can go to File->Project Structure->Modules. For your current Module, click on the Dependencies tab. Click on the icon to add a JAR or Library, find the MySQL Connector/J JAR.

Now, your executing JVM should find it, if you are running it through IntelliJ.

Sign up to request clarification or add additional context in comments.

Comments

0

I suggest to replace

 try {
     Class.forName("com.mysql.jdbc.Driver").newInstance();
 } catch (Exception err){
     System.out.println(err.getMessage());
 }

with

Class.forName("com.mysql.jdbc.Driver")

1) newInstance() is irrelevant

2) if there is no such driver let it throw an exception

1 Comment

When I did this, a red line appeared under the Class.forName("com.mysql.jdbc.Driver"); and it says java: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
0

"No suitable driver found" indicates that the JVM can't find your mysql connector jar file. Which version of the mysql java connector are you using? If putting it in the IntelliJ classpath isn't working try making it a dependency to your project.

1 Comment

I am using version 5.1.22, when I downloaded it I just placed it in any directory, grouped it with some Java files. Does it have to be somewhere specific? Directory: //Library/Java/JavaVirtualMachines/jdk1.7.0_12.jdk/Contents/MacOS/mysql-connector-java-5.1.22-bin.jar!/
0

Your code seems to work fine the problem must be with the connector. Which connector are you currently using? You can use J connector and add jar files to the build path.

2 Comments

I downloaded it from dev.mysql.com/downloads/connector/j, then linked the .jar in.
if you are using eclipse then go to configure build path and then add external jars and browse to where you have extracted J connector and add the jar file to the project..try to build it should work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.