By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,846 Members | 1,191 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,846 IT Pros & Developers. It's quick & easy.

how to retrieve and add data to a database

P: 10
hello friends
i am new to struts.

i want to add the user information in the database and then display it back to him.

i am using struts 1.2,Eclipse Platform Version: 3.4.2,mySql 5.0.1,jdk 1.5..

i have created a form bean to get the data from the user.
then in my action class i m trying to get a database connection and adding the values of the user. and i also wanna show them back to him.
its a dummy project for my learning(to get an insight into struts)
the code is as follows.

m sure the mistake is a silly one but as of now i am lost.
please help

Code:

register.jsp
Expand|Select|Wrap|Line Numbers
  1. <%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %>
  2. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  3.     pageEncoding="ISO-8859-1"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  8.   <title>Registration Page</title>
  9.   <link href="example.css" rel="stylesheet" type="text/css" />
  10. </head>
  11.  
  12. <body>
  13.  
  14. <div id="top">
  15. <table border="0">
  16. <tr>
  17. <td width="250">    
  18.         <img src="car.jpg"/>    </td>
  19. <td width="250">    <h1 align="center"><font face="courier new" >thecarsite</font></h1>    </td>
  20. <td width="250">     <h2 align="right"><html:link forward="index">Home</html:link></h2></td>
  21. </tr>
  22. </table>
  23. <hr width="100%">
  24. </div>
  25.  
  26. <div id="restpage" >
  27. <html:errors/>
  28. <html:form   action="/register">
  29.  
  30. <h2>USER DETAILS:</h2>
  31. <font size="-1" color="red">*</font>FIRST NAME <html:text property="fname" size="15" maxlength="20"/>&nbsp;&nbsp;&nbsp;&nbsp;
  32. MIDDLE NAME <html:text property="mname" size="10" maxlength="20"/>&nbsp;&nbsp;&nbsp;&nbsp;
  33. <font size="-1" color="red">*</font>LAST NAME <html:text property="lname" size="15" maxlength="20"/><br><br>
  34. DATE OF BIRTH <html:text property="dob" size="10" maxlength="10"/>(dd/mm/yyyy)<br><br>
  35. ADDRESS <html:text property="add" size="30" maxlength="50"/>&nbsp;&nbsp;&nbsp;&nbsp;
  36. CITY <html:text property="city" size="15" maxlength="20"/>&nbsp;&nbsp;&nbsp;&nbsp;
  37. STATE <html:text property="state" size="15" maxlength="20"/><br><br>
  38. ZIP <html:text property="zip" size="6" maxlength="6"/>&nbsp;&nbsp;&nbsp;&nbsp;
  39. CONTACT NO <html:text property="cno" size="10" maxlength="10"/>
  40. <font size="-1" color="red">*</font>EMAIL ID <html:text property="email" size="15" maxlength="20"/><br>
  41.  
  42. <h2>LOGIN DETAILS</h2>
  43. <font size="-1" color="red">*</font>USER NAME &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  44. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  45. <html:text property="uid" size="15" maxlength="20"/><br>
  46. <font size="-1" color="red">*</font>PASSWORD &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  47. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  48. <html:password property="psswd" size="15" maxlength="20"/><br>
  49. <font size="-1" color="red">*</font>RE-TYPE PASSWORD <html:password property="rpsswd" size="15" maxlength="20"/><br><br><br>
  50. <center>
  51. <font size="5" color="red">(*) Mandatory Fields</font><br><br>
  52. </center>
  53. <html:submit value="Submit" /><br>
  54.  
  55. </html:form >
  56. </div>
  57. </body>
  58.  
  59.  
  60. </html>

