Thursday 15 April 2010

how to handle sql exception in java? -


I am trying to create a simple signup form in Java. I have created a table in SQL where I have chosen not to allow zero values, but when I sign up without entering any value then no exception is being given .... all the zero values Skipping .. !! What should I do?

  Private zero b1ActionPerformed (java.awt.event.ActionEventevt) {try {class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection ("jdbc: odbc: mydsn1"); Prepared placement pstmt = con.prepareStatement ("enter project.dbo.signup values ​​(?,?,?,?,?,?)"); Pstmt.setString (1, t1.getText ()); Pstmt.setString (2, t2.getText ()); Pstmt.setString (3, String.valueOf (t3.getText ())); Pstmt.setString (4, t4.getText ()); Pstmt.setString (5, t5.getText ()); Pstmt.setString (6, c1.getSelectedItem () toString ().); Pstmt.executeUpdate (); } Grip (eclipse e) {joppenpan.Show Message Diagnosis (faucet, e.toostring ()); } Hold (exception ob) {JOptionPane.showMessageDialog (null, ob.toString ()); }}    

zero and empty is not equal if you set a column value as not null so you can not insert any null or you must enter a value. But this does not mean that you can not enter empty string ..

Example of empty and empty string

  string s = null; String s = "";   

So in your case, when you are not inserting any input into the textbox and you're getting the value using getText () then this one blank The string is not returning to hopy you understand differenc

So for an exception you can do one thing to check the input string if the length is 0, then do not include it, or enter a zero value there

  string s = t1.getText (); If (s.length () == 0) pstmt.setString (1, zero); // or do not insert    

No comments:

Post a Comment