我的第一个JDBC程序(含重要纠错)

我的第一个JDBC程序(含重要纠错)

#日常分享所得,欢迎关注学习计算机、热爱计算机的我!后续更精彩#

在学习了MySQL数据库的基础知识之后,自然是要使用MySQL数据库来辅助我们的Java代码了。

步骤

  • 创建一个新的MySQL数据库,并创建表,录入一些数据;

  • 在IDEA中创建一个新的项目;

  • 准备好jdbc驱动,并导入驱动到IDEA的Java过程中;

代码模板

package com;
​
//这是我的第一个jdbc程序
​
import java.sql.*;
​
public class jdbcFirst {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //第一步:加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");     //抛出异常,写法是固定的
​
        //第二步:验证用户信息和url
        String url = "jdbc:mysql://localhost:3306/class?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC";
                //  以上的“class”为即将连接的数据库名称,
                //“useUnicode=true&characterEncoding=utf8&useSSL=true”分别为支持中文编码、定义编码类型和使用安全连接,可以理解为固定写法。
        String username = "root";     //数据库账户名
        String password = "123456";       //数据库登录密码
​
        //第三步:连接成功后获得数据库对象
        Connection connection = DriverManager.getConnection(url, username, password);//需要抛出异常,"connection"就可以代表数据库了
​
​
        //第四步:执行sql的对象
        Statement statement = connection.createStatement();     //statement来执行sql的对象
​
        //第五步:使用sql的对象去执行sql语句
        String sql = "SELECT * FROM `student`";
​
        ResultSet resultSet = statement.executeQuery(sql);      //执行完sql后返回一个结果集resultSet,这个结果集已经封装了全部查询出来的结果
​
        while (resultSet.next()){       //对结果进行打印
            System.out.println("id="+resultSet.getObject("id"));        //当不知道字段的类型时,可以使用.getObject
            System.out.println("name="+resultSet.getObject("name"));
            System.out.println("password="+resultSet.getObject("pwd"));
            System.out.println("sex="+resultSet.getObject("sex"));
            System.out.println("birthday="+resultSet.getObject("birthday"));
            System.out.println("address="+resultSet.getObject("address"));
            System.out.println("email="+resultSet.getObject("email"));
            System.out.println("gradeid="+resultSet.getObject("gradeid"));
        }
        //第六步:使用完毕,释放数据库的连接
        resultSet.close();
        statement.close();
        connection.close();
    }
}
​

纠错1

由于JDBC驱动的版本不同,在加载驱动阶段有一些不同的地方;

旧版:

 //第一步:加载驱动
       Class.forName("com.mysql.jdbc.Driver");

较新版:

 //第一步:加载驱动
       Class.forName("com.mysql.cj.jdbc.Driver");

在新版jdbc加载驱动阶段如果没有添加“.cj”,会有以下报错信息:

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.

  

从报错信息也可以看出应该做的修改。

纠错2

还是由于jdbc驱动的问题,如果遇到以下报错信息:

Exception in thread "main" java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. 

  

这是由于时区的错误导致的错误,修改一下代码即可:

String url = "jdbc:mysql://localhost:3306/class?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC";

  

关键在于这句代码:

serverTimezone=UTC

在输出数据数据时,必修要确保字段名的准确性!

原文地址:https://www.cnblogs.com/awong18/p/13198958.html