944,033 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Marked Solved
  • Views: 1549
  • JSP RSS
Oct 13th, 2009
0

java.sql.SQLException: Column 'XXXXXX' not found.

Expand Post »
JSP Syntax (Toggle Plain Text)
  1. package Add;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10.  
  11. import org.apache.struts.action.Action;
  12. import org.apache.struts.action.ActionForm;
  13. import org.apache.struts.action.ActionMapping;
  14. import org.apache.struts.action.ActionForward;
  15.  
  16. public class AddSuccessAction extends Action
  17. {
  18. private final static String SUCCESS = "success";
  19.  
  20. public ActionForward execute(ActionMapping mapping, ActionForm form,
  21. HttpServletRequest request, HttpServletResponse response)
  22. throws Exception
  23. {
  24. MyBean bean = (MyBean) form;
  25. String cityName = bean.getCn();
  26. String pincode = bean.getPc();
  27.  
  28. Connection con = null;
  29. ResultSet rs = null;
  30. PreparedStatement stmt = null;
  31.  
  32. String connectionURL = "jdbc:mysql://localhost:3306/city";
  33.  
  34. try
  35. {
  36. Class.forName("com.mysql.jdbc.Driver");
  37. con = DriverManager.getConnection (connectionURL,"root","welcome12#");
  38. String sq = "SELECT city_name,pincode from data ";
  39. stmt = con.prepareStatement(sq);
  40. rs = stmt.executeQuery();
  41. boolean flag = false;
  42. while (rs.next())
  43. {
  44. String pic = rs.getString(pincode);
  45.  
  46. if(pic=="pincode")
  47. {
  48. System.out.println("Pincode already exists");
  49. flag = true;
  50. }
  51. }
  52. if(!flag)
  53. {
  54. String sql = "INSERT INTO city (city name,pincode) VALUES('"+cityName+','+pincode+"')";
  55. stmt = con.prepareStatement(sql);
  56. stmt.executeUpdate();
  57. }
  58. }
  59. catch (SQLException e)
  60. {
  61. e.printStackTrace();
  62. }
  63. catch (Exception e)
  64. {
  65. e.printStackTrace();
  66. }
  67. finally
  68. {
  69. stmt.close();
  70. con.close();
  71. }
  72. return mapping.findForward(SUCCESS);
  73. }
  74.  
  75. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vishalanuj is offline Offline
10 posts
since Oct 2009
Oct 13th, 2009
0
Re: java.sql.SQLException: Column 'XXXXXX' not found.
JSP Syntax (Toggle Plain Text)
  1. String pic = rs.getString("pincode");
not
JSP Syntax (Toggle Plain Text)
  1. String pic = rs.getString(pincode);

Also, use a preparedStatement, cobbling a statement together using String concatenation like you've done is just begging for trouble. Maybe only as innocent as a mistyped user input string that causes the SQL to fail with a syxntax error, or as damaging as an intentional SQL Injection attack.

Edit: And this
JSP Syntax (Toggle Plain Text)
  1. if(pic=="pincode")
is probably backwards, too.
Last edited by masijade; Oct 13th, 2009 at 6:49 am.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Oct 13th, 2009
-1
Re: java.sql.SQLException: Column 'XXXXXX' not found.
Edit: Ignore this, masijade already spotted error
First check your name spelling for tables and columns used if you have any spelling mistakes (can check it as you failed to provide database or affected tables specs)

In the future please post full error message not just section.
Last edited by peter_budo; Oct 13th, 2009 at 6:49 am. Reason: Edit
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,658 posts
since Dec 2004
Oct 13th, 2009
0
Re: java.sql.SQLException: Column 'XXXXXX' not found.
Click to Expand / Collapse  Quote originally posted by masijade ...
JSP Syntax (Toggle Plain Text)
  1. String pic = rs.getString("pincode");
not
JSP Syntax (Toggle Plain Text)
  1. String pic = rs.getString(pincode);

Also, use a preparedStatement, cobbling a statement together using String concatenation like you've done is just begging for trouble. Maybe only as innocent as a mistyped user input string that causes the SQL to fail with a syxntax error, or as damaging as an intentional SQL Injection attack.

Edit: And this
JSP Syntax (Toggle Plain Text)
  1. if(pic=="pincode")
is probably backwards, too.
Hi.i just rectified the error.i should have used if(pic.equals(pincode)) instead of if(pic=="pincode").
i am a newbie.
thanks for replying neways.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vishalanuj is offline Offline
10 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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 JSP Forum Timeline: problem in comparision of dropdown value and database vale
Next Thread in JSP Forum Timeline: checkbox value





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


Follow us on Twitter


© 2011 DaniWeb® LLC