我正在尝试为 JDBC 中的语句设置字符串。但是我不知道如何为这个例子设置字符串:
public static final String SELECT_ST = "SELECT name, car FROM rating WHERE car= ?";
try (Connection connection = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWD);
PreparedStatement statement1 = connection.prepareStatement(INSERT_ST);
Statement statement2 = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
) {
try(ResultSet rs = statement2.executeQuery(SELECT_ST)){
//some code here
}
}catch (SQLException e) {
throw new ScoreException("Some problem here", e);
}
我如何设置字符串?在 SELECT_ST?
你要找的是 PreparedStatement#setString.
你的代码应该是这样的:
try (Connection connection = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWD);
PreparedStatement statement1 = connection.prepareStatement(INSERT_ST);
PreparedStatement statement2 = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
) {
statement2.setString(1, "BMW"); //Set your where condition
try(ResultSet rs = statement2.executeQuery(SELECT_ST)){
//some code here
}
}catch (SQLException e) {
throw new ScoreException("Some problem here", e);
}
不知道为什么你有 statement1 :)