1

I'm following a tutorial and am completely new to JDBC, I have solid knowledge on Java and brief knowledge on SQL. I have an online VPS which has a small database in it(Just using it to learn this) but keep getting errors when:

  1. Registering the driver
  2. Opening a connection

Here is my simple class. If anyone could help, would be greatly appreciated.

import java.sql.*;
public class ExampleJava {
static final String USER = "HIDDEN_USERNAME";
static final String PASS = "HIDDEN_PASSWORD";
static final String DB_URL = "studiobooch.x10.mx";
static Connection conn;
public static void main(String[] args) {
 System.out.println("Connecting to database");
 try {
 Class.forName("com.mysql.jdbc.Driver");
 conn = DriverManager.getConnection("DB_URL", USER, PASS);
 }catch(SQLException e){
 System.out.println("Connection error");
 e.printStackTrace();
 }catch(ClassNotFoundException e) {
 e.printStackTrace();
 }
}
}

Prints out the following:

Connecting to database
 java.sql.SQLException: No suitable driver found for studiobooch.x10.mx
at java.sql.DriverManager.getConnection(DriverManager.java:596)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at ExampleJava.main(ExampleJava.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at 
 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
asked Jun 24, 2014 at 17:34
9
  • 2
    What kind of errors are you getting? Any stack traces you can post? Commented Jun 24, 2014 at 17:35
  • Sure, in a moment. Let me edit the original post Commented Jun 24, 2014 at 17:36
  • Your classpath is missing the mysql driver jar(s). Also, are you sure that your VPS allow remote connections to their databases? Commented Jun 24, 2014 at 17:36
  • It does allow, when I asked live support stating my reasons they told me it's fine. As far as drivers goes, mind directing me to the jar files you have in mind? Commented Jun 24, 2014 at 17:38
  • You need to include the JDBC jar files in your class path while building. Commented Jun 24, 2014 at 17:42

2 Answers 2

3

change conn = DriverManager.getConnection("DB_URL", USER, PASS); with conn = DriverManager.getConnection(DB_URL, USER, PASS);

Edit : for the ClassNotFoundException, make sure you correctly added the .jar driver to your lib.

answered Jun 24, 2014 at 17:36
Sign up to request clarification or add additional context in comments.

1 Comment

Ah yes thanks for pointing that, I changed it earlier as I had the details in there. Still with the same issue though
1

I think DB_URL needs to be a JDBC connection string, as opposed to just a domain. Try something akin to this:

static final String DB_URL = "jdbc:mysql://studiobooch.x10.mx/myDatabase"

From here: What is the MySQL JDBC driver connection string?

answered Jun 24, 2014 at 17:57

6 Comments

Oh yes! This did work, need to see if I can login with the correct login details, thanks alot man
Strange, I'm being given access to my database though. However the DB_URL did work! That's a step forward I guess
Check to make sure you're using the right port as well. The default for MySQL is 3306. To test if it is actually an authentication issue on their end, you can try connecting to it via a standalone MySQL client and seeing if you are able to authenticate.
So I remove the USER and PASS parameters from getConnection(..) and use PORT and DB_URL instead? Issue being, I'm honestly not sure what port this webhost is running
Sorry, I edited my answer to a better way to test this before you commented. If you get the same error in a standalone MySQL client, then likely the permissions are not set up correctly on their end, or for some reason the port needs to be changed (for example, if they have different clients on different ports, in this case you would have to find out from them the proper port). However if you able to connect, you know it is something else with your program.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.