jdbc操作mysql8.0的坑
package com.vrv.test;
import java.sql.*;
public class JdbcTest {
// 定义sql
private static String sql = "SELECT stu_name FROM t_login ";
public static void main(String[] args) throws SQLException {
// 连接对象
Connection connection = null;
// 预编译对象 PreparedStatement
PreparedStatement preparedStatement = null;
// 结果集
ResultSet resultSet = null;
try {
// 加载驱动
// Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.cj.jdbc.Driver");
// 获取连接对象
connection = (Connection) DriverManager.getConnection("jdbc:mysql://192.168.220.139:3306/teaching_manage?useSSL=false&serverTimezone=Asia/Shanghai", "root", "123456");
// 获取preparedStatement
preparedStatement = (PreparedStatement) connection.prepareStatement(sql);
//
执行sql语句,并返回结果集到resultSet中
resultSet = preparedStatement.executeQuery();
遍历结果集
while (resultSet.next()) {
String name = resultSet.getString(1);
System.out.println(name);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("执行结束");
}
}
}