jdbc连接mysql8.0实例及问题

news/2024/5/18 23:11:30 标签: jdbc

当我们使用之前的方法通过jdbc连接mysql8.0版本时,会出现如下问题提示:

Wed Aug 29 15:38:16 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.
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:127)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:87)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:61)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:71)
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
	at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862)
	at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
	at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
	at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
	at java.sql.DriverManager.getConnection(Unknown Source)
	at java.sql.DriverManager.getConnection(Unknown Source)
	at com.brz.jdbc.demo1.JDBCUtils.main(JDBCUtils.java:29)
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: 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.
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)
	at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:83)
	at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:128)
	at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2201)
	at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2225)
	at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1391)
	at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:993)
	at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:852)
	... 6 more
Exception in thread "main" java.lang.NullPointerException
	at com.brz.jdbc.demo1.JDBCUtils.main(JDBCUtils.java:39)

此时,我们只需要修改两处即可解决问题:

1.在URL中加上时区信息:serverTimezone=GMT即可解决问题,如果需要使用gmt+8时区,需要写成GMT%2B8

显示SSL连接,即:useSSL=false 。

private static final String URL = "jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";

2.修改新版本的驱动包类名:

Class.forName("com.mysql.cj.jdbc.Driver");

注:mysql的连接驱动下载地址:http://mvnrepository.com/artifact/mysql/mysql-connector-java

 

实例:


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 使用JDBC访问数据库
 * @author brz
 *
 */
public class JDBCUtils {
	private static final String USER = "root";
	private static final String PASSWD = "123456";
	private static final String URL = "jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
	
	public static void main(String[] args) {
		//  1、注册数据库的驱动,使用反射注册驱动
        try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			System.out.println("注册驱动成功");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        Connection connection = null;
         //2、获得数据库的连接访问权限
        try {
			connection = DriverManager.getConnection(URL, USER, PASSWD);
			System.out.println("连接数据库成功");
        } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         //3、向数据库发送执行的SQL执行语句
        Statement statement = null;
        
        try {
			statement = connection.createStatement();
			//编写SQL语句
			String sql = "delete from student where sid = 1";
			int row_count =statement.executeUpdate(sql);
			if (row_count>0) {
				System.out.println("删除数据库成功");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			
			
		}
        
         //4、获取执行的返回结果
        
         //5、关闭数据库连接
		 try {
			statement.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

}

 


http://www.niftyadmin.cn/n/1519307.html

相关文章

JKI State Machine

LabVIEW中的JKI State Machine是一种简单易用、功能强大的状态机模板。JKI State Machine是由JKI维护的一个开源项目。 安装方式&#xff1a; 通过VI Package Manager下载安装即可 如何使用&#xff1a; 打开LabVIEW&#xff0c;在程序框图上点右键->函数选板->JKI To…

Raspberry PI 3静态IP配置

之前在Digilent社区share的一个帖子&#xff0c;贴到这里仅供大家参考。 好像是因为Rasbian新系统的原因&#xff0c;网上现在能搜到的配置树莓派静态IP的文章99.9%都一样&#xff0c;都不能用。 按照原来的教程&#xff0c;输入sudo nano /etc/network/interfaces后&#xff…

信号发生器输出幅值与输出阻抗的关系

输出负载 以Aglient33500B信号发生器为例&#xff1a;他的输出阻抗固定位50欧姆 输出负载(Output Channel Load)改变不改变输出阻抗和实际输出电压&#xff0c;只是改变显示的数字。 屏幕显示的是在给定的“输出负载”下在输出端口测量的电压值。 集总电路&#xff08;类似接…

数据的完整性

数据的完整性 作用&#xff1a;保证用户输入的数据保存到数据库中是正确的。 确保数据完整性 在创建表时给表中添加约束 数据的完整性是指数据的可靠性和准确性&#xff0c;数据完整性类型一般分三种&#xff1a; 1.实体完整性&#xff1a;通过唯一约束&#xff0c;主键约…

R软件的下载及安装

1. R的下载 1.1打开浏览器&#xff0c;输入网址&#xff1a;https://www.r-project.org/&#xff0c;点击download R。 1.2找到China分类&#xff0c;点击选择清华大学的镜像地址进入&#xff1a; 1.3点击Download R for Windows进行下载。 1.4点击install R for the first…

LabVIEW生成任意周期大小的波形

LabVIEW可以方便的生成我们所需要的基本函数波形和任意波形&#xff0c;本贴主要介绍一下使用LabVIEW生成非整数周期大小的基本函数波形。 以生成正弦波为例&#xff0c;直接贴程序框图&#xff0c;此处使用了Sine Wave . vi这个函数 函数各参数请自行查看LabVIEW帮助&#xf…

ResultSet

ResultSet 的功能 java.sql.ResultSet接口表示一个数据库查询的结果集。一个ResultSet对象具有一个游标指向当前行的结果集。 最初&#xff0c;光标被置于第一行之前。调用 next() 方法将光标移动到下一行&#xff1b;因为该方法在 ResultSet 对象没有下一行时返回 false&…

LabVIEW生成任意波形

参考之前写的一篇Blog&#xff1a;http://blog.csdn.net/u011973222/article/details/72852471 这次来生成一个任意波形&#xff1a;2.5周期个正弦波&#xff0c;2.25周期三角波和2周期方波 最后效果如图&#xff1a;