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)
Juxhin
  • 5,068
  • 8
  • 29
  • 55
  • 2
    What kind of errors are you getting? Any stack traces you can post? – Mike Elofson Jun 24 '14 at 17:35
  • Sure, in a moment. Let me edit the original post – Juxhin Jun 24 '14 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 Jun 24 '14 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 Jun 24 '14 at 17:38
  • You need to include the JDBC jar files in your class path while building. – Mike Elofson Jun 24 '14 at 17:42
  • Alright guys, fixed the first issue. However now it's telling me the Driver specified is not suitable for the DB_URL I have. I shall update my original post – Juxhin Jun 24 '14 at 17:51
  • @Booch Are you sure that it's a mysql database? Is it listening on the standard port? – Elliott Frisch Jun 24 '14 at 17:54
  • Yes I'm definitely sure it's a MySQL database as shown: http://prntscr.com/3w2l91 running MySQL version 5.1.68-cll. @The second part of your question, what's the standard listening port if I may ask? – Juxhin Jun 24 '14 at 17:58
  • So I fixed the issue, it's connecting fine. I think you're right Elliott. I'm not being given access permissions to my DB using my control panel username and password..The following is being printed out - http://prntscr.com/3w2nh4 – Juxhin Jun 24 '14 at 18:01

2 Answers2

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.

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
  • Ah yes thanks for pointing that, I changed it earlier as I had the details in there. Still with the same issue though – Juxhin Jun 24 '14 at 17:38
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?

Community
  • 1
  • 1
Alex Beardsley
  • 20,988
  • 15
  • 52
  • 67
  • Oh yes! This did work, need to see if I can login with the correct login details, thanks alot man – Juxhin Jun 24 '14 at 17:59
  • Strange, I'm being given access to my database though. However the DB_URL did work! That's a step forward I guess – Juxhin Jun 24 '14 at 18:01
  • 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. – Alex Beardsley Jun 24 '14 at 18:06
  • 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 – Juxhin Jun 24 '14 at 18:11
  • 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. – Alex Beardsley Jun 24 '14 at 18:18
  • Alright that sounds like the right thing to do. Thanks alot Alex! – Juxhin Jun 24 '14 at 18:19