点击查看代码
public class TestQuery {
public static void main(String[] args) {
// 1.找驱动
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// 2.建链接
// 3.建通道
// 4.执行SQL并且返回结果
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Student> list = new ArrayList<>();
Student stu = null;
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/new","root","123456");
ps = con.prepareStatement("select * from student");
rs = ps.executeQuery();
while(rs.next()){
stu = new Student();
stu.setId(rs.getInt("id"));
stu.setStuNum(rs.getInt("stunum"));
stu.setName(rs.getString("name"));
stu.setScore(rs.getDouble("score"));
list.add(stu);
}
ResultSetMetaData rsmd = rs.getMetaData();
} catch (SQLException e) {
e.printStackTrace();
}
for (Student st : list) {
System.out.println(st);
}
}
}