-3

i have an problem could anyone help me? i'm trying to connect to mysql using java but get an error

the error:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)

code :

public static Connection c;

static String host = "localhost";
static String port = "3306";
static String database = "";
static String username = "root";
static String password = "";

public static void connect() {
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();

        c = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
10
  • Have you configured jdbc connector? Commented Jan 30, 2019 at 16:13
  • Show your code. Commented Jan 30, 2019 at 16:14
  • i've added it into my project it exports with it and to run it i use this command java -cp connector2 -jar GCDSS.jar Commented Jan 30, 2019 at 16:14
  • 1
    the connector JAR name needs to appear in the classpath. you don't appear to have the correct file name. Commented Jan 30, 2019 at 16:18
  • 1
    The mysql jar file is obviously missing. Commented Jan 30, 2019 at 16:18

2 Answers 2

0

Error trace shows its not able to resolve "com.mysql.jdbc.Driver"

Check whether dependency for mysql.jar is provided in class path. If its maven project check pom.xml for mysql dependency provided

Sign up to request clarification or add additional context in comments.

Comments

-1

Just download "ojdbc7.jar" and import that jar to your project.

2 Comments

ojdbc7 is for Oracle, the error indicates the OP wants to use MySQL...
Sorry i meant mysql-connector-java-8.x.jar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.