0

I'm trying to use JTextField to update content of my DB, I have a frame where is all information about specific record, then I input correct or changed data to all JTextFields, and then I try to execute UPDATE statement with preparedStatement, but it doesn't seem to work. I'm trying this way:

.....
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String Base = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)}; DBQ=DATABASE.mdb";
Connection con = DriverManager.getConnection(Base,"","");
PreparedStatement pst = con.prepareStatement("update table_name SET table_item1='?', table_item2='?', table_item3='?', table_item4='?', table_item5=?, table_item6=?, table_item7=? WHERE table_item8=?");
pst.setString(1, tf1.getText());
pst.setString(2, tf2.getText());
pst.setString(3, tf3.getText());
pst.setString(4, tf4.getText());
pst.setString(5, tf5.getText());
pst.setString(6, tf6.getText());
pst.setString(7, tf7.getText());
pst.setString(8, comboItem.getSelectedItem());
int res= pst.excecuteUpdate();
pst.close();
con.close();
......

In the query I use ' ' because it is a String component, I get error at pst.setString(5, tf5.getText()); maybe because it's Integer values starts there, thats just a hunch.

1 Answer 1

2

No need to use single quotes (') in prepared statement for string fields and correct 'update' query :

PreparedStatement pst = con.prepareStatement("update table table_name SET table_item1='?'

remove '' from all '?' like this for all string fields :

PreparedStatement pst = con.prepareStatement("update table table_name SET table_item1=?, 

So correct query becomes :

PreparedStatement pst = con.prepareStatement("update table table_name SET table_item1=?, table_item2=?, table_item3=?, table_item4=?, table_item5=?, table_item6=?, table_item7=? WHERE table_item8=?");
Sign up to request clarification or add additional context in comments.

8 Comments

[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'Pa_RaM000'. at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source) at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source) at sun.jdbc.odbc.JdbcOdbc.SQLExecute(Unknown Source) at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(Unknown Source) at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeUpdate(Unknown Source)
hey correct query is like 'update table table_name set...' correct it
and still new error apears [Microsoft][ODBC Microsoft Access Driver] Syntax error in UPDATE statement.
[Microsoft][ODBC Microsoft Access Driver] Syntax error in UPDATE statement. at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source) at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source) at sun.jdbc.odbc.JdbcOdbc.SQLExecute(Unknown Source) at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(Unknown Source) at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeUpdate(Unknown Source)
Show me errors and by the way you have to copy just the last query in your code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.