Inserting single quote in JDBC for SQL Query not working

ぐ巨炮叔叔 提交于 2021-02-05 12:01:30

问题


I'm having issues dealing with the single quote while using it in a prepared statement in JAVA via Oracle JDBC.

Let's say we have a table Restaurant with a column restaurant_name with 1 value : Jack's Deli

I want to use a simple prepared statement query like this:

    String result = "Jack\'\'s Deli"
    String sqlStatement = "select * from Restaurant where restauraunt_name like ? escape '\\' ";
    PreparedStatement pStmt = conn.prepareStatement(sqlStatement);
    pstmt.setString(1, result);

The result shows 0 returned values, however when I directly search the query in the database (ORACLE) it works fine and retrieves the result. (Oracle uses two single quotes as an escape for the first)

I am thinking that the value is not being passed properly to the database. Or there is some other formatting issue.


回答1:


The point of prepared statements is that you don't need any escaping.

.setString(1, "Jack's Deli") will get it done.



来源:https://stackoverflow.com/questions/60805912/inserting-single-quote-in-jdbc-for-sql-query-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!