General error

Reply

Join Date: Nov 2006
Posts: 42
Reputation: bluebird is an unknown quantity at this point 
Solved Threads: 0
bluebird bluebird is offline Offline
Light Poster

General error

 
0
  #1
Sep 17th, 2007
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".

  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. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,358
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is online now Online
Nearly a Posting Maven

Re: General error

 
0
  #2
Sep 17th, 2007
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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC