我有一个文本框。当用户在文本框中输入名称时,我希望从表中获取详细信息
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 Injection攻击。关于java - 在SELECT查询中使用JTextBox中的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15685027/