public static void main(String[] args) throws SQLException {
String url = "jdbc:mysql://127.0.0.1:3306/student";
String userName = "root";
String password = "11111";
String sql = "select id,name,age from user;";
// 1.建立连接
Connection connection = DriverManager.getConnection(url, userName, password);
// 2.具体操作
// 2.1 预编译 SQL
PreparedStatement statement = connection.prepareStatement(sql);
// 2.2 执行查询
ResultSet resultSet = statement.executeQuery();
// 2.3 处理结果集
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
System.out.println("id = " + id + ", name=" + name + ", age=" + age);
}
// 3.资源关闭
resultSet.close();
statement.close();
connection.close();
}