java连接Mysql8.0.11与之前版本有些不同。
其中driver为com.mysql.cj.jdbc.Driver
url为jdbc:mysql://localhost/studentuseSSL=FALSE&serverTimezone=UTC(student为数据库的名字)
代码如下:
package study;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test1 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/student?useSSL=FALSE&serverTimezone=UTC","root", "yc529529");
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select*from studinfo");
while (rs.next()) {
for (int i = 1; i <= 4; i++) {
System.out.print(rs.getString(i) + " ");
}
System.out.println();
}
}
}
数据库student中的studinfo表如下图所示:

本文介绍了如何使用Java连接MySQL 8.0.11数据库,特别指出driver应为com.mysql.cj.jdbc.Driver,url配置为jdbc:mysql://localhost/student?useSSL=FALSE&serverTimezone=UTC,其中'student'为数据库名。文章还提及了数据库中studinfo表的结构,并简单提到了下载jar包和运行结果的情况。
最低0.47元/天 解锁文章
6475

被折叠的 条评论
为什么被折叠?



