[英]Search from oracle database in Java using LIKE keyword
query="select * from books where BookName LIKE \"%" +txt1.getText()+"%\"";
這用於mysql服務器數據庫代碼。 甲骨文將會發生什么變化?
不要使用字符串連接來構建SQL查詢-您應該使用綁定參數。
您的查詢字符串應為:
query="select * from books where BookName LIKE ?";
然后您可以執行以下操作:
Class.forName( "oracle.jdbc.OracleDriver" ); // If you are using the Oracle driver.
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:XE",
"username",
"password"
);
final String query="select * from books where BookName LIKE ?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString( 1, "%" + txt1.getText() + "%" );
ResultSet rs = ps.executeQuery();
// Loop through the result set.
// Close statement/connections
(您將需要處理異常等)
和:
如果要以字符串形式編寫查詢,則在SQL中字符串文字會被單引號(而不是雙引號)包圍:
query="select * from books where BookName LIKE '%your_string%'";
並且您需要確保字符串中的任何單引號均已正確轉義(但只能使用bind參數)。
問題解決了..
query =“ select * from BookName LIKE'%” + txt1.getText()+“%'”;
感謝大家 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.