Java连接MySQL简单步骤及常见问题解析

这篇文章跟读者们介绍Java语言如何连接到MySQL数据库相关操作,及FAQ(Frequently Asked Questions)介绍。

选择工具和准备数据集

IDE:Eclipse

数据库:使用的是MySQL 8.0

驱动程序包:mysql-connector-java-8.0.12.jar

驱动程序类名:com.mysql.cj.jdbc.Driver

提前准备工作:笔者使用MySQL Workbench 工具来进行数据库CRUD各种操作,在数据库中建立了一个test数据库,接着在test数据库中建立teacher表格和往向格中增加数据。

并且已经往表中插入了6项数据。

扫描二维码关注公众号,回复: 6116756 查看本文章

在Eclipse中建立一个数据库操作的Java project。并且在project里面引入(配置)驱动程序jar包

主要步骤

1.导入所需要的包

2.注册JDBC驱动

3.建立连接到数据库

 

4.执行 CRUD 操作

5.处理得到的结果

6.关闭资源,释放连接

提供源代码参考:

 1 //STEP 1:导入包
 2 //大多数情况下,使用import java.sql.*;就足够了。
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 import java.sql.Statement;
 8 
 9 public class DBConnettion {
10 
11     private static final String JDBC_DEIVER = "com.mysql.cj.jdbc.Driver";
12     //笔者查看本机数据库进程,默认使用3306端口,大家根据自己数据库进程端口号来相应修改。
13     private static final String DB_URL = "jdbc:mysql://localhost:3306/test?useSSL=false";//?useUnicode=true&characterEncoding=utf8&useSSL=false
14     private static final String USER = "****";//"****"替换成自己的数据库用户名
15     private static final String PASS = "****";//"****"替换成自己的数据库密码
16 
17     public static void main(String[] args) {
18         Connection conn = null;
19         Statement stmt = null;
20         
21         try {
22             //STEP 2:注册 JDBC 驱动
23             Class.forName(JDBC_DEIVER);
24             
25             //STEP 3:建立连接到数据库
26             System.out.println("正在连接数据库......");
27             conn = DriverManager.getConnection(DB_URL, USER, PASS);
28             stmt = conn.createStatement();
29             
30             //STEP 4:执行 CRUD 操作
31             System.out.println("正在查询数据:");
32             String sql = "select * from teacher;";
33             ResultSet rs = stmt.executeQuery(sql);
34             
35             //STEP 5:处理得到结果
36             System.out.println("Department   ID     Name");
37             while (rs.next()) {
38                 int id = rs.getInt("T_id");
39                 String dept_name = rs.getString("dept_name");
40                 String name = rs.getString("name");
41                 if (!rs.isLast())
42                     System.out.println(dept_name + "\t    " + id + "   " + name);
43                 else
44                     System.out.println(dept_name + "   " + id + "   " + name);
45             }
46             
47             //STEP 6:关闭资源,释放连接。
48             rs.close();
49             stmt.close();
50             conn.close();
51         } catch (ClassNotFoundException e) {
52             e.printStackTrace();
53         } catch (SQLException e) {
54             e.printStackTrace();
55         } finally {
56             try {
57                 if (stmt != null)
58                     stmt.close();
59                 if (conn != null)
60                     conn.close();
61             } catch (SQLException e) {
62                 e.printStackTrace();
63             }
64         }
65         System.out.println("释放数据库连接......");
66     }
67 }

运行结果:

常见问题解析及解决方法

出现问题:

java.sql.SQLException: The server time zone value '???ú±ê×??±??' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

解析:

从错误提示可知道是时区的错误,因此只要将数据库系统的时区设置为你当前系统时区即可。

解决方法:

因此使用root用户登录mysql,按照如下图所示操作即可。

查看数MySQL据库系统的时区设置

show variables like '%time_zone%';

笔者电脑系统时区设置为GMT+8:北京时间。而数据库系统时区默认为GMT+0(格林尼治时间),因此修改数据库系统时区跟我的系统时区相互一致。

set global time_zone = '+8:00';

对数据库系统时区变量做修改,不会立即发生改变。退出本次黑窗命令行(客户端)后

重新登录数据库,再次进行数据库系统时区查询,发现已经做出更改。

show variables like '%time_zone%';

 

出现问题:

Sat May 04 19:01:08 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

解析:

从MySQL数据库 5.5.45+ 以后,对数据库的访问都要求建立SSL加密连接。我们只是做一个简单的数据库访问实例,因此访问数据库不进行SSL加密连接。

解决方法:

可以在数据库URL连接语句末尾加上"?useSSL=false"来取消数据库的警告。

或者可以给数据库服务器提供证书验证的信任库,进行SSL加密连接。

猜你喜欢

转载自www.cnblogs.com/Lints/p/10809235.html