Answer the question
In order to leave comments, you need to log in
How to connect to MySQL8 java database?
So, I have a database,
Schema name user
Root user
Password banana
I write the following code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JavaNioServer {
public static void main (String [] args) {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/user?"
+ "user=root&password=banana");
}catch (Exception ex) {
ex.printStackTrace();
return;
}
}
}
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Unknown Source)
at JavaNioServer.JavaNioServer.main(JavaNioServer.java:9)
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/user?user=root&password=88224148
at java.sql/java.sql.DriverManager.getConnection(Unknown Source)
at java.sql/java.sql.DriverManager.getConnection(Unknown Source)
at JavaNioServer.JavaNioServer.main(JavaNioServer.java:24)
Answer the question
In order to leave comments, you need to log in
I will draw your attention to the fact that a Google search for this error immediately gives a solution.
You need to download and connect the mysql-connector-java library, which can be found on the official mysql website.
Or register it as a dependency in pom.xml/build.gradle.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question