preparedStatement的查询操作
@Test
public void statementTest() throws Exception{
InputStream resourceAsStream = PreparedStatementTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(resourceAsStream);
String url = properties.getProperty("url");
String driver = properties.getProperty("driver");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
String sql = "select * from tb_city where id = ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,3);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
int id = resultSet.getInt("id");
String city_name = resultSet.getString("city_name");
int country_id = resultSet.getInt("country_id");
System.out.println("id:"+id+",city_name:"+city_name+",country_id:"+country_id);
}
resultSet.close();
connection.close();
}