import java.awt.*;
import java.awt.Event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.*;
public class UpdateRecord implements ActionListener {
    private ScrollingPanel fields;
    private JTextArea output;
    private Connection connection;
 
    public UpdateRecord(Connection c, ScrollingPanel f, JTextArea o) {
        connection = c;
        fields = f;
        output = o;
 
    }
 
    public void actionPerformed(ActionEvent e)
    {
        try{
            Statement statement = connection.createStatement();
 
        if (!fields.id.getText().equals(""))
        {    
                  String query = "UPDATE Grades SET  " +
                    "FirstName='" + fields.first.getText() +
                    "', LastName = '" + fields.last.getText() +
                    "', Quiz1 = '" + fields.Quiz1.getText() +
                    "', Quiz2 = '" + fields.Quiz2.getText() +
                    "', Quiz3 = '" + fields.Quiz3.getText() +
                    "', MP1 = '" + fields.MP1.getText() +
                    "', MP2 = '" + fields.MP2.getText()+     
                    "', FE = '" + fields.FE.getText() +
                    "' WHERE id=" + fields.id.getText();
 
                    output.append("\nSending Query: " +
                    connection.nativeSQL(query) + "\n" );
 
            int result = statement.executeUpdate(query);
 
            if(result == 1)
                output.append("\nUpdate Succesful\n");
            else{
                output.append("\nUpdate Failed\n");
                    fields.first.setText("");
                    fields.last.setText("");
                    fields.Quiz1.setText("");
                    fields.Quiz2.setText("");
                    fields.Quiz3.setText("");
                    fields.MP1.setText("");
                    fields.MP2.setText("");
                    fields.FE.setText("");
                }
                    statement.close();                
            }
        else
            output.append("\nYou may only update an " +
                          "existing record. Use Find to " +
                          "locate the record, then " +
                          "modify the information and " +
                          "press Update. \n");
    }
    catch (SQLException sqlex) {
        sqlex.printStackTrace();
        output.append(sqlex.toString());
    }
  }   
}
What's The Problem In This Syntax... Everytime I'm Always Trying To Update A Record There Is A Message Saying: "Update Syntax Error"... What Will I Do...
 
Please Help Me Tnx...

Recommended Answers

All 2 Replies

which indicates that the SQL you're sending to the server is incorrect.
That's hardly surprising given the way you're working.
You have PreparedStatement to send parameterised queries, use it.

If it still doesn't work, use a database client and type in the query, see what it does.
It's however a weird driver that doesn't provide more information on an error when able than what you're getting, though if the SQL is malformed it (as I suspect) may not be able to.

String query = "UPDATE Grades SET  " +
 "FirstName='" + fields.first.getText() +
 "', LastName = '" + fields.last.getText() +
 "', Quiz1 = '" + fields.Quiz1.getText() +
 "', Quiz2 = '" + fields.Quiz2.getText() +
 "', Quiz3 = '" + fields.Quiz3.getText() +
 "', MP1 = '" + fields.MP1.getText() +
 "', MP2 = '" + fields.MP2.getText()+
 "', FE = '" + fields.FE.getText() +
 "' WHERE id=" + fields.id.getText();

Considering that the "id" is a string, you are missing a single quote around it. The query should look like:

String query = "UPDATE Grades SET  " +
 "FirstName='" + fields.first.getText() +
 "', LastName = '" + fields.last.getText() +
 "', Quiz1 = '" + fields.Quiz1.getText() +
 "', Quiz2 = '" + fields.Quiz2.getText() +
 "', Quiz3 = '" + fields.Quiz3.getText() +
 "', MP1 = '" + fields.MP1.getText() +
 "', MP2 = '" + fields.MP2.getText()+
 "', FE = '" + fields.FE.getText() +
 "' WHERE id='" + fields.id.getText() + "';";

But like it has been already correctly mentioned, use PreparedStatement instead. Also read up on the Sun JDBC tutorials to get a hang of things.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.