RegisterForm.java
Expand|Select|Wrap|Line Numbers
  1. package register;
  2.  
  3. import javax.servlet.http.HttpServletRequest;
  4. import org.apache.struts.action.ActionErrors;
  5. import org.apache.struts.action.ActionForm;
  6. import org.apache.struts.action.ActionMapping;
  7. import org.apache.struts.action.ActionMessage;
  8.  
  9.  
  10. public class RegisterForm extends ActionForm {
  11.  
  12.     String Fname,Lname,Email,Uid,Psswd,Rpsswd,mname,dob,add,cno,state,city,zip;
  13.  
  14.  
  15.  
  16.     public String getDob() {
  17.         return dob;
  18.     }
  19.  
  20.     public void setDob(String dob) {
  21.         this.dob = dob;
  22.     }
  23.  
  24.     public String getAdd() {
  25.         return add;
  26.     }
  27.  
  28.     public void setAdd(String add) {
  29.         this.add = add;
  30.     }
  31.  
  32.  
  33.     public String getCno() {
  34.         return cno;
  35.     }
  36.  
  37.     public void setCno(String cno) {
  38.         this.cno = cno;
  39.     }
  40.  
  41.     public String getState() {
  42.         return state;
  43.     }
  44.  
  45.     public void setState(String state) {
  46.         this.state = state;
  47.     }
  48.  
  49.     public String getCity() {
  50.         return city;
  51.     }
  52.  
  53.     public void setCity(String city) {
  54.         this.city = city;
  55.     }
  56.  
  57.     public String getZip() {
  58.         return zip;
  59.     }
  60.  
  61.     public void setZip(String zip) {
  62.         this.zip = zip;
  63.     }
  64.  
  65.     public String getMname() {
  66.         return mname;
  67.     }
  68.  
  69.     public void setMname(String mname) {
  70.         this.mname = mname;
  71.     }
  72.  
  73.     public String getFname() {
  74.         return Fname;
  75.     }
  76.  
  77.     public void setFname(String fname) {
  78.         Fname = fname;
  79.     }
  80.  
  81.     public String getLname() {
  82.         return Lname;
  83.     }
  84.  
  85.     public void setLname(String lname) {
  86.         Lname = lname;
  87.     }
  88.  
  89.     public String getEmail() {
  90.         return Email;
  91.     }
  92.  
  93.     public void setEmail(String email) {
  94.         Email = email;
  95.     }
  96.  
  97.     public String getUid() {
  98.         return Uid;
  99.     }
  100.  
  101.     public void setUid(String uid) {
  102.         Uid = uid;
  103.     }
  104.  
  105.     public String getPsswd() {
  106.         return Psswd;
  107.     }
  108.  
  109.     public void setPsswd(String psswd) {
  110.         Psswd = psswd;
  111.     }
  112.  
  113.     public String getRpsswd() {
  114.         return Rpsswd;
  115.     }
  116.  
  117.     public void setRpsswd(String rpsswd) {
  118.         Rpsswd = rpsswd;
  119.     }
  120.     public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)
  121.     {
  122.         ActionErrors actionErrors= new ActionErrors();
  123.         if(getFname()=="")
  124.         {
  125.             actionErrors.add("Fname", new ActionMessage("fname.required"));
  126.         }
  127.         if(getLname()=="")
  128.         {
  129.             actionErrors.add("Lname", new ActionMessage("lname.required"));
  130.         }
  131.         if(getEmail()=="")
  132.         {
  133.             actionErrors.add("Email", new ActionMessage("email.required"));
  134.         }
  135.         if(getUid()=="")
  136.         {
  137.             actionErrors.add("Username",new ActionMessage("uid.required"));
  138.         }
  139.  
  140.         if(getPsswd()=="")
  141.         {
  142.             actionErrors.add("Password",new ActionMessage("psswd.required"));
  143.         }
  144.         else if (getUid().equals(getPsswd()))
  145.         {
  146.             actionErrors.add("Unp",new ActionMessage("value.match"));
  147.         }
  148.         if(getRpsswd()=="")
  149.         {
  150.             actionErrors.add("Password",new ActionMessage("rpsswd.required"));
  151.         }
  152.         if ( getRpsswd().equals(getPsswd()) )
  153.         {}else{
  154.             actionErrors.add("Unp",new ActionMessage("value.notmatch"));
  155.         }
  156.  
  157.         return actionErrors;
  158.     }
  159.  
  160. }
  161.  
RegisterAction.java
Expand|Select|Wrap|Line Numbers
  1. package register;
  2.  
  3. import java.sql.Connection;
  4.  
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.sql.DataSource;
  8.  
  9. import register.RegisterDAO;
  10.  
  11. import org.apache.struts.action.Action;
  12. import org.apache.struts.action.ActionForm;
  13. import org.apache.struts.action.ActionForward;
  14. import org.apache.struts.action.ActionMapping;
  15.  
  16. public class RegisterAction extends Action {
  17.  
  18.     public ActionForward execute(ActionMapping mapping, ActionForm form,
  19.             HttpServletRequest request, HttpServletResponse response)
  20.     throws Exception {
  21.         RegisterForm form1= (RegisterForm)form;
  22.         RegisterDAO registerDAO = new RegisterDAO();
  23.         User user = new User();
  24.  
  25.         Connection con = null; 
  26.         DataSource ds = null;         
  27.  
  28.         ds = getDataSource(request);
  29.  
  30.         String fname= form1.getFname();
  31.         String mname= form1.getMname();
  32.         String lname= form1.getLname();
  33.         String dob= form1.getDob();
  34.         String address= form1.getAdd();
  35.         String city= form1.getCity();
  36.         String state= form1.getState();
  37.         String zip= form1.getZip();
  38.         String cno= form1.getCno();
  39.         String email= form1.getEmail();
  40.         String uid= form1.getUid();
  41.         String psswd= form1.getPsswd();
  42.         user= RegisterDAO.setRecord(con,user);
  43.         user= RegisterDAO.getRecord(con,user);
  44.  
  45.  
  46.         registerDAO.closeConnection(con);
  47.  
  48.  
  49.  
  50.         return mapping.findForward("success");
  51.     }
  52.  
RegisterDAO.java
Expand|Select|Wrap|Line Numbers
  1. package register;
  2.  
  3. import register.User;
  4.  
  5. import java.sql.Connection; 
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet; 
  8. import java.sql.SQLException;
  9. import java.util.ArrayList;
  10.  
  11. import javax.sql.DataSource; 
  12.  
  13. public class RegisterDAO {
  14.  
  15.  
  16.     public Connection getConnection(DataSource carsite) { 
  17.         Connection con = null; 
  18.         DataSource ds = carsite; 
  19.         try { 
  20.             con = ds.getConnection(); 
  21.         } catch (SQLException e) { 
  22.             e.printStackTrace(); 
  23.         } 
  24.         return con; 
  25.     }
  26.     public void closeConnection(Connection con) { 
  27.         try { 
  28.             con.setAutoCommit(true);
  29.             con.close(); 
  30.         } catch (SQLException e) { 
  31.             e.printStackTrace(); 
  32.         } 
  33.     }
  34.     public static User getRecord(Connection con, User user) { 
  35.         ArrayList<User> list= new ArrayList<User>();
  36.         String qry = "select * from register where username=?"; 
  37.         ResultSet resultSet = null; 
  38.  
  39.         try { 
  40.             PreparedStatement pstat = con.prepareStatement(qry); 
  41.             pstat.setString(11, user.getUid()); 
  42.             resultSet = pstat.executeQuery(); 
  43.             while (resultSet.next()) { 
  44.  
  45.                 user.setFname(resultSet.getString("fname")); 
  46.                 user.setMname(resultSet.getString("mname")); 
  47.                 user.setLname(resultSet.getString("lname")); 
  48.                 user.setDob     (resultSet.getString("dob")); 
  49.                 user.setAdd  (resultSet.getString("address")); 
  50.                 user.setCity (resultSet.getString("city")); 
  51.                 user.setState(resultSet.getString("state"));
  52.                 user.setFname(resultSet.getString("zip"));
  53.                 user.setFname(resultSet.getString("cno"));
  54.                 user.setFname(resultSet.getString("email"));
  55.  
  56.                 list.add(user);
  57.             } 
  58.  
  59.         } catch (SQLException e) { 
  60.             e.printStackTrace(); 
  61.         } return user;
  62.     }
  63.  
  64.  
  65.     public static User setRecord(Connection con, User user) {
  66.  
  67.         String qry = "INSERT INTO register VALUES('" + user.getFname() + "','"
  68.                     + user.getMname() + "','" + user.getLname() + "' ,'" + user.getDob() + " ','" + user.getAdd()
  69.                     + "','" + user.getState() + "' ,'" + user.getZip() + "','" + user.getCno() + "','"
  70.                     + user.getEmail() + "','" + user.getUid() + "','" + user.getPsswd() + "')";
  71.  
  72.  
  73.         ResultSet resultSet = null; 
  74.  
  75.         try { 
  76.             // m yet to write the code for this                 
  77.             } }
  78.         catch (SQLException e) { 
  79.                 e.printStackTrace();
  80.         }    
  81.             return user;
  82.  
  83.     }
  84.  
  85. }
  86.  
User.java
Expand|Select|Wrap|Line Numbers
  1. package register;
  2.  
  3. public class User {
  4. private String Fname,Lname,Email,Rpsswd,mname,dob,add,cno,state,city,zip,uid,psswd;
  5.  
  6. public String getPsswd() {
  7.     return psswd;
  8. }
  9.  
  10. public void setPsswd(String psswd) {
  11.     this.psswd = psswd;
  12. }
  13.  
  14. public String getUid() {
  15.     return uid;
  16. }
  17.  
  18. public void setUid(String uid) {
  19.     this.uid = uid;
  20. }
  21.  
  22. public String getFname() {
  23.     return Fname;
  24. }
  25.  
  26. public void setFname(String fname) {
  27.     Fname = fname;
  28. }
  29.  
  30. public String getLname() {
  31.     return Lname;
  32. }
  33.  
  34. public void setLname(String lname) {
  35.     Lname = lname;
  36. }
  37.  
  38. public String getEmail() {
  39.     return Email;
  40. }
  41.  
  42. public void setEmail(String email) {
  43.     Email = email;
  44. }
  45.  
  46. public String getRpsswd() {
  47.     return Rpsswd;
  48. }
  49.  
  50. public void setRpsswd(String rpsswd) {
  51.     Rpsswd = rpsswd;
  52. }
  53.  
  54. public String getMname() {
  55.     return mname;
  56. }
  57.  
  58. public void setMname(String mname) {
  59.     this.mname = mname;
  60. }
  61.  
  62. public String getDob() {
  63.     return dob;
  64. }
  65.  
  66. public void setDob(String dob) {
  67.     this.dob = dob;
  68. }
  69.  
  70. public String getAdd() {
  71.     return add;
  72. }
  73.  
  74. public void setAdd(String add) {
  75.     this.add = add;
  76. }
  77.  
  78. public String getCno() {
  79.     return cno;
  80. }
  81.  
  82. public void setCno(String cno) {
  83.     this.cno = cno;
  84. }
  85.  
  86. public String getState() {
  87.     return state;
  88. }
  89.  
  90. public void setState(String state) {
  91.     this.state = state;
  92. }
  93.  
  94. public String getCity() {
  95.     return city;
  96. }
  97.  
  98. public void setCity(String city) {
  99.     this.city = city;
  100. }
  101.  
  102. public String getZip() {
  103.     return zip;
  104. }
  105.  
  106. public void setZip(String zip) {
  107.     this.zip = zip;
  108. }
  109.  
  110. }
  111.  
Also the database is named carsite and it has a table register created in it.
the struts-config and web xml enteries are error free. i shall post them too if need be.
please help

