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

Thread Solved

Join Date: Oct 2009
Posts: 10
Reputation: vishalanuj is an unknown quantity at this point 
Solved Threads: 0
vishalanuj vishalanuj is offline Offline
Newbie Poster

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

 
0
  #1
Oct 13th, 2009
  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. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,415
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: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
0
  #2
Oct 13th, 2009
  1. String pic = rs.getString("pincode");
not
  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
  1. if(pic=="pincode")
is probably backwards, too.
Last edited by masijade; Oct 13th, 2009 at 6:49 am.
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  
Join Date: Dec 2004
Posts: 4,200
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer
 
-1
  #3
Oct 13th, 2009
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
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: vishalanuj is an unknown quantity at this point 
Solved Threads: 0
vishalanuj vishalanuj is offline Offline
Newbie Poster
 
0
  #4
Oct 13th, 2009
Originally Posted by masijade View Post
  1. String pic = rs.getString("pincode");
not
  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
  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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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