0

Here i made table called sub_master with column sub_id and name, insertion and deletion working perfectly fine with this, so put those functions here as well to get reference for update function and i'm using PostgreSQL for this. In command line UPDATE query is working fine and query as: UPDATE school_submaster SET name ='' WHERE sub_id = ;

private void InsertRowActionPerformed(java.awt.event.ActionEvent evt) 
{                                          
  String query = "INSERT INTO school_submaster (sub_id, \"name\") VALUES ("+SidInput.getText()+",'"+SnameInput.getText()+"')";
  executeSQlQuery(query, "Inserted");
}                                         

private void UpdateRowActionPerformed(java.awt.event.ActionEvent evt)    
{                                          
  String query = "UPDATE school_submaster SET 'name' ='"+SnameInput.getText()+"'+WHERE sub_id = "+SidInput.getText();
  executeSQlQuery(query, "Updated");
}                                         

private void DeleteRowActionPerformed(java.awt.event.ActionEvent evt) 
{                                          
    String query = "DELETE FROM school_submaster WHERE sub_id = "+SidInput.getText();
    executeSQlQuery(query, "Deleted");
}                                         
4
  • 2
    1) "Update SQL query is not working in Java Swing" Does it work in the command line? If not, it's a pretty good indication that Swing has nothing to do with the problem, and should not be mentioned. Same goes for your IDE mentioned in the tags. 2) Use a PreparedStatement rather than raw strings when accessing a DB. 3) Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and use it consistently. Commented Apr 2, 2017 at 12:44
  • yes it is working in command line @AndrewThompson Commented Apr 2, 2017 at 13:15
  • For better help sooner, post a minimal reproducible example or Short, Self Contained, Correct Example. Commented Apr 2, 2017 at 13:20
  • What is the error you get? "Not working" is not a proper error description. Also, but: please learn how to properly use a PreparedStatement (maybe that even fixes your problem) Commented Apr 2, 2017 at 16:34

2 Answers 2

2

Only use single quotes for string and date constants. Never use single quotes around column names or table names.

Your update is:

UPDATE school_submaster
    SET 'name' ='<something>'+WHERE sub_id = "+SidInput.getText();

This has the additional issue of a + in the query string. It should look ore like:

UPDATE school_submaster
    SET name = '<something>'
    WHERE sub_id = "+SidInput.getText();

But even that is not true. You need to learn to use parameters to pass parameters into queries. The query should be some variant of:

UPDATE school_submaster
    SET name = ?
    WHERE sub_id = ?

Where the ? is a placeholder for a parameter (it might also be @name or something else).

Sign up to request clarification or add additional context in comments.

Comments

0

You are missing a space in the sql WHERE clause, so add it as shown below:

String query = "UPDATE school_submaster SET 'name' ='"+
                 SnameInput.getText()+"'  WHERE sub_id = "+SidInput.getText();

Comments

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.