Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Java Swing textfield insert to mysql not working?

I am having a problem with my code

this line works

st.executeUpdate("insert WaterStation . test VALUES ("+3+","+"'test1'"+","+"'test2'"+");");

but if I replaced the line with this

st.executeUpdate("insert WaterStation . test VALUES ("+3+","+txtFName.getText()+","+txtLName.getText()+");");

this is what I get in stack trace

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'test1' in 'field list'

'test1' is the one that I wrote in JTextField txtFName

what is wrong with my syntax?

1 Answer

Relevance
  • 10 years ago
    Favorite Answer

    You need to include the single quote in your update.

    You wrote:

    st.executeUpdate("insert WaterStation . test VALUES ("+3+","+txtFName.getText()+","+txtLName…

    It should be

    st.executeUpdate("insert WaterStation . test VALUES ("+3+",'"+txtFName.getText()+"','"+txtLName…

    Notice the extra single quote marks.

Still have questions? Get your answers by asking now.