Java通过mysql-connector-java-8.0.11连接MySQL

  • driver = “com.mysql.cj.jdbc.Driver”;
  • url=”jdbc:mysql://127.0.0.1:3306/Users?useSSL=true&serverTimezone=GMT”;
  • Mysql的ResultSet 索引是从1开始的

Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/Users?useSSL=true&serverTimezone=GMT", "root", "root");
PreparedStatement ps = conn.prepareStatement("select * from t_users");
ResultSet rs = ps.executeQuery();
while(rs.next())
{
User user = new User();
user.setId(rs.getInt(1));
user.setUuid(rs.getString(2));
user.setName(rs.getString(3));
user.setPassword(rs.getString(4));
System.out.println(user);
}
rs.close();
ps.close();
conn.close();

你可能感兴趣的:(MySQL)