java连接Mysql8.0.11

java连接Mysql8.0.11与之前版本有些不同。

其中driver为com.mysql.cj.jdbc.Driver

url为jdbc:mysql://localhost/student?useSSL=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) + "\t");
			}
			System.out.println();
		}
	}
}

数据库student中的studinfo表如下图所示:


从官网下载jar包放到项目下就不再说了和之前一样。

运行结果如下:


你可能感兴趣的:(java连接Mysql8.0.11)