10 Replies - 11174 Views - Last Post: 15 July 2011 - 06:41 AM Rate Topic: -----

#1 newaudrey  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 162
  • Joined: 08-July 10

Error in my update statement

Posted 10 July 2011 - 09:53 PM

ConnectDB();
        /*"UPDATE table_1 SET name = '" & txtname.Text & "'," _
                    & "address = '" & txtaddress.Text & "'," _
                    & "age = '" & txtage.Text & "' " _
                    & "WHERE id = " & Ctrl & ""*/
        String q= "Update table2 set studname ='" + txtname.getText() "'," + "studaddress = '" + txtaddress.getText() + "'," + "studage='" + txtage.getText() + "'," + "studhabit='" + txthabit.getText() + "'," +  "WHERE studid = " + txtid.getText() + "";
                
        try{
            st = conn.createStatement();
            st.executeUpdate(q);
            st.close();
            conn.close();
            JOptionPane.showMessageDialog(null, "Updated");
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
        txtid.setText("");
        txtaddress.setText("");
        txtname.setText("");
        txthabit.setText("");
        txtage.setText("");


it says ';' expected

where will i put the ;

Is This A Good Question/Topic? 0
  • +

Replies To: Error in my update statement

#2 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Error in my update statement

Posted 10 July 2011 - 10:21 PM

Your biggest mistake is cobbling the statement together in this form. Use PreparedStatement (see the JDBC Tutorials) and then see what happens.
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Error in my update statement

Posted 11 July 2011 - 09:01 PM

Hav you tried to System.out.println(q) to see what it looks like

Also a lot of + for nothing
        String q= "Update table2 set studname ='" + txtname.getText() "'," + "studaddress = '" + txtaddress.getText() + "'," + "studage='" + txtage.getText() + "'," + "studhabit='" + txthabit.getText() + "'," +  "WHERE studid = " + txtid.getText() + "";


would be more readable that way

        String q= "Update table2 set studname ='" + txtname.getText() "', studaddress = '" + txtaddress.getText() + "', studage='" + txtage.getText() + "', studhabit='" + txthabit.getText() + "', WHERE studid = " + txtid.getText();



Seems you have a + missing after txtname.getText()
Seems you have an extra , before WHERE

But anyhow masijade is right, PreparedStatement is the way to go. You wont do type type of stupid mistakes.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12149
  • View blog
  • Posts: 45,175
  • Joined: 27-December 08

Re: Error in my update statement

Posted 12 July 2011 - 05:59 AM

View Postmasijade, on 11 July 2011 - 01:21 AM, said:

Your biggest mistake is cobbling the statement together in this form. Use PreparedStatement (see the JDBC Tutorials) and then see what happens.

I agree. Not only will PreparedStatement make it easier to organize the parameters, but it guards against SQL Injection attacks as well. Plus, PreparedStatements are cached on the database engine to help increase performance.
Was This Post Helpful? 1
  • +
  • -

#5 newaudrey  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 162
  • Joined: 08-July 10

Re: Error in my update statement

Posted 12 July 2011 - 10:32 PM

i got error again on my update statement this is my code for my update button

String sql= "Update table1 set studname ='" + txtname.getText() + "', studaddress = '" + txtaddress.getText() + "', studage='" + txtage.getText() + "',  WHERE studid ='" + txtid.getText();
        try {

              pst = conn.prepareStatement(sql);
              pst.executeUpdate();

            JOptionPane.showMessageDialog(null, "Updated");
            UpdateJTable();

            }
            catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
            }


studid is a text column in my access database

this is the error
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error in update statement.

This post has been edited by newaudrey: 12 July 2011 - 10:38 PM

Was This Post Helpful? 0
  • +
  • -

#6 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Error in my update statement

Posted 13 July 2011 - 12:49 AM

Okay. Go on ignoring our advice, I will now start ignoring this question.
Was This Post Helpful? 1
  • +
  • -

#7 newaudrey  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 162
  • Joined: 08-July 10

Re: Error in my update statement

Posted 13 July 2011 - 07:28 AM

sir masijade i dont understand what you mean. i tried to use preparedstatement. is there something wrong sir about how i use preparedstatement?
Was This Post Helpful? 0
  • +
  • -

#8 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Error in my update statement

Posted 13 July 2011 - 08:11 AM

View Postnewaudrey, on 13 July 2011 - 04:28 PM, said:

sir masijade i dont understand what you mean. i tried to use preparedstatement. is there something wrong sir about how i use preparedstatement?

Then you didn't bother to look at the tutorials or you wouldn't still be cobbling together your statement like this.
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12149
  • View blog
  • Posts: 45,175
  • Joined: 27-December 08

Re: Error in my update statement

Posted 13 July 2011 - 08:13 AM

Specifically, tutorials like this one: http://download.orac...s/prepared.html
Was This Post Helpful? 0
  • +
  • -

#10 newaudrey  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 162
  • Joined: 08-July 10

Re: Error in my update statement

Posted 14 July 2011 - 10:17 PM

sir pbl
sir macosxnerd101
sir masijade

thank you all sir for guiding me. it worked. thank you so much :gun_bandana: :bananaman:
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12149
  • View blog
  • Posts: 45,175
  • Joined: 27-December 08

Re: Error in my update statement

Posted 15 July 2011 - 06:41 AM

Glad we could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1