-
SQL Update function
can't see what i'm doing wrong...
it'll prob be another stupid typo but just can't seem to find it
i'll explain what i'm hoping to achieve. :p
so i'm making a little project(kind of rpg game) and i want to save wich language the user wants to use.
so i made a table in MSAccess database and with those two values i make a resourcebundle object wich gives the matching strings for the selected language.
this is the code in the mapper....
Code:
public void updateHuidigeTaal(ResourceBundles oud, ResourceBundles nieuw)
{
Statement statement;
Connection connection = PersistentieController.getInstance().getConnection();
try{
statement = connection.createStatement();
String nieuweTaal = nieuw.getTaal();
String nieuwLand = nieuw.getLand();
String oudeTaal = oud.getTaal();
//String UPDATE_TALEN_SQL = "UPDATE HuidigeTaal SET Taal = 'nieuweTaal', Land = 'nieuwLand' WHERE Taal = 'oudeTaal'";
ResultSet resultSet = statement.executeQuery("UPDATE HuidigeTaal SET Taal = " + nieuweTaal + ", Land = " + nieuwLand + " WHERE Taal = " + oudeTaal + "");
}catch (SQLException e)
{
JOptionPane.showMessageDialog(null, /*e.getMessage(),*/
"Geen skills gevonden");// JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
can someone help me please. :p ^^
ty, liluma
-
this might be usefull. :p
this is the error i'm getting...
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(Unkno wn Source)
at persitentie.TaalMapper.updateHuidigeTaal(TaalMappe r.java:99)
at persitentie.PersistentieController.updateTaal(Pers istentieController.java:58)
at domein.DomeinController.updateTaal(DomeinControlle r.java:46)
at gui.Console.<init>(Console.java:35)
at StartUp.main(StartUp.java:9)
-
Code:
ResultSet resultSet = statement.executeQuery("UPDATE HuidigeTaal SET Taal = " + nieuweTaal + ", Land = " + nieuwLand + " WHERE Taal = " + oudeTaal + "");
You're missing quotes for the set values. The above will give you:
UPDATE HuidigeTaal SET Taal = <nieuweTaal>, Land = <nieuwLand> WHERE Taal = <oudeTaal>
If you used a PreparedStatement and setString (or whatever type) then you wouldn't have this problem.
-
and how can i fix this???
so what should i change for the program to work?
-
Use a PreparedStatement is the recommended fix, instead of a Statement as you are now.
Your SQL would then be:
Code:
UPDATE HuidigeTaal SET Taal = ?, Land = ? WHERE Taal = ?
and you'd us setString (assuming they're all Strings) to set the values:
Code:
ps.setString(1,nieuweTaal);
will set the first ? to the correct value (handling all quotes and escaping problems).
-
And also its not jst a query.Its update query. its some thing like "executeupdate". try it out.
-
Oh, I missed they were doing an executeQuery().
Nice catch.
-
got an easier example to work. :p
so thanks for all the help i should have no problem anymore to fix my little problem. :p ^^
ty for al the help. ^^