I am relatively new to Java.
Need to use Java with JDBC. I currently try to connect to the database local on my machine.
This is a part of training which is very important to me now.
I am using Java 8:
The training and demonstration accordingly is done using Java 7 Driver provided with an exercise is: mysql-connector-java-5.1.21-bin.jar However I was recommended to use Java 8 driver, so I downloaded mysql-connector-java-8.0.29.jar (Platform independent) from https://dev.mysql.com/downloads/connector/j/
MySQL (local DB) looks like following: Host name: 127.0.0.1 User: root Port: 3306
Copy JDBC Connection String to the clipboard brings the following: jdbc:mysql://127.0.0.1:3306/?user=root
Driver: mysql-connector-java-8.0.29.jar was copied into the project folder "libs" and added to the build path:
The following code I created (class main):
package com.lynda.javatraining.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main {
private static final String USERNAME = "root";
private static final String PASSWORD = "secret";
//private static final String CONN_STRING =
//"jdbc:mysql://localhost/explorecalifornia";
//private static final String CONN_STRING =
//"jdbc:mysql://127.0.0.1/explorecalifornia";
**//This is the string I use currently:**
private static final String CONN_STRING =
"jdbc:mysql://127.0.0.1:3306/explorecalifornia";
//"jdbc:mysql://127.0.0.1:3306/?user=root/explorecalifornia";
public static void main(String[] args) throws SQLException {
Connection conn = null;
try {
conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
System.out.println("Connected");
} catch (SQLException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.err.println(e);
}finally {
if (conn != null) {
conn.close();
}
}
}
}
When I run the code I am getting: java.sql.SQLException: No suitable driver found for jdbc:mysql://127.0.0.1:3306/explorecalifornia
I tried different combinations for CONN_STRING (see all of them commented in the code above), still not working. I tried driver provided originally (mysql-connector-java-5.1.21-bin.jar) with no success (this was expected). Can somebody give an advice, please.