Mattiavelli
Mattiavelli

Reputation: 898

Connecting to external mysql database with DriverManager - Java

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.

Upvotes: 1

Views: 3810

Answers (4)

Philip Tenn
Philip Tenn

Reputation: 6073

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.

Upvotes: 2

onkar
onkar

Reputation: 26

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.

Upvotes: 0

Jerry B
Jerry B

Reputation: 135

"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.

Upvotes: 0

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

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

Upvotes: 0

Related Questions