Results 1 to 19 of 19
  1. #1
    bodylojohn is offline Member
    Join Date
    Nov 2013
    Posts
    37
    Rep Power
    0

    Default ExecuteUpdate throws exception

    Hello,

    I am fairly new to java and thats the reason that I am following an online class to educate myself in this matter.

    For an assignement we have to built an applet containing buttos en jtextfields and jtextareas that will insert data in a MySql database.
    The connection to the database is correct.

    My code throws an exception on the following code "statement.executeUpdate(sset);"
    The function is called nieuwGerecht and resides in the Class Kreta.

    Java Code:
    public void nieuwGerecht(){
    	try {
    		Statement statement = con.connection.createStatement();
    		System.out.println("Statement statement = con.connection.createStatement(); --> OK");
    		String sset;
    		sset = "INERT INTO 'afhaalmenus' ('naam_gerecht','prijs','ingredienten','recept')" +
    				"VALUES ('" + this.naam_gerecht + "', " + this.prijs + "', '" + this.ingredienten + "', '" + this.recept + "');";
    		System.out.println("built string --> OK");
    		statement.executeUpdate(sset);
    		System.out.println("statement.executeUpdate(sset); --> OK");
    		}
    	catch (Exception e){
    		System.out.println("foutje");
    	}
    	}
    The code where the button resides is in the Class KretaGUI.
    Java Code:
    class bToevoegenListener implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			Kreta kreta = new Kreta();
    			kreta.setNaam_Gerecht(tNaam_gerecht.getText());
    			kreta.setPrijs(tPrijs.getText());
    			kreta.setNaam_Gerecht(tNaam_gerecht.getText());
    			kreta.setNaam_Gerecht(tNaam_gerecht.getText());
    			kreta.nieuwGerecht();
    		}
    	}
    What am I doing wrong?
    Also tips and commends on my programming are welcome.
    Thanks in advance

    PS: I attached the 3 classes containing all of the code as a .zip file
    Attached Files Attached Files
    Last edited by bodylojohn; 01-30-2014 at 12:45 PM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: ExecuteUpdate throws exception

    WHAT exception? And use a PreparedStatement, do NOT cobble together an SQL string like this or you are just BEGGING for problems.

    But this
    Java Code:
    this.naam_gerecht + "', " + this.prijs + "'
    is probably the CURRENT problem. Notice the unmatched single quote?

    THAT is one of the MANY reasons you should not be doing this.
    Last edited by masijade; 01-30-2014 at 12:57 PM.

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: ExecuteUpdate throws exception

    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    bodylojohn is offline Member
    Join Date
    Nov 2013
    Posts
    37
    Rep Power
    0

    Default Re: ExecuteUpdate throws exception

    Quote Originally Posted by PhHein View Post
    Yes indeed I crossposted.
    Is this a problem?

    Thanks for replying

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: ExecuteUpdate throws exception

    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,669
    Rep Power
    5

    Default Re: ExecuteUpdate throws exception

    Quote Originally Posted by bodylojohn View Post
    Yes indeed I crossposted.
    Is this a problem?

    Thanks for replying
    If you crosspost and I spend a good amount of time writing an answer that was already given in your other post(s), I will get pretty angry with you for not simply posting a link to it so I could check.

    Its a form of courtesy; make the job of the people who will take the time to answer you as easy as possible.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    bodylojohn is offline Member
    Join Date
    Nov 2013
    Posts
    37
    Rep Power
    0

    Default Re: ExecuteUpdate throws exception

    @masijade:
    I definitely agree with you. But we have to use the examples from our education material.
    Thank you for replying.

  8. #8
    bodylojohn is offline Member
    Join Date
    Nov 2013
    Posts
    37
    Rep Power
    0

    Default Re: ExecuteUpdate throws exception

    I found a solution that works:
    Java Code:
    public void nieuwGerecht(){
    	try {
    		Statement statement = con.connection.createStatement();
    		
    		String gget;
    		gget = "SELECT max(afhaalmenus.menu_id) FROM afhaalmenus;";
    				
    		ResultSet rs = statement.executeQuery(gget);
    		rs.last();
    		this.menu_id = (rs.getInt(1)) + 1;
    		}
    	catch (Exception e) {
    		con.displaySQLErrors(e);		
    	}
    	
    	try {
    		PreparedStatement sset = con.connection.prepareStatement("INSERT INTO afhaalmenus (menu_id,naam_gerecht,prijs,ingredienten,recept) VALUES (?,?,?,?,?)");
    		sset.setInt(1, this.menu_id);
    		sset.setString(2, this.naam_gerecht);
    		sset.setString(3, this.prijs);
    		sset.setString(4, this.ingredienten);
    		sset.setString(5, this.recept);
    		sset.executeUpdate();
    		}
    	catch (Exception e){
    		e.printStackTrace(); 
    	}
    	}
    Everybody thank you for helping!!!

  9. #9
    bodylojohn is offline Member
    Join Date
    Nov 2013
    Posts
    37
    Rep Power
    0

    Default Re: ExecuteUpdate throws exception

    Hello,

    I finally completed my assignment and everything works fine. I've got 3 classes and I would love it if somebody could look through them and give me some tips and tricks concerning my code.
    NOTE: everything works and I completed the assignment but I just want to learn the "right" way and check if there aren't any stupid mistakes or redundancy in my code.

    Thanks in advance!!!
    Attached Files Attached Files

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,669
    Rep Power
    5

    Default Re: ExecuteUpdate throws exception

    The one thing I would stress is that you start to use English naming. Java itself already lends from the English language, if you then start to use Dutch method and variable names you get a mess which will constantly confuse your brain. Also, it makes it easier to share the code in international environments, be it a forum, an open source project or with foreign colleagues/classmates.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    911
    Rep Power
    2

    Default Re: ExecuteUpdate throws exception

    Selecting the new ID using max()+1 is generally a bad idea. When your code is executed twice real quick, for example by two threads, then the second thread will get the same ID before the first thread could have executed the second statement. If possible, use autoincrements, sequences, whatever the database uses. In your case it won't go wrong since you can't click that fast, but if you apply this in a real world moderate sized application, you will definately run into problems here.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  12. #12
    bodylojohn is offline Member
    Join Date
    Nov 2013
    Posts
    37
    Rep Power
    0

    Default Re: ExecuteUpdate throws exception

    Quote Originally Posted by gimbal2 View Post
    The one thing I would stress is that you start to use English naming. Java itself already lends from the English language, if you then start to use Dutch method and variable names you get a mess which will constantly confuse your brain. Also, it makes it easier to share the code in international environments, be it a forum, an open source project or with foreign colleagues/classmates.
    I totally agree with you and normally (I am a VB.NET programmer) is only use english.
    However the course material is in dutch and my teacher also wants everything in dutch. But I totally AGREE with you on this!

    Thanks

  13. #13
    bodylojohn is offline Member
    Join Date
    Nov 2013
    Posts
    37
    Rep Power
    0

    Default Re: ExecuteUpdate throws exception

    Quote Originally Posted by SurfMan View Post
    Selecting the new ID using max()+1 is generally a bad idea. When your code is executed twice real quick, for example by two threads, then the second thread will get the same ID before the first thread could have executed the second statement. If possible, use autoincrements, sequences, whatever the database uses. In your case it won't go wrong since you can't click that fast, but if you apply this in a real world moderate sized application, you will definately run into problems here.
    Thanks.
    The database uses autoincrement.

    I replaced:
    Java Code:
    public void nieuwGerecht(){
    	try {
    		Statement statement = con.connection.createStatement();
    		String gget;
    		// Verkrijg het hoogste menu_id
    		gget = "SELECT max(afhaalmenus.menu_id) FROM afhaalmenus;";
    				
    		ResultSet rs = statement.executeQuery(gget);
    		// Ga naar het laatste record in de Resultset
    		rs.last();
    		// Verhoog het hoogste menu_id met 1
    		this.menu_id = (rs.getInt(1)) + 1;
    		}
    	catch (Exception e) {
    		con.displaySQLErrors(e);		
    	}
    	try {
                    PreparedStatement sset = con.connection.prepareStatement("INSERT INTO afhaalmenus (menu_id,naam_gerecht,prijs,ingredienten,recept) VALUES (?,?,?,?,?)");
    		sset.setInt(1, this.menu_id);			//Eerste parameter '?' is het menu_id welke wordt toegekend in het eerste stuk van de functie. 
    		sset.setString(2, this.naam_gerecht);	//Tweede parameter '?' is het naam_gerecht
    		sset.setString(3, this.prijs);			//Derde parameter '?' is het prijs
    		sset.setString(4, this.ingredienten);	//Vierde parameter '?' is het ingredienten
    		sset.setString(5, this.recept);			//Vijfde parameter '?' is het recept
    		sset.executeUpdate();
    		}
    	catch (Exception e){
    		e.printStackTrace(); // Ik maak gebruik van de printstack om meer informatie te verkrijgen bij een exception 
    	}
    	}
    With:
    Java Code:
    public void nieuwGerecht(){
                      try {
    				PreparedStatement sset = con.connection.prepareStatement("INSERT INTO afhaalmenus (naam_gerecht,prijs,ingredienten,recept) VALUES (?,?,?,?)",Statement.RETURN_GENERATED_KEYS);
    				sset.setString(1, this.naam_gerecht);	//Eerste parameter '?' is het naam_gerecht
    				sset.setString(2, this.prijs);			//Tweede parameter '?' is het prijs
    				sset.setString(3, this.ingredienten);	//Derde parameter '?' is het ingredienten
    				sset.setString(4, this.recept);			//Vierde parameter '?' is het recept
    				sset.executeUpdate();
    				ResultSet tableKeys = sset.getGeneratedKeys();
    				tableKeys.next();
    				this.menu_id = tableKeys.getInt(1);
    		}
    	catch (Exception e){
    		e.printStackTrace(); // Ik maak gebruik van de printstack om meer informatie te verkrijgen bij een exception 
    	}
    	}
    And it works great
    Last edited by bodylojohn; 01-31-2014 at 11:50 AM.

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,669
    Rep Power
    5

    Default Re: ExecuteUpdate throws exception

    Quote Originally Posted by bodylojohn View Post
    I totally agree with you and normally (I am a VB.NET programmer) is only use english.
    However the course material is in dutch and my teacher also wants everything in dutch. But I totally AGREE with you on this!

    Thanks
    Oh, then I didn't say anything. You're doing the right thing.

    Quote Originally Posted by bodylojohn View Post
    Thanks.
    The database uses autoincrement. How would I use this in my code?
    Nothing, the database does it for you! Simply don't pass a value for the ID and see how an ID will magically appear upon insertion.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    bodylojohn is offline Member
    Join Date
    Nov 2013
    Posts
    37
    Rep Power
    0

    Default Re: ExecuteUpdate throws exception

    Quote Originally Posted by gimbal2 View Post
    Oh, then I didn't say anything. You're doing the right thing.



    Nothing, the database does it for you! Simply don't pass a value for the ID and see how an ID will magically appear upon insertion.
    Thanks. I already posted the adjustments in post #13

  16. #16
    bodylojohn is offline Member
    Join Date
    Nov 2013
    Posts
    37
    Rep Power
    0

    Default Re: ExecuteUpdate throws exception

    I just recieved an email from my teacher.
    Because the assignement (i have to make an applet) sais that there hase to be a html file that calls the class.

    How should I go about this?

  17. #17
    bodylojohn is offline Member
    Join Date
    Nov 2013
    Posts
    37
    Rep Power
    0

    Default Re: ExecuteUpdate throws exception

    Anybody?

  18. #18
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,669
    Rep Power
    5

    Default Re: ExecuteUpdate throws exception

    Google it dude. You are abusing this thread to now have ALL your questions answered, that's not going to help you learn to do stuff on your own.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  19. #19
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    911
    Rep Power
    2

    Default Re: ExecuteUpdate throws exception

    There should be plenty of examples of an html-file containing the applet-tag or, more recently the object-tag. That will get you started.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. The below program fails and throws an exception
    By frintocf in forum New To Java
    Replies: 1
    Last Post: 10-04-2011, 09:41 AM
  2. throws exception
    By simorgh in forum New To Java
    Replies: 1
    Last Post: 07-30-2010, 12:24 AM
  3. JButton: getLocationOnScreen() throws Exception
    By ExceptionHandler in forum AWT / Swing
    Replies: 10
    Last Post: 02-15-2010, 01:38 PM
  4. throws Exception
    By javaplus in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 07:32 PM

Posting Permissions

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