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:
- Registering the driver
- 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)
-
2What kind of errors are you getting? Any stack traces you can post?Mike Elofson– Mike Elofson2014年06月24日 17:35:09 +00:00Commented Jun 24, 2014 at 17:35
-
Sure, in a moment. Let me edit the original postJuxhin– Juxhin2014年06月24日 17:36:17 +00:00Commented 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?Elliott Frisch– Elliott Frisch2014年06月24日 17:36:20 +00:00Commented 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?Juxhin– Juxhin2014年06月24日 17:38:20 +00:00Commented Jun 24, 2014 at 17:38
-
You need to include the JDBC jar files in your class path while building.Mike Elofson– Mike Elofson2014年06月24日 17:42:01 +00:00Commented Jun 24, 2014 at 17:42
2 Answers 2
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.
1 Comment
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"