image.pngpreparedStatement的查询操作

    1. @Test
    2. public void statementTest() throws Exception{
    3. InputStream resourceAsStream = PreparedStatementTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
    4. Properties properties = new Properties();
    5. properties.load(resourceAsStream);
    6. String url = properties.getProperty("url");
    7. String driver = properties.getProperty("driver");
    8. String user = properties.getProperty("user");
    9. String password = properties.getProperty("password");
    10. Class.forName(driver);
    11. Connection connection = DriverManager.getConnection(url, user, password);
    12. String sql = "select * from tb_city where id = ?";
    13. PreparedStatement preparedStatement = connection.prepareStatement(sql);
    14. preparedStatement.setInt(1,3);
    15. ResultSet resultSet = preparedStatement.executeQuery();
    16. while (resultSet.next()){
    17. int id = resultSet.getInt("id");
    18. String city_name = resultSet.getString("city_name");
    19. int country_id = resultSet.getInt("country_id");
    20. System.out.println("id:"+id+",city_name:"+city_name+",country_id:"+country_id);
    21. }
    22. resultSet.close();
    23. connection.close();
    24. }