mysql-8.0 使用jdbc与java连接的那些坑

【注】本文章也算亲身实践,但是是踩着坑去的。然后看了一篇文章,得到一些解决办法。

以下部分代码和思路来源于此篇文章:mysql-8.0.13使用jdbc与java连接教程(亲测)

一、mysql版本

1.1、MySQL Community Server    社区版

(Current Generally Available Release: 8.0.17)  当前版本为8.0.17

MySQL Community Server is the world's most popular open source database. 是世上最流行的开源版本数据库。

下载地址:https://dev.mysql.com/downloads/mysql/

1.2、Jdbc版本

mysql-connector-java-8.0.17

下载地址:https://dev.mysql.com/downloads/connector/j/

下载的下方选择:PlatForm Independent (与平台无关),然后选择Platform Independent (Architecture Independent), ZIP Archive。

再新打开的页面中,点击:No thanks, just start my download. 直接下载

二、注意事项——我踩中的那些坑

2.1  版本匹配问题

原本我已经安装了mysql 8.0了,后来要引用jar包的时候,嫌弃打开mysql的官网太慢,自己去随便百度了个教程,就在某些网站直接下载了jdbc的jar包。当时下载的版本是:mysql-connector-java-5.1.39-bin.jar 。

系统会提示我Could not create connection to database server.

于是我重新去官网下载了最新的jdbc版本。

2.1  新版的变化

以前的版本,连接字符串"jdbc:mysql://localhost:3306/test"即可。“jdbc:mysql://{地址}:{端口号}/{数据库名}”

并且旧版的调用的是Class.forName("com.mysql.jdbc.Driver");

而新版,却需要在字符串中加上参数“?serverTimezone=UTC”,即:"jdbc:mysql://localhost:3306/test?serverTimezone=UTC"。

调用的是Class.forName("com.mysql.jc.jdbc.Driver");

否则将会报以下错误:

Exception in thread "main" 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.

三、代码示范

代码一:(新增,简单版)

package Insert;
import java.sql.*;


public class InsertTest {

	public static void main(String[] args) throws Exception {
		
		Class.forName("com.mysql.cj.jdbc.Driver");
		
		String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC";
		Connection conn = DriverManager.getConnection(url,"root","123456");
		Statement state = conn.createStatement();
		String sql="insert into test.person(PID,Name,Age) values(2,'Baobao',27)";
		state.execute(sql);
		
		conn.close();
		
	}

}

代码二:查询数据

package Select;

import java.sql.*;


public class SelectTest {

	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			System.out.println("加载数据库成功:");
		}catch(Exception ex){
			System.out.println("加载数据库失败:");
			System.out.println(ex.getMessage());
		}
		
		String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC";
		String userName = "lbc";
		String password ="123456";
		try {
			conn = DriverManager.getConnection(url,userName,password);
			System.out.println("连接成功");
			String sql = "select PID,Name,Age FROM test.person";
			ps= conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()) {
				int pid = rs.getInt("PID");
				String name = rs.getString("Name");
				int age = rs.getInt("Age");
				System.out.println("["+pid+"]"+"\t" + name+"\t"+ age);
			}
		}catch(Exception ex){
			System.out.println("数据库操作失败:");
			System.out.println(ex.getMessage());
		}

	}

}

代码二的运行结果:

以上,创建了一个名为test的数据库,新建有一个表Person(以下界面来自MySqL Workbench):

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值