Reputation: 39
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) {
}
Upvotes: 0
Views: 967
Reputation: 521914
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.
Upvotes: 1