0

I'm trying to get my JSpinners value to add to the column in my database, why is my variable query2 working when I put it in db.update() but not query? What do I need to change?

try {
    int points = (int) antalPoäng.getValue();

    String query = "UPDATE ELEVHEM SET HUSPOANG = HUSPOANG" + points + "WHERE ELEVHEMSNAMN ='Gryffindor'";
    String query2 = "UPDATE ELEVHEM SET HUSPOANG = HUSPOANG +1 WHERE ELEVHEMSNAMN ='Gryffindor'";

    if (namnElevhem.getSelectedItem().equals("Gryffindor")) {
        db.update(query);
        JOptionPane.showMessageDialog(null,"The points has been added")
    }
}        
catch(InfException e) {

}
1
  • 1
    Off topic: matter off opinion and or taste i would make SQL keywords/functions uppercase only and identifiers like database, table and column names all lowercase makes it a bit more easy to read and maintain.. Commented Dec 29, 2018 at 15:28

1 Answer 1

1

I suspect that in your first query you want to increment the HUSPOANG column by some amount. We could try to just correct your concatenated query string, but it would be much better to use a prepared statement here:

String sql = "UPDATE ELEVHEM SET HUSPOANG = HUSPOANG + ? WHERE ELEVHEMSNAMN = 'Gryffindor'";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, points);
ps.executeUpdate();

If you want to continue with your current approach, then you would need to fix your query string:

String query = "UPDATE ELEVHEM SET HUSPOANG = HUSPOANG + " + points +
    " WHERE ELEVHEMSNAMN = 'Gryffindor'";

You were missing some needed spaces, but again it would be preferable to use a prepared statement, which it makes it easy to avoid such formatting problems.

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

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.