Results 1 to 8 of 8
  1. #1
    liluma's Avatar
    liluma is offline Member
    Join Date
    May 2011
    Location
    belgium
    Posts
    50
    Rep Power
    0

    Default 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....

    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();
    		}
    	}
    can someone help me please. :p ^^

    ty, liluma

  2. #2
    liluma's Avatar
    liluma is offline Member
    Join Date
    May 2011
    Location
    belgium
    Posts
    50
    Rep Power
    0

    Default

    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)

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Java 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.
    Last edited by Tolls; 07-14-2011 at 08:58 PM. Reason: Make the code fit better

  4. #4
    liluma's Avatar
    liluma is offline Member
    Join Date
    May 2011
    Location
    belgium
    Posts
    50
    Rep Power
    0

    Default

    and how can i fix this???

    so what should i change for the program to work?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Use a PreparedStatement is the recommended fix, instead of a Statement as you are now.
    Your SQL would then be:
    Java Code:
    UPDATE HuidigeTaal SET Taal = ?, Land = ? WHERE Taal = ?
    and you'd us setString (assuming they're all Strings) to set the values:
    Java Code:
    ps.setString(1,nieuweTaal);
    will set the first ? to the correct value (handling all quotes and escaping problems).

  6. #6
    acmohan is offline Member
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Default

    And also its not jst a query.Its update query. its some thing like "executeupdate". try it out.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Oh, I missed they were doing an executeQuery().
    Nice catch.

  8. #8
    liluma's Avatar
    liluma is offline Member
    Join Date
    May 2011
    Location
    belgium
    Posts
    50
    Rep Power
    0

    Default

    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. ^^

Similar Threads

  1. function
    By nikkka in forum New To Java
    Replies: 7
    Last Post: 04-10-2011, 11:07 AM
  2. Calling function in Javascript- from other function
    By jdigger in forum New To Java
    Replies: 1
    Last Post: 02-27-2011, 10:00 PM
  3. Hibernate update function
    By ananddevaraj in forum Web Frameworks
    Replies: 1
    Last Post: 06-30-2010, 02:28 PM
  4. Replies: 3
    Last Post: 12-04-2009, 04:08 PM
  5. Auto Update Function
    By abiieez in forum New To Java
    Replies: 4
    Last Post: 04-16-2008, 05:17 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •