我们可以使用JDBC查询来执行select语句。
try(Connection conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD)){
try(Statement stmt = conn.getStatement()){
try(ResultSet rs = stmt.executeQuery("select * from students where gender=\'M\';")){
while(rs.next()){
long id = rs.getLong(1); //注意:索引从1开始
long classId = rs.getLong(2);
String name = rs.getString(3);
String gender = rs.getString(4);
}
}
}
}