the error is:
WARNING: Unhandled Exception thrown: class java.lang.NullPointerException
Aug 4, 2009 10:28:14 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet action threw exception
java.lang.NullPointerException
at register.RegisterDAO.setRecord(RegisterDAO.java:76 )
at register.RegisterAction.execute(RegisterAction.jav a:42)
at org.apache.struts.action.RequestProcessor.processA ctionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process( RequestProcessor.java:226)
at org.apache.struts.action.ActionServlet.process(Act ionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doPost(Acti onServlet.java:415)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11 ConnectionHandler.processConnection(Http11BaseProt ocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.process Socket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThr ead.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlR unnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)
Aug 4 '09 #1
Share this Question
Share on Google+
9 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
This is a java related question so I will move it to the appropriate forum. If you question is regarding your coding, please be sure to start the thread in the correct forum.

Regards,

Jeff
Aug 4 '09 #2

P: 10
thank you jeff..
i was confused so as to where to put it.
Aug 4 '09 #3

Expert 10K+
P: 11,448
Expand|Select|Wrap|Line Numbers
  1. WARNING: Unhandled Exception thrown: class java.lang.NullPointerException
  2. Aug 4, 2009 10:28:14 AM org.apache.catalina.core.StandardWrapperValve invoke
  3. SEVERE: Servlet.service() for servlet action threw exception
  4. java.lang.NullPointerException
  5. at register.RegisterDAO.setRecord(RegisterDAO.java:76 )
  6. at register.RegisterAction.execute(RegisterAction.jav a:42)
  7.  
According to the stacktrace something was null which is shouldn't be. A line 76 in method setRecord of class RegisterDAO. According to your code you showed us there is no code at all at that line. You have to figure that out.

kind regards,

Jos
Aug 4 '09 #4

P: 10
thnks for the reply..
i ll repost after i code the class completely..
thereby clearing all the confusions.
will ask again in time.
Aug 4 '09 #5

P: 10
have chnged the package structure (not imp but thought as well let u knw)
RegisterDAO.java
Expand|Select|Wrap|Line Numbers
  1. package dao;
  2.  
  3. import bean.User;
  4.  
  5. import java.sql.Connection; 
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet; 
  8. import java.sql.SQLException;
  9. import java.util.ArrayList;
  10.  
  11. import javax.sql.DataSource; 
  12.  
  13. public class RegisterDAO {
  14.  
  15.  
  16.     public Connection getConnection(DataSource carsite) { 
  17.         Connection con = null; 
  18.         DataSource ds =carsite; 
  19.         try { 
  20.             con = ds.getConnection(); 
  21.         } catch (SQLException e) { 
  22.             e.printStackTrace(); 
  23.         } 
  24.         return con; 
  25.     }
  26.     public void closeConnection(Connection con) { 
  27.         try { 
  28.             con.close(); 
  29.         } catch (SQLException e) { 
  30.             e.printStackTrace(); 
  31.         } 
  32.     }
  33.     public static User getRecord(Connection con, User user) { 
  34.         ArrayList<User> list= new ArrayList<User>();
  35.         String qry = "select * from register where uid=?"; 
  36.         ResultSet resultSet = null; 
  37.  
  38.         try { 
  39.             PreparedStatement pstat = con.prepareStatement(qry); 
  40.             pstat.setString(10, user.getUid()); 
  41.             resultSet = pstat.executeQuery(); 
  42.             while (resultSet.next()) { 
  43.  
  44.                 user.setFname(resultSet.getString("fname")); 
  45.                 user.setMname(resultSet.getString("mname")); 
  46.                 user.setLname(resultSet.getString("lname")); 
  47.                 user.setDob     (resultSet.getString("dob")); 
  48.                 user.setAdd  (resultSet.getString("address")); 
  49.                 user.setCity (resultSet.getString("city")); 
  50.                 user.setState(resultSet.getString("state"));
  51.                 user.setFname(resultSet.getString("zip"));
  52.                 user.setFname(resultSet.getString("cno"));
  53.                 user.setFname(resultSet.getString("email"));
  54.  
  55.                 list.add(user);
  56.             } 
  57.  
  58.         } catch (SQLException e) { 
  59.             e.printStackTrace(); 
  60.         } return user;
  61.     }
  62.  
  63.  
  64.     public static User setRecord(Connection con, User user) {
  65.  
  66.         String qry = "INSERT INTO register VALUES('" + user.getFname() + "','"
  67.                     + user.getMname() + "','" + user.getLname() + "' ,'" + user.getDob() + " ','" + user.getAdd()
  68.                     + "','" + user.getState() + "' ,'" + user.getZip() + "','" + user.getCno() + "','"
  69.                     + user.getEmail() + "','" + user.getUid() + "','" + user.getPsswd() + "')";
  70.  
  71.         try { 
  72.             System.out.println(qry);
  73.             PreparedStatement pstat = con.prepareStatement(qry);
  74.             pstat.executeUpdate(qry); 
  75.             System.out.println("******************************************");
  76.             System.out.println("Entries are Successfully entered into database");
  77.             }
  78.         catch (SQLException e) { 
  79.                 e.printStackTrace();
  80.         }    
  81.             return user;
  82.  
  83.     }
  84.  
  85. }
  86.  

now uid is my 10th feild in the table thus i have given the value 10 in pstat.setString().
but i m getting an error.

which is

java.sql.SQLException: Parameter index out of range (10 > number of parameters, which is 1).
at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:910)
at com.mysql.jdbc.PreparedStatement.setInternal(Prepa redStatement.java:2715)
at com.mysql.jdbc.PreparedStatement.setString(Prepare dStatement.java:3546)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStat ement.setString(DelegatingPreparedStatement.java:1 31)
at dao.RegisterDAO.getRecord(RegisterDAO.java:40)
at action.RegisterAction.execute(RegisterAction.java: 45)
at org.apache.struts.action.RequestProcessor.processA ctionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process( RequestProcessor.java:226)
at org.apache.struts.action.ActionServlet.process(Act ionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doPost(Acti onServlet.java:415)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11 ConnectionHandler.processConnection(Http11BaseProt ocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.process Socket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThr ead.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlR unnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)


the earlier problems wre solved there was alogical error on my part.
Aug 4 '09 #6

Expert 10K+
P: 11,448
@gavy7210
That PreparedStatement has only one parameter (one unquoted question mark) so you can't set parameter #10; it doesn't matter which column in your table it actually is, you are setting the single parameter i.e. #1 just as the error message clearly says.

kind regards,

Jos
Aug 4 '09 #7

P: 10
hey jos
thnks for your reply.
but can u explain it to me more clearly.
now my aim is to display the data of the logged in user to him(not all users)
for this i have changed the DAO and action classes as below.

RegisterDAO.java
package dao;

import bean.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.sql.DataSource;

public class RegisterDAO {


public Connection getConnection(DataSource carsite) {
Connection con = null;
DataSource ds =carsite;
try {
con = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
public void closeConnection(Connection con) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static ArrayList<User> getRecord(Connection con, User user) {
ArrayList<User> list= new ArrayList<User>();
String qry = "select * from register where username=?";
ResultSet resultSet = null;

try {
PreparedStatement pstat = con.prepareStatement(qry);
pstat.setString(1, user.getUid());
resultSet = pstat.executeQuery();
while (resultSet.next()) {

user.setFname(resultSet.getString("fname"));
user.setMname(resultSet.getString("mname"));
user.setLname(resultSet.getString("lname"));
user.setDob (resultSet.getString("dob"));
user.setAdd (resultSet.getString("address"));
user.setCity (resultSet.getString("city"));
user.setState(resultSet.getString("state"));
user.setZip (resultSet.getString("zip"));
user.setCno (resultSet.getString("cno"));
user.setEmail(resultSet.getString("email"));
user.setUid (resultSet.getString("fname"));
user.setPsswd(resultSet.getString("fname"));


list.add(user);
}

} catch (SQLException e) {
e.printStackTrace();
} return list;
}


public static User setRecord(Connection con, User user) {

int updateQuery= 0;

String qry = "INSERT INTO register VALUES('" + user.getFname() + "','" + user.getMname()
+ "','" + user.getLname() + "' ,'" + user.getDob() + " ','" + user.getAdd()
+ "','" + user.getCity() + "','" + user.getState() + "' ,'" + user.getZip() + "','"
+ user.getCno() + "','" + user.getEmail() + "','" + user.getUid() + "','"
+ user.getPsswd() + "')";

try {
System.out.println(qry);
PreparedStatement pstat = con.prepareStatement(qry);
updateQuery = pstat.executeUpdate();
if (updateQuery != 0)
{
System.out.println("****************************** ************");
System.out.println("Entries are Successfully entered into database");
}
}
catch (SQLException e) {
e.printStackTrace();
}
return user;

}

}
RegisterAction.java
package action;

import java.sql.Connection;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import bean.User;
import dao.RegisterDAO;
import form.RegisterForm;

public class RegisterAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

RegisterDAO registerDAO = new RegisterDAO();
User user = new User();

DataSource ds = getDataSource(request);
Connection con = ds.getConnection();

RegisterForm form1= (RegisterForm)form;

user.setFname(form1.getFname());
user.setMname(form1.getMname());
user.setLname(form1.getLname());
user.setDob(form1.getDob());
user.setAdd(form1.getAdd());
user.setAdd(form1.getCity());
user.setCity(form1.getCity());
user.setState(form1.getState());
user.setZip(form1.getZip());
user.setCno(form1.getCno());
user.setEmail(form1.getEmail());
user.setUid(form1.getUid());
user.setPsswd(form1.getPsswd());

user= RegisterDAO.setRecord(con,user);
ArrayList<User> list= RegisterDAO.getRecord(con,user);
request.setAttribute("userinfo", list);

con.setAutoCommit(true);
registerDAO.closeConnection(con);



return mapping.findForward("success");
}
}
viewreginfo.jsp
<%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/tlds/struts-logic.tld" prefix="logic" %>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>The Stored User Information</title>
</head>
<body background="nexus.jpg" >
<div id="top">
<table border="0">
<tr>
<td width="250">
<img src="car.jpg"/> </td>
<td width="250"> <h1 align="center"><font face="courier new" color="white">thecarsite</font></h1> </td>
<td width="250"> <h2 align="right"><html:link forward="index">Home</html:link></h2></td>
</tr>
</table>
<hr width="100%">
</div>
The Account information for user
<div id="restpage" >

<logic:iterate id="result" name="form1" property="userinfo">
<bean:write name="result" />
</logic:iterate>

</div>
</body>
</html>

and am replyin late cuz it was a holiday yesterday(an imp hindu festival called Rakhi).

hoping for guidance
Gaurav
Aug 6 '09 #8

Expert 10K+
P: 11,448
@gavy7210
You should read the API documentation for the PreparedStatement interface; it explains it all.

kind regards,

Jos
Aug 6 '09 #9

P: 10
ohk i shall :)
regards
gaurav
Aug 6 '09 #10

Post your reply

Sign in to post your reply or Sign up for a free account.