简体   繁体   English

JDBC 驱动程序错误,无法连接到 mysql 服务器

[英]JDBC driver error and cannot connect to mysql server

I keep getting the error messages when I run my code attempting to connect to an sql server当我运行我的代码尝试连接到 sql 服务器时,我不断收到错误消息

Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:375)

This is my code.这是我的代码。 I've made sure to add connector/J to the classpath and still no chamge我确保将连接器/J 添加到类路径中,但仍然没有变化

import java.sql.*;

public class App {
    
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/vehicle", "root", "root"); // For MySQL only

        vehicle focus = new vehicle("Ford", "Focus", 200, 2004, 3000);

        System.out.println(focus.getMake());
    }
}

First of all, loading the JDBC driver using Class.forName is not necessary since JDBC 4.0 which was made available in 2006 (the driver registers its implementation as service provider and will be loaded by the ServiceLoader ).首先,不需要使用Class.forName加载 JDBC 驱动程序,因为JDBC 4.0 于 2006 年提供,将由服务提供程序寄存器加载(服务驱动程序寄存器)

Just remove this line and it should work.只需删除此行,它应该可以工作。 If you need the class name, its a good idea to consult the documentation of Connector/J.如果您需要 class 名称,最好查阅 Connector/J 的文档 Take care to read the docs for the correct driver version because the class name changed (currently it's com.mysql.cj.jdbc.Driver). Take care to read the docs for the correct driver version because the class name changed (currently it's com.mysql.cj.jdbc.Driver).

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用java jdbc驱动程序连接到Mysql - Cannot connect to Mysql using java jdbc Driver 无法为连接URL'jdbc:mysql // localhost:3306 /创建类'com.mysql.jdbc.Driver'的JDBC驱动程序 - Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'jdbc:mysql//localhost:3306/ 无法使用 MS JDBC 驱动程序从 CentOS 8 服务器连接到 SQL 服务器服务器:使用安全连接 ZEA52C2Z42253C5F99C23Z 错误 - Cannot connect to SQL Server server with MS JDBC Driver from CentOS 8 server: SSL error but not using secure connection 带有mysql的Tomcat:“无法为连接URL'null'创建类”的JDBC驱动程序” - Tomcat with mysql : “ Cannot create JDBC driver of class '' for connect URL 'null' ” 无法为连接URL创建类'com.mysql.jdbc.Driver'的JDBC驱动程序 - Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 如何解决“无法为连接URL创建类'com.mysql.jdbc.Driver'的JDBC驱动程序” - How to resolve “Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL” MySQL JDBC驱动程序错误 - MySQL JDBC driver error 无法为连接URL'null'创建类''的JDBC驱动程序:Tomcat和SQL Server JDBC驱动程序 - Cannot create JDBC driver of class '' for connect URL 'null' : Tomcat & SQL Server JDBC driver 无法连接到JDBC驱动程序MySQL - cant connect to jdbc driver mysql JDBC无法连接到mysql - JDBC Cannot connect to mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM