Results 1 to 8 of 8
Thread: SQL Update function
- 07-14-2011, 07:09 PM #1
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....
can someone help me please. :p ^^Java 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(); } }
ty, liluma
- 07-14-2011, 07:10 PM #2
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)
- 07-14-2011, 07:58 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,480
- Rep Power
- 16
You're missing quotes for the set values. The above will give you:Java Code:ResultSet resultSet = statement.executeQuery("UPDATE HuidigeTaal SET Taal = " + nieuweTaal + ", Land = " + nieuwLand + " WHERE Taal = " + oudeTaal + "");
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.Last edited by Tolls; 07-14-2011 at 07:58 PM. Reason: Make the code fit better
- 07-14-2011, 08:00 PM #4
and how can i fix this???
so what should i change for the program to work?
- 07-14-2011, 08:43 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,480
- Rep Power
- 16
Use a PreparedStatement is the recommended fix, instead of a Statement as you are now.
Your SQL would then be:
and you'd us setString (assuming they're all Strings) to set the values:Java Code:UPDATE HuidigeTaal SET Taal = ?, Land = ? WHERE Taal = ?
will set the first ? to the correct value (handling all quotes and escaping problems).Java Code:ps.setString(1,nieuweTaal);
- 07-15-2011, 01:58 PM #6
Member
- Join Date
- Jul 2011
- Posts
- 26
- Rep Power
- 0
And also its not jst a query.Its update query. its some thing like "executeupdate". try it out.
- 07-15-2011, 02:22 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,480
- Rep Power
- 16
Oh, I missed they were doing an executeQuery().
Nice catch.
- 07-15-2011, 05:54 PM #8
Similar Threads
-
function
By nikkka in forum New To JavaReplies: 7Last Post: 04-10-2011, 10:07 AM -
Calling function in Javascript- from other function
By jdigger in forum New To JavaReplies: 1Last Post: 02-27-2011, 09:00 PM -
Hibernate update function
By ananddevaraj in forum Web FrameworksReplies: 1Last Post: 06-30-2010, 01:28 PM -
Possible? Callback function passed as arguments to another function
By TreyAU21 in forum Advanced JavaReplies: 3Last Post: 12-04-2009, 03:08 PM -
Auto Update Function
By abiieez in forum New To JavaReplies: 4Last Post: 04-16-2008, 04:17 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks