gpt4 book ai didi

java - 运行 jdbc 程序时线程 "main"java.sql.SQLException 中出现异常?

转载 作者:行者123 更新时间:2023-12-01 16:21:08 26 4
gpt4 key购买 nike

我最近开始学习 JDBC 并编写一个程序在 MySQL 数据库中创建表,但运气不好,我遇到了一些错误,或者可能是我自己无法解决的异常。我只是一个初学者,所以请帮助我摆脱这个困境。

下面是我写的程序:

package first;
import java.sql.*;
public class Firstclass {

public static void main(String[] args) throws Exception
{
// TODO Auto-generated method stub
String driver="com.mysql.jdbc.Driver";
String JDBC_URL=" jdbc:mysql://localhost:3306/demodatabase";
String user_name="root";
String password="toor";
String sqlquery="create table employee(eno number,ename varchar2(10),esal number(10,2),addr varchar2(10))";
Class.forName(driver);
Connection con=DriverManager.getConnection(JDBC_URL,user_name,password);
Statement st=con.createStatement();
st.executeUpdate(sqlquery);
System.out.println("Table created Successfully");
con.close();
}

}

我正在使用 Eclipse IDE。我安装了 oracle java 14.0.1。

下面是我得到的错误/异常:

loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/demodatabase
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at first.Firstclass.main(Firstclass.java:14)

最佳答案

在您使用的版本中,驱动程序类名称已更改为 com.mysql.cj.jdbc.Driver。由于您正在尝试使用纯 java 而不使用 maven 或 gradle,因此您必须提供您正在使用的所有依赖 jar,以便自己工作。检查以下内容:

  • 针对您的连接器版本mysql-connector-java-8.0.20.jar要工作,java 版本需要是 java8。
  • 如果您从源代码构建,请验证 Protocol Buffers 和 Simple Logging Facade API 是否位于类路径中。

You may also need to install the following third-party libraries on your system for Connector/J 8.0 to work: Protocol Buffers (required for using X DevAPI)\ Simple Logging Facade API (required for using the logging capabilities provided by the default implementation of org.slf4j.Logger.Slf4JLogger by Connector/J) . These and other third-party libraries are required for building Connector/J from source (see the section for more information on the required libraries).

此外,在 MySql 文档中,他们建议我们像这样调用 newInstance() (文档对此没有正确的解释):

public class LoadDriver {
public static void main(String[] args) {
try {
// The newInstance() call is a work around for some
// broken Java implementations

Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
} catch (Exception ex) {
// handle the error
}
}
}

官方doc link

此外,从 java 6 开始,我们不需要提供 Class.forName() 来加载驱动程序,因为 java SPI(服务提供者接口(interface))机制应该自动检测适当的驱动程序。即使您像旧方式一样提供它,它仍然可以完美工作。

The DriverManager methods getConnection and getDrivers have been enhanced to support the Java Standard Edition Service Provider mechanism.

Java official link .

关于java - 运行 jdbc 程序时线程 "main"java.sql.SQLException 中出现异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62274270/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com