简体   繁体   English

运行 jdbc 程序时线程“主”java.sql.SQLException 异常?

[英]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:检查以下内容:

  • For your version of the connector 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。
  • Verify that Protocol Buffers and Simple Logging Facade API are on the classpath if you are building from source.如果您是从源代码构建,请验证协议缓冲区和简单日志记录外观 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).您可能还需要在系统上安装以下第三方库以使 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
        }
    }
}

Official doc link官方文档链接

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.

相关问题 线程“主”中的异常java.sql.SQLException:找不到数据 - Exception in thread “main” java.sql.SQLException: No data found 线程“ main”中的异常java.sql.SQLException:找不到适合的驱动程序 - Exception in thread “main” java.sql.SQLException: No suitable driver found for 线程“ main”中的异常java.sql.SQLException:缺少定义 - Exception in thread “main” java.sql.SQLException: Missing defines 线程“ main”中的异常java.sql.SQLException:未为参数1指定值 - Exception in thread “main” java.sql.SQLException: No value specified for parameter 1 线程“ main”中的异常java.sql.SQLException:无法打开文件 - Exception in thread “main” java.sql.SQLException: Unable to open file 线程“主”java.sql.SQLException 中的异常:未找到适合 jdbc:ucanaccess://C:/Users/gfas1/acctop/. - Exception in thread “main” java.sql.SQLException: No suitable driver found for jdbc:ucanaccess://C:/Users/gfas1/Desktop/st.accdb 线程“ main”中的异常java.sql.SQLException:无效的名称模式:pakagename.sqlTypename - Exception in thread “main” java.sql.SQLException: invalid name pattern: pakagename.sqlTypename 线程“主”中的异常java.sql.SQLException:ORA-00955:现有对象已使用名称 - Exception in thread “main” java.sql.SQLException: ORA-00955: name is already used by an existing object 线程“main”java.sql.SQLException 中的异常:用户“@”localhost 的访问被拒绝(使用密码:否) - Exception in thread "main" java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO) 线程“ main”中的异常java.sql.SQLException:索引处缺少IN或OUT参数:25此错误是什么意思 - Exception in thread “main” java.sql.SQLException: Missing IN or OUT parameter at index:: 25 What was this error meant
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM