JDBC的两个小异常处理

JDBC中遇到的一些小问题

今天在学习JDBC连接数据库时,学完了六个步骤以后,自己学着把JDBC的功能做了一个封装,封装类代码如下:


/*  JDBC编程六步:
 *      1.注册驱动
 *      2.获取连接
 *      3.获取数据库操作对象
 *      4.执行SQL语句
 *      5处理查询结果集
 *      6.释放资源
 * */

import java.sql.*;



/**
 * @author zyx
 */
public class JdbcDemo {
    /**
     * 创建连接对象
     */
    private Connection connection = null;

    /**
     * 创建操作对象
     */
    private Statement statement = null;

    /**
     * 创建查询结果集
     */
    private ResultSet resultSet = null;

    /**
     * 数据库url
     */
    private static final String URL = "jdbc:mysql://192.168.3.23:3306/mysqlTest";

    /**
     * 数据库用户名
     */
    private static final String USER = "root";

    /**
     * 数据库密码
     */
    private static final String PASSWORD = "010425";

    public JdbcDemo() {
        try {
            /*注册驱动*/
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            /*获取连接*/
            connection = DriverManager.getConnection(URL,USER,PASSWORD);
            /*获取操作对象*/
            statement = connection.createStatement();

        }catch(SQLException e){
            e.printStackTrace();
        }
    }

    public void dmlExecutor(String dml){
        try {
            statement.executeUpdate(dml);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public ResultSet dqlExecutor(String dql){
        try{
            resultSet = statement.executeQuery(dql);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return resultSet;
    }

    public void close(){
        try{
            if (resultSet != null){
                resultSet.close();
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        try{
            if (statement != null){
                statement.close();
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        try{
            if (connection != null){
                connection.close();
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
}

但是在测试类中运行时,报出了一大串异常,异常代码如下:
`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. java.sql.SQLException: null, message from server: "Host 'LAPTOP-VUVIRV68' is not allowed to connect to this MySQL server" at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:833) at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:453) at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246) at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677) at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228) at jdbc.JdbcDemo.<init>(JdbcDemo.java:55) at jdbc.JdbcTest.main(JdbcTest.java:8) Exception in thread "main" java.lang.NullPointerException at jdbc.JdbcDemo.dmlExecutor(JdbcDemo.java:66) at jdbc.JdbcTest.main(JdbcTest.java:9) `
下面分开看每一行异常:

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.

大概意思是,加载com.mysql.jdbc.Driver这一个类是不被允许的,新的驱动类为com.mysql.cj.jdbc.Driver,那我们就把注册驱动那里实例化的类改为它要求的类,如下:
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
再运行一次,第一行的异常提示就消失了,这说明现在我们连接用的应该是com.mysql.cj.jdbc.Driver这一个类,为了找出异常的原因,我去翻看了旧版的Driver类的源码,简化后如下:

package com.mysql.jdbc;

import java.sql.SQLException;

/**
* Backwards compatibility to support apps that call <code>Class.forName("com.mysql.jdbc.Driver");</code>.
*/
public class Driver extends com.mysql.cj.jdbc.Driver {
   public Driver() throws SQLException {
       super();
   }

   static {
       System.err.println("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.");
   }
}

这个类继承了com.mysql.cj.jdbc.Driver这个类,除此之外,没有其他的成员方法,也就是说,这两个类在所有的方法上没有不同,并且可以看见在类的前面有一行注释,翻译过来的意思是向后兼容以支持调用Class.forName,也就是说,这个类是给较旧版本的jdk使用的,我使用的jdk11已经不支持使用这一个类了,所以,以后使用的就应该是这个cj包下的Driver类来注册驱动。

但是运行程序后,剩下的异常依然还没有被解决,那我们继续分析异常代码
java.sql.SQLException: null, message from server: "Host 'LAPTOP-VUVIRV68' is not allowed to connect to this MySQL server"
很显然,这里说连接不被允许,也就是说这个程序没有权限去访问MySQL的数据库,那我们就想想办法让它拥有访问权限,那么就是让MySQL的数据库变为公有的数据库。首先我们先登录MySQL(这里我用的是DOS)。在敲入命令show databases;后就可以看见一堆数据库罗列了出来,找到mysql数据库并敲入use mysql,看见Database change就说明现在已经再调用mysql数据库了,找到user这张表,然后再敲入update user set host = '%' where = 'root';然后它会弹出Query OK, 1 row affected的字样,最后需要保存变动,敲入flush privileges;,弹出Query OK, 0 rows affected字样,就可以了。
我们先看一遍我们连接的数据库中的表格是否有数据。
运行程序前
运行程序前
然后我们再运行一次程序
运行结果
运行程序结果
没有报错了,我们再看一次数据库数据是否有改变
运行程序后
运行程序后
数据成功插入到数据库中

猜你喜欢

转载自blog.csdn.net/m0_54783418/article/details/115437789