eclipse链接MYSQL

eclipse版本为最新版 MYSQL为5.7版 JDBC如图所示:

  

新建Java项目并导入JDBC包,之后验证:(数据库为test 表为user 字段为name password)

package my;

import java.sql.*;

public class Demo {

// JDBC 驱动名及数据库 URL
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/test
// 数据库的用户名与密码,需要根据自己的设置
static final String USER = "root";
static final String PASS = "123456 ";

public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
// 注册 JDBC 驱动
Class.forName("com.mysql.cj.jdbc.Driver");

// 打开链接
System.out.println("连接数据库...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);

// 执行查询
System.out.println(" 实例化Statement对象...");
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM user";
ResultSet rs = stmt.executeQuery(sql);

// 展开结果集数据库
while(rs.next()){
// 通过字段检索
// int id = rs.getInt("id");
String name = rs.getString("name");
String password = rs.getString("password");

// 输出数据
//System.out.print("ID: " + id);
System.out.print(" 姓名: " + name);
System.out.print("密码: " + password);
System.out.print("\n");
}
// 完成后关闭
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
// 处理 JDBC 错误
se.printStackTrace();
}catch(Exception e){
// 处理 Class.forName 错误
e.printStackTrace();
}finally{
// 关闭资源
try{
if(stmt!=null) stmt.close();
}catch(SQLException se2){
}// 什么都不做
try{
if(conn!=null) conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}

运行会出现如下错误:

Wed Apr 25 00:02:22 CST 2018 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.
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.

大概翻译如下:

不建议建立没有服务器身份验证的SSL连接。根据MySQL 5.5.45 +、5.626+和5.7.6+的要求,如果没有设置显式选项,默认情况下必须建立SSL连接。对于不使用SSL的现有应用程序,ValuyServer证书属性设置为“false”。您需要通过设置USESL= false来显式禁用SSL,或者设置USELS=真,并为服务器证书验证提供信任存储。

无法识别或代表一个以上的时区。如果要利用时区支持,必须配置服务器或JDBC驱动程序(通过Server TimeStand配置属性)使用更具体的时区值

所以为了配合他们的要求,且不为我学习jdbc添加困难,我们选择禁用:

您需要通过设置useSSL = false显式禁用SSL,或者设置useSSL = true并为服务器证书验证提供信任库。

所以,在注册驱动,填写url地址的时候,在地址后面加上“?useSSL = false”

时区我们需要在后面加上参数?serverTimezone=utc

UTC代表的是全球标准时间 ,但是我们使用的时间是北京时区也就是东八区,领先UTC八个小时。

UTC + (+0800) = 本地(北京)时间

url的时区使用中国标准时间。也是就serverTimezone=Asia/Shanghai

最终:

// JDBC 驱动名及数据库 URL
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai";

运行:

猜你喜欢

转载自www.cnblogs.com/yf0826/p/8934660.html