T
T
thatmaniscool2018-07-30 05:59:04
MySQL
thatmaniscool, 2018-07-30 05:59:04

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;
 		}
  }
}

Throws exceptions
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)

As I understand it, to work with the database, you do not need any third-party libraries. It is enough to use those libraries that are already in java.
What am I doing wrong?
PS. Using the official mysql tutorial

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-04-06
@Corexis

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 question

Ask a Question

731 491 924 answers to any question