简单回忆:
按照步骤来操写:
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获得连接
Connection conn = DriverManager.getConnetion("jdbc:mysql://127.0.0.1:3306/test","root","123");
//获得statement
PreparedStatement pstmt = conn.prepareStatement("select * from user");
//执行查询
ResultSet rs = pstmt.executeQuery();
//遍历结果集
while(rs.next()){
//封装实体
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
}
//释放资源
rs.close();
pstmt.close();
conn.close();