简体   繁体   中英

MySQL/JDBC Issues

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)

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM