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环境来。
二、测试连接数据库
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
连接失败
分析:驱动加载成功,连接数据库失败。这个问题我在百度上查了好多,他们的解决办法都很复杂,而且有的是针对远端服务器上的数据库,我这是本地数据库,如何解决呢?
有时候不得不佩服歪果仁,办法简单明了,我把他们回答的那整个页面都截取下来,请看下图:
我也没仔细看,反正我把连接数据库的方式修改了一下,如下:
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的方法,你懂了吗?