Results 1 to 6 of 6
Thread: Update statement not working.
- 04-08-2010, 10:57 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 23
- Rep Power
- 0
Update statement not working.
When I run the program, there seems to be a problem in the
PreparedStatement s2=con.prepareStatement(query+" where "+condition);
s2.execute();
part. Until then everything seems fine. The System.out.println(query+" where "+condition); prints just what I want it to print. Only, it doesn't seem to be getting executed. It just gets stuck and I get no error. I have to stop it manually outside the window. The code in red is the part where I'm having trouble.
Java Code:private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { String option,query="",condition=""; if(!jTextField2.getText().contentEquals("")) { if(!jRadioButton1.isSelected()) { if(condition.isEmpty()) { condition="name='"+jTextField2.getText()+"'"; } else { condition+=" and name='"+jTextField2.getText()+"'"; } } } if(!jTextField3.getText().contentEquals("")) { if(!jRadioButton2.isSelected()) { if(condition.isEmpty()) { condition="subjects_handled='"+jTextField3.getText()+"'"; } else { condition+=" and subjects_handled='"+jTextField3.getText()+"'"; } } } if(!jTextField4.getText().contentEquals("")) { if(!jRadioButton3.isSelected()) { if(condition.isEmpty()) { condition="desig='"+jTextField4.getText()+"'"; } else { condition+=" and desig='"+jTextField4.getText()+"'"; } } } if(!jTextField5.getText().contentEquals("")) { if(!jRadioButton4.isSelected()) { if(condition.isEmpty()) { condition="idno='"+jTextField5.getText()+"'"; } else { condition+=" and idno='"+jTextField5.getText()+"'"; } } } if(jTextField2.getText().equals("")) { if(jTextField3.getText().equals("")) { if(jTextField4.getText().equals("")) { if(jTextField5.getText().equals("")) { JOptionPane.showMessageDialog(rootPane,"Enter values"); } } } } if (jRadioButton1.isSelected()) { option="name"; query="update "+Exam_Season.dept2+" set name='"+jTextField2.getText()+"'"; } if (jRadioButton2.isSelected()) { option="subjects"; query="update "+Exam_Season.dept2+" set subjects_handled='"+jTextField3.getText()+"'"; } if (jRadioButton3.isSelected()) { option="desig"; query="update "+Exam_Season.dept2+" set desig='"+jTextField4.getText()+"'"; } if (jRadioButton4.isSelected()) { option="idno"; query="update "+Exam_Season.dept2+" set idno='"+jTextField5.getText()+"'"; } [B][COLOR="Red"]Connection con = null; String driver="sun.jdbc.odbc.JdbcOdbcDriver"; String user = "scott"; String pass = "tiger"; try{ System.out.println(query+ " where "+condition); Class.forName(driver); con = DriverManager.getConnection("jdbc:odbc:login",user,pass); Statement s=con.createStatement(); //System.out.println("so far so good"); //s.execute(query+" where "+condition); PreparedStatement s2=con.prepareStatement(query+" where "+condition); s2.execute(); JOptionPane.showMessageDialog(rootPane, "Successfully updated"); } catch(Exception e) { JOptionPane.showMessageDialog(rootPane, "Error."); e.printStackTrace(); } }[/COLOR][/B]Last edited by OMFGITSROHIT; 04-08-2010 at 11:19 AM.
- 04-08-2010, 11:36 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
1.) Use code tags when posting code.
2.) When using a PreparedStatement, use it with parameters.
3.) Did you check your console for any possible errors?
4.) When running an update statement, use the executeUpdate method and check its return value.
- 04-08-2010, 11:54 AM #3
Hi,
It's highly impossible to scan your code.Please put the code with code tag.
You are telling that s.o.p is printing ur query and not executing.Best thing is directly take the printed query and execute in sql editor.Might be spaces or case problem will be there in the executing query.
Try to put proper indentation.Ramya:cool:
- 04-08-2010, 12:43 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
You're doing too much in one place.
Mixing display logic (all those option panes) in with your db logic is simply confusing things.
Strip out the display stuff and simply have a db layer that takes the input values and builds a quiery from that.
As has been stated, there's no point using a PreparedStatement if you're not going to set the parameters (ie using '?' and all that stuff). May as well stick to a Statement. However, since this involves user input, it should be a PreparedStatement with properly bound values.
- 04-08-2010, 12:49 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 23
- Rep Power
- 0
The part in red is the only part that matters. The other part generates the value for the strings query and condition.
System.out.println(query+ " where "+condition); prints just the statement I need to execute. Infact I type the exact words and execute it in sql.
Whereas, in netbeans it doesn't seem to work. Execution occurs until these 3 lines of code. Here, it stops executing or maybe just keeps doing so forever. It doesn't even go to the catch part of the exception.
Java Code:PreparedStatement s2=con.prepareStatement(query+" where "+condition); s2.execute(); JOptionPane.showMessageDialog(rootPane, "Successfully updated");
- 04-08-2010, 01:03 PM #6
Member
- Join Date
- Mar 2010
- Posts
- 23
- Rep Power
- 0
Guys, thanks for the help. Its working now. Replaced a small part of the code and it worked.
Java Code:Connection con = null; String driver="sun.jdbc.odbc.JdbcOdbcDriver"; String user = "scott"; String pass = "tiger"; try{ String qry=query+" where "+condition; System.out.println(qry); Class.forName(driver); con = DriverManager.getConnection("jdbc:odbc:login",user,pass); Statement s=con.createStatement(); System.out.println("so far so good"); s.execute(qry); //PreparedStatement s2=con.prepareStatement(qry); //s2.execute(); JOptionPane.showMessageDialog(rootPane, "Successfully updated"); } catch(Exception e) { JOptionPane.showMessageDialog(rootPane, "Error."); e.printStackTrace(); }
Similar Threads
-
phonebook update
By nanna in forum New To JavaReplies: 5Last Post: 03-09-2009, 10:13 PM -
How to update my jdk???
By low224 in forum New To JavaReplies: 4Last Post: 01-04-2009, 04:51 PM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM -
Using sql:update tag
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 11:49 PM -
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08-01-2007, 04:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks