I'm trying to make a search with PreparedStatement, but the query returns no result:
final Session session = em.unwrap(Session.class);
final String sql = "SELECT u.name FROM Users AS u WHERE u.name LIKE ?";
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
try (PreparedStatement stmt = connection.prepareStatement(sql))
{
stmt.setString(1, "%" + nameTerm + "%");
stmt.executeQuery();
}
}
});
There are records in the DB, it should return a result. I test my query in iSeries Navigator and it works. But if I executed it through PreparedStatement it returns no result. I'm using DB2 database.
EDIT
I forgot to mention that this problem happens only if the search term contains apostrophe.
pstmt.executeUpdate()
executes prepared statement but I don't find it in your code.
you have to call executeUpdate()
api to execute your prepared statement.
Try
try (PreparedStatement stmt = connection.prepareStatement(sql)){
stmt.setString(1, "%" + nameTerm + "%");
stmt.executeUpdate();
}
You are only setting the PreparedStatement
, you are not actually calling the executeUpdate()
method, which makes the call out to the database. Check out this PreparedStatement Tutorial
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.