Pix*_*els 2 java prepared-statement
我有以下代码
public void savePosition(String positionName) {
String sqlStatement = "INSERT INTO positions (name) VALUES (?)";
try (
Connection connection = getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sqlStatement);
preparedStatement.setString(1, positionName);
preparedStatement.executeUpdate();
){
} catch (SQLException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
并且附近有语法错误; .在setString和executeUpdate行中.
对于这一行
preparedStatement.setString(1, positionName);
Run Code Online (Sandbox Code Playgroud)
我有
令牌".",@ expected上的语法错误
令牌","的语法错误,.预期
令牌";"上的语法错误,删除此令牌
我看不出它有什么问题.
您已将try语句的整个主体放入仅用于初始化可关闭资源的部分.你要:
// This part is initializing resources
try (Connection connection = getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
// This part is just statements
preparedStatement.setString(1, positionName);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)