java连接MySQL
MySQL jdbc 6.0 版本以上serverTimezone参数
UTC代表的是全球标准时间
List<Student> students = new ArrayList<>();
Connection connection = null;
PreparedStatement preparedStatement =null;
ResultSet resultSet = null;
try {
String driverclass = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3308/javaweb?serverTimezone=UTC";
String user = "root";
String password = "root1234";
Class.forName(driverclass);
connection = DriverManager.getConnection(url, user, password);
String sql="SELECT flow_id, type, id_card, exam_card, student_name, location,grade FROM examstudent";
preparedStatement=connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
int flowId = resultSet.getInt(1);
int type = resultSet.getInt(2);
String idCard = resultSet.getString(3);
String examCard = resultSet.getString(4);
String studentName = resultSet.getString(5);
String location = resultSet.getString(6);
int grade = resultSet.getInt(7);
Student student = new Student(flowId, type, idCard, examCard, studentName, location, grade);
students.add(student);
}
} catch (Exception e) {
// TODO: handle exception
}finally {
try {
if(resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}