[英]Using value from a JTextBox in SELECT query in mysql
我有一個文本框。 當用戶在文本框中輸入名稱時,我希望從表中獲取詳細信息
String getTxt = text.getText();
ResultSet rs=st.executeQuery("SELECT * FROM authors_4 WHERE self_authors="+getTxt);
在執行此操作時,我遇到異常
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
有什么解決方案。 需要幫忙
您缺少單引號:
st.executeQuery("SELECT * FROM authors_4 WHERE self_authors='" + getTxt + "'");
更好地使用PreparedStatement
來防止SQL注入攻擊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.