Shahzaib
Shahzaib

Reputation: 127

Using value from a JTextBox in SELECT query in mysql

I have a textbox. When the user enters the name in the textbox I want the details fetched from the table

String getTxt = text.getText();
ResultSet rs=st.executeQuery("SELECT * FROM authors_4 WHERE self_authors="+getTxt);

On executing this i'm getting exception

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server

What is the solution to this. Need help

Upvotes: 0

Views: 48

Answers (1)

Reimeus
Reimeus

Reputation: 159754

You're missing single quotes:

st.executeQuery("SELECT * FROM authors_4 WHERE self_authors='" + getTxt + "'");

Better use a PreparedStatement to protect against SQL Injection attacks.

Upvotes: 3

Related Questions