mysql-8.0.13使用jdbc与java连接教程(亲测)

一、下载jdbc驱动

1. 进入mysql官网,依次进入如下页面

2. 往下滑会有一个MySQL Connectors

3.点击进入,然后选择java的驱动

 

4.此时你会看到让你选择当前的系统,我的是windows,但你会发现没有这个系统的,我当时就很纳闷,那么怎么在官网下载windows的mysql的jdbc驱动呢?且往下看

5.你会发现之所以没有windows操作系统,是因为官方把windows单独做成了一个网页,看到上图那个for windows吗,点进去会有windows的整套mysql文件,是一个msi后缀。但是,问题又来了,我只要jdbc驱动,mysql我已经装好了。。。

唉,都怪本人英语渣,看上图那个选择操作系统的,有个platform independent,即平台无关,点进去就有我们需要的jar包,下载下来并解压。然后复制mysql-connector-java-8.0.13.jar包到你的D:\Program Files\Java\jre1.8.0_181\lib\ext;按照你自己的java环境来。

二、测试连接数据库(server version:8.0.13 MySQL)

1. 先在你的mysql里新建个数据库,再建个表,再加点数据,我的如图所示:

2.我用的是eclipse开发工具,新建一个java project 。把如下代码键入

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class MysqlTest {
	public static void main(String []args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		 try {
	            // The newInstance() call is a work around for some
	            // broken Java implementations

	            Class.forName("com.mysql.cj.jdbc.Driver");
	            System.out.println("加载成功");
	        } catch (Exception ex) {
	        	System.out.println("加载失败");
	            // handle the error
	        }
		
		try {
		    conn =
		       DriverManager.getConnection("jdbc:mysql://localhost/student?"+"user=root&password=123456");

		   System.out.println("连接成功");
		   ps = conn.prepareStatement("select num,name from stu;");
		   rs = ps.executeQuery();
		   while(rs.next()) {
			   int num = rs.getInt("num");
			   String name = rs.getString("name");
			   System.out.print(num+"\t"+name);
			   System.out.println("");
		   }
		   

		   
		} catch (SQLException ex) {
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		    System.out.println("连接失败");
		}
	}
}

发生报错:
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.
SQLState: 01S00
VendorError: 0

原因分析:驱动加载成功,连接数据库失败。发生这个错误是因为时区问题,数据库的时区和我本地的时区不一样,我按照网上修改,修改一行:

DriverManager.getConnection("jdbc:mysql://localhost/student?serverTimezone=UTC"+"user=root&password=123456");

如下:

public class MysqlTest {
	public static void main(String []args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		 try {
	            // The newInstance() call is a work around for some
	            // broken Java implementations

	            Class.forName("com.mysql.cj.jdbc.Driver");
	            System.out.println("加载成功");
	        } catch (Exception ex) {
	        	System.out.println("加载失败");
	            // handle the error
	        }
		
		try {
		    conn =
		       DriverManager.getConnection("jdbc:mysql://localhost/student?serverTimezone=UTC"+"user=root&password=123456");

		   System.out.println("连接成功");
		   ps = conn.prepareStatement("select num,name from stu;");
		   rs = ps.executeQuery();
		   while(rs.next()) {
			   int num = rs.getInt("num");
			   String name = rs.getString("name");
			   System.out.print(num+"\t"+name);
			   System.out.println("");
		   }
		   

		   
		} catch (SQLException ex) {
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		    System.out.println("连接失败");
		}
	}
}

但是,又有错误,错误如下:

加载成功
SQLException: Access denied for user ''@'localhost' (using password: YES)
SQLState: 28000
VendorError: 1045
连接失败
 

分析:驱动加载成功,连接数据库失败。这个问题我在百度上查了好多,他们的解决办法都很复杂,而且有的是针对远端服务器上的数据库,我这是本地数据库,如何解决呢?

有时候不得不佩服歪果仁,办法简单明了,我把他们回答的那整个页面都截取下来,请看下图:

(https://stackoverflow.com/questions/23410439/access-denied-for-user-adminlocalhost-using-password-yes)

 

 

 

 

 

我也没仔细看,反正我把连接数据库的方式修改了一下,如下:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class MysqlTest {
	public static void main(String []args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		 try {
	            // The newInstance() call is a work around for some
	            // broken Java implementations

	            Class.forName("com.mysql.cj.jdbc.Driver");
	            System.out.println("加载成功");
	        } catch (Exception ex) {
	        	System.out.println("加载失败");
	            // handle the error
	        }
		
		try {
		    conn =
		       DriverManager.getConnection("jdbc:mysql://localhost/student?serverTimezone=UTC","root","123456");

		   System.out.println("连接成功");
		   ps = conn.prepareStatement("select num,name from stu;");
		   rs = ps.executeQuery();
		   while(rs.next()) {
			   int num = rs.getInt("num");
			   String name = rs.getString("name");
			   System.out.print(num+"\t"+name);
			   System.out.println("");
		   }
		   

		   
		} catch (SQLException ex) {
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		    System.out.println("连接失败");
		}
	}
}

这两种连接方式,我以前用sqlserver的时候都用过,没有出现过问题。而且我也不是很清楚这两种连接方式有什么区别,待解决!

以上就是在eclipse下使用jdbc连接MySQL的方法,你懂了吗?

 


2019.07.14 更新

篇外

       以前我见过这种com.mysql.jdbc.Driver的驱动,但是上面用的是 com.mysql.cj.jdbc.Driver这个驱动。

在使用com.mysql.cj.jdbc.Driver时需要额外增加一个时区的参数。设定serverTimezone=UTC,会比中国时间早8个小时,如果在中国,可以选择Asia/Shanghai或者Asia/Hongkong。

这是之前的错误尝试:https://blog.csdn.net/qq_36923376/article/details/84327174

       顺便说下,新版本的MySQL要使用新版本的驱动,老版本MySQL就使用老版本的驱动,这样可以避免不必要的问题。

  • 28
    点赞
  • 118
    收藏
    觉得还不错? 一键收藏
  • 16
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值