[英]Exception in thread “main” java.sql.SQLException while running jdbc program?
I have recently started learning JDBC and write a program to create table in a MySQL database, but bad luck I am getting some error or may be exceptions which I am unable to figure it out on my own.我最近开始学习 JDBC 并编写程序在 MySQL 数据库中创建表,但运气不好,我遇到了一些错误,或者可能是我自己无法解决的异常。 I am just a beginner so please help me to get if of this rid.
我只是一个初学者,所以请帮我解决这个问题。
Below is the program I have written:下面是我写的程序:
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();
}
}
I am working on Eclipse IDE.我正在研究 Eclipse IDE。 And i have oracle java 14.0.1 installed.
我安装了 oracle java 14.0.1。
Below is the error/Exceptions I get:以下是我得到的错误/异常:
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)
The driver class name was changed to com.mysql.cj.jdbc.Driver
in the version that you are using.驱动程序 class 名称已更改为
com.mysql.cj.jdbc.Driver
版本中的 Z84BEFFD3A0D49636A58CE6080CAA8。 Since you are trying this out with pure java without maven or gradle you will have to provide all the dependent jars that you are using for this to work yourself. Since you are trying this out with pure java without maven or gradle you will have to provide all the dependent jars that you are using for this to work yourself. Check the following:
检查以下内容:
mysql-connector-java-8.0.20.jar
to work, the java version needs to be java8.mysql-connector-java-8.0.20.jar
版本正常工作,java 版本需要是 java8。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).
您可能还需要在系统上安装以下第三方库以使 Connector/J 8.0 工作:Protocol Buffers(使用 X DevAPI 所需)\ Simple Logging Facade API(使用默认实现提供的日志记录功能所需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).
从源代码构建 Connector/J 需要这些和其他第三方库(有关所需库的更多信息,请参阅 章节)。
Also, in the MySql docs they suggest we call newInstance() like this (the documentation doesn't have proper explanation for this):此外,在 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
}
}
}
Also, since java 6 we need not provide the Class.forName()
to load the driver because of the java SPI (Service Provider Interface) mechanism which should automatically detect the appropriate driver.另外,由于 java 6 我们不需要提供
Class.forName()
来加载驱动程序,因为 java 应该自动检测适当的驱动程序接口机制。 It will still work perfectly even if you provide it like the old way.即使您像旧方式一样提供它,它仍然可以完美运行。
The DriverManager methods getConnection and getDrivers have been enhanced to support the Java Standard Edition Service Provider mechanism.
DriverManager 方法 getConnection 和 getDrivers 已得到增强,以支持 Java 标准版服务提供程序机制。
Java official link . Java 官方链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.