如何使用Java10连接到mysql8.x数据库?

bttbmeg0  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(265)

首先,对不起我的英语不好。我有方案和表在基地,我想使连接到它。1) 方案名称-用户。2) 用户名-root 3)密码-banana。我使用mysql的教程编写了这段代码

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)

我使用了标准的java.sql libs,我不明白我做错了什么。。。

rqmkfv5c

rqmkfv5c1#

ClassNotFoundException 抛出,因为您尚未添加相关的mysql连接器。添加mysql 8 connector for java。
mysql连接器/j» 8.0.11-罐

des4xlb0

des4xlb02#

下载此jar加载项 buildpath/classpath .

相关问题