944,144 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 854
  • Java RSS
Sep 17th, 2007
0

General error

Expand Post »
Hello!
I am trying to add new data into a database.
What is the mistake in my code?
I got the error message such as "general error".

Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.sql.*;
  4. import javax.swing.*;
  5. public class AddRecord implements ActionListener{
  6. private ScrollingPanel fields;
  7. private JTextArea output;
  8. private Connection connection;
  9. public AddRecord(Connection c, ScrollingPanel f, JTextArea o)
  10. {
  11. connection =c;
  12. fields=f;
  13. output=o;
  14. }
  15. public void actionPerformed( ActionEvent e)
  16. {
  17. try {
  18. Statement statement=connection.createStatement();
  19. if( !fields.last.getText().equals("")&&
  20. !fields.first.getText().equals("")){
  21. String query="INSERT INTO addresses ("+
  22. "firstname, lastname, address, city,"+
  23. "stateorprovince, postalcode, country,"+
  24. "emailaddress, homephone, faxnumber"+
  25. ") VALUES ('"+
  26. fields.first.getText()+"','"+
  27. fields.last.getText()+"','"+
  28. fields.address.getText()+"','"+
  29. fields.city.getText()+"','"+
  30. fields.state.getText()+"','"+
  31. fields.zip.getText()+"','"+
  32. fields.country.getText()+"','"+
  33. fields.email.getText()+"','"+
  34. fields.home.getText()+"','"+
  35. fields.fax.getText()+"')";
  36. output.append("\nSending query:"+ connection.nativeSQL(query)+"\n");
  37. int result=statement.executeUpdate(query);
  38. if (result==1)
  39. output.append("\nInsertion successful\n");
  40. else {
  41. output.append("\nInsertion failed\n");
  42. fields.first.setText("");
  43. fields.last.setText("");
  44. fields.address.setText("");
  45. fields.city.setText("");
  46. fields.state.setText("");
  47. fields.zip.setText("");
  48. fields.country.setText("");
  49. fields.email.setText("");
  50. fields.home.setText("");
  51. fields.fax.setText("");
  52. }
  53. }
  54. else
  55. output.append("\nEnter at least first and "+
  56. "last name then press Add\n");
  57. statement.close();
  58. }
  59. catch (SQLException sqlex){
  60. sqlex.printStackTrace();
  61. output.append(sqlex.toString());
  62. }
  63. }
  64. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bluebird is offline Offline
58 posts
since Nov 2006
Sep 17th, 2007
0

Re: General error

Try using a preparedStatement rather than cobbling together a query statement like that.

See the API and Sun tutorials (a link can be found in the API) for more info.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: New in Java. Please help!
Next Thread in Java Forum Timeline: Extract columns from text file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC