473,734 Members | 2,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to retrieve and add data to a database

10 New Member
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.ja va
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.jav a
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.NullP ointerException
Aug 4, 2009 10:28:14 AM org.apache.cata lina.core.Stand ardWrapperValve invoke
SEVERE: Servlet.service () for servlet action threw exception
java.lang.NullP ointerException
at register.Regist erDAO.setRecord (RegisterDAO.ja va:76)
at register.Regist erAction.execut e(RegisterActio n.java:42)
at org.apache.stru ts.action.Reque stProcessor.pro cessActionPerfo rm(RequestProce ssor.java:421)
at org.apache.stru ts.action.Reque stProcessor.pro cess(RequestPro cessor.java:226 )
at org.apache.stru ts.action.Actio nServlet.proces s(ActionServlet .java:1164)
at org.apache.stru ts.action.Actio nServlet.doPost (ActionServlet. java:415)
at javax.servlet.h ttp.HttpServlet .service(HttpSe rvlet.java:709)
at javax.servlet.h ttp.HttpServlet .service(HttpSe rvlet.java:802)
at org.apache.cata lina.core.Appli cationFilterCha in.internalDoFi lter(Applicatio nFilterChain.ja va:252)
at org.apache.cata lina.core.Appli cationFilterCha in.doFilter(App licationFilterC hain.java:173)
at org.apache.cata lina.core.Stand ardWrapperValve .invoke(Standar dWrapperValve.j ava:213)
at org.apache.cata lina.core.Stand ardContextValve .invoke(Standar dContextValve.j ava:178)
at org.apache.cata lina.core.Stand ardHostValve.in voke(StandardHo stValve.java:12 6)
at org.apache.cata lina.valves.Err orReportValve.i nvoke(ErrorRepo rtValve.java:10 5)
at org.apache.cata lina.core.Stand ardEngineValve. invoke(Standard EngineValve.jav a:107)
at org.apache.cata lina.connector. CoyoteAdapter.s ervice(CoyoteAd apter.java:148)
at org.apache.coyo te.http11.Http1 1Processor.proc ess(Http11Proce ssor.java:869)
at org.apache.coyo te.http11.Http1 1BaseProtocol$H ttp11Connection Handler.process Connection(Http 11BaseProtocol. java:664)
at org.apache.tomc at.util.net.Poo lTcpEndpoint.pr ocessSocket(Poo lTcpEndpoint.ja va:527)
at org.apache.tomc at.util.net.Lea derFollowerWork erThread.runIt( LeaderFollowerW orkerThread.jav a:80)
at org.apache.tomc at.util.threads .ThreadPool$Con trolRunnable.ru n(ThreadPool.ja va:684)
at java.lang.Threa d.run(Unknown Source)
Aug 4 '09 #1
9 5559
numberwhun
3,509 Recognized Expert Moderator Specialist
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
gavy7210
10 New Member
thank you jeff..
i was confused so as to where to put it.
Aug 4 '09 #3
JosAH
11,448 Recognized Expert MVP
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
gavy7210
10 New Member
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
gavy7210
10 New Member
have chnged the package structure (not imp but thought as well let u knw)
RegisterDAO.jav a
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.SQLExc eption: Parameter index out of range (10 > number of parameters, which is 1).
at com.mysql.jdbc. SQLError.create SQLException(SQ LError.java:910 )
at com.mysql.jdbc. PreparedStateme nt.setInternal( PreparedStateme nt.java:2715)
at com.mysql.jdbc. PreparedStateme nt.setString(Pr eparedStatement .java:3546)
at org.apache.tomc at.dbcp.dbcp.De legatingPrepare dStatement.setS tring(Delegatin gPreparedStatem ent.java:131)
at dao.RegisterDAO .getRecord(Regi sterDAO.java:40 )
at action.Register Action.execute( RegisterAction. java:45)
at org.apache.stru ts.action.Reque stProcessor.pro cessActionPerfo rm(RequestProce ssor.java:421)
at org.apache.stru ts.action.Reque stProcessor.pro cess(RequestPro cessor.java:226 )
at org.apache.stru ts.action.Actio nServlet.proces s(ActionServlet .java:1164)
at org.apache.stru ts.action.Actio nServlet.doPost (ActionServlet. java:415)
at javax.servlet.h ttp.HttpServlet .service(HttpSe rvlet.java:709)
at javax.servlet.h ttp.HttpServlet .service(HttpSe rvlet.java:802)
at org.apache.cata lina.core.Appli cationFilterCha in.internalDoFi lter(Applicatio nFilterChain.ja va:252)
at org.apache.cata lina.core.Appli cationFilterCha in.doFilter(App licationFilterC hain.java:173)
at org.apache.cata lina.core.Stand ardWrapperValve .invoke(Standar dWrapperValve.j ava:213)
at org.apache.cata lina.core.Stand ardContextValve .invoke(Standar dContextValve.j ava:178)
at org.apache.cata lina.core.Stand ardHostValve.in voke(StandardHo stValve.java:12 6)
at org.apache.cata lina.valves.Err orReportValve.i nvoke(ErrorRepo rtValve.java:10 5)
at org.apache.cata lina.core.Stand ardEngineValve. invoke(Standard EngineValve.jav a:107)
at org.apache.cata lina.connector. CoyoteAdapter.s ervice(CoyoteAd apter.java:148)
at org.apache.coyo te.http11.Http1 1Processor.proc ess(Http11Proce ssor.java:869)
at org.apache.coyo te.http11.Http1 1BaseProtocol$H ttp11Connection Handler.process Connection(Http 11BaseProtocol. java:664)
at org.apache.tomc at.util.net.Poo lTcpEndpoint.pr ocessSocket(Poo lTcpEndpoint.ja va:527)
at org.apache.tomc at.util.net.Lea derFollowerWork erThread.runIt( LeaderFollowerW orkerThread.jav a:80)
at org.apache.tomc at.util.threads .ThreadPool$Con trolRunnable.ru n(ThreadPool.ja va:684)
at java.lang.Threa d.run(Unknown Source)


the earlier problems wre solved there was alogical error on my part.
Aug 4 '09 #6
JosAH
11,448 Recognized Expert MVP
@gavy7210
That PreparedStateme nt 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
gavy7210
10 New Member
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.jav a
package dao;

import bean.User;

import java.sql.Connec tion;
import java.sql.Prepar edStatement;
import java.sql.Result Set;
import java.sql.SQLExc eption;
import java.util.Array List;

import javax.sql.DataS ource;

public class RegisterDAO {


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

try {
PreparedStateme nt pstat = con.prepareStat ement(qry);
pstat.setString (1, user.getUid());
resultSet = pstat.executeQu ery();
while (resultSet.next ()) {

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


list.add(user);
}

} catch (SQLException e) {
e.printStackTra ce();
} return list;
}


public static User setRecord(Conne ction 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.prin tln(qry);
PreparedStateme nt pstat = con.prepareStat ement(qry);
updateQuery = pstat.executeUp date();
if (updateQuery != 0)
{
System.out.prin tln("********** *************** *************** **");
System.out.prin tln("Entries are Successfully entered into database");
}
}
catch (SQLException e) {
e.printStackTra ce();
}
return user;

}

}
RegisterAction. java
package action;

import java.sql.Connec tion;
import java.util.Array List;

import javax.servlet.h ttp.HttpServlet Request;
import javax.servlet.h ttp.HttpServlet Response;
import javax.sql.DataS ource;

import org.apache.stru ts.action.Actio n;
import org.apache.stru ts.action.Actio nForm;
import org.apache.stru ts.action.Actio nForward;
import org.apache.stru ts.action.Actio nMapping;

import bean.User;
import dao.RegisterDAO ;
import form.RegisterFo rm;

public class RegisterAction extends Action {

public ActionForward execute(ActionM apping mapping, ActionForm form,
HttpServletRequ est request, HttpServletResp onse response)
throws Exception {

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

DataSource ds = getDataSource(r equest);
Connection con = ds.getConnectio n();

RegisterForm form1= (RegisterForm)f orm;

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

user= RegisterDAO.set Record(con,user );
ArrayList<User> list= RegisterDAO.get Record(con,user );
request.setAttr ibute("userinfo ", list);

con.setAutoComm it(true);
registerDAO.clo seConnection(co n);



return mapping.findFor ward("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="te xt/html; charset=ISO-8859-1"
pageEncoding="I SO-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="nex us.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">t hecarsite</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:iterat e id="result" name="form1" property="useri nfo">
<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
JosAH
11,448 Recognized Expert MVP
@gavy7210
You should read the API documentation for the PreparedStateme nt interface; it explains it all.

kind regards,

Jos
Aug 6 '09 #9
gavy7210
10 New Member
ohk i shall :)
regards
gaurav
Aug 6 '09 #10

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

Similar topics

16
3011
by: Daniel Tonks | last post by:
First, please excuse the fact that I'm a complete MySQL newbie. My site used forum software that I wrote myself (in Perl) which, up until now, has used flat files. This worked fine, however lately I've been wanting to do more stuff with user accounts, and had been eying MySQL for over a year. Finally I've decided to start off small by converting the forum's account system to a MySQL database (and convert the rest later after I'm...
4
3938
by: Claudia Fong | last post by:
Hi, I have a form where I need to display the data of a department that stores in a access db. When I first load the form it will call a function name loaddata which will get the first record from the db and then display for the user: oleDbConnection1.Open();
5
2931
by: Roy Gourgi | last post by:
Hi, I am used to working in Visual FoxPro and I would like to be able to create a database and store and retrieve information from it. What is the simplest way to do it and what should I be using as there are many choices to choose from. My database will contain a lot of records. TIA
5
2437
by: ggk517 | last post by:
We are trying to develop an Engineering application using PHP, Javascript with Informix as the back-end. Is it possible to retrieve data using Javascript but by accessing the Database. Say somebody enters part_no, than using Javascript is it possible to connect to the part master and retrieve the division and desc information? I am not allowed to use the PHP because this will require the user to insert the part number on the first...
13
3433
by: kev | last post by:
Hi all, I have created a database for equipments. I have a form to register the equipment meaning filling in all the particulars (ID, serial, type, location etc). I have two buttons at the end of the form which is submit and cancel. After i have clicked submit, the information is stored directly into my corresponding database table. My problem here is i need to retrieve back the information submitted to display all the data that the...
3
5058
by: Alfred | last post by:
I want to post text field data from these HTML TEXTAREA tags to a PostgreSQL database and have it reappear back on another page exactly as I had typed it. Over the years I have done this but only did it with simple text. This time around, I want to handle much more complex text. I need to preserve some kinds of features. (Yes, I have turned off magic quotes.) - Need to strip diacritics. I learned I could use htmlentities to catch...
1
1923
by: Mel | last post by:
Anyone know how I would retrieve the MaxLength property of a column in my Access Database table? I know how to retrieve table data, for example the "Quote #" field in my example code below, but I have never attempted to access a property of a field. 'Beginning of my Code Example Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\support\webbmq.mdb;" Dim strSel As String = "SELECT * FROM " Dim conWebOrdNum As New...
5
2088
by: fruityfreak | last post by:
I would like to know how to I retrieve information from another database table... Instead of keying in manually, I would like to retrieve data directly from the logged in user row. For the user table, i have outletid in it. Outlet ID: <input type="text" name="outletid"> (Manually) Outlet ID: <?php echo $row; ?> (Is this how it looks like if I want to retrieve data another table) I think it has got to do with join table but I'm not...
0
1433
by: tigger | last post by:
Hi there, I'm having problems retrieving data from Access database using Visual basic 2005. My database is called Actual_Database and it has a few tables. One of them is called Threshold Database. I'm trying to retrieve data from Threshold Database. In that table, it has columns of AC250L, AC250R, BC250L, BC250R, AC500L etc.. When i select a name from the combo box, and click presentButton, it will show a smile if the values from...
21
2895
by: giandeo | last post by:
Hello Experts. Is it possible to retrieve the value from a populated pull down menu from a database and then use that value to access the same database to get the related fields. Example: Database name: sp Table: importer Field names: imp_code, imp_name, imp_address, imp_tel
0
9449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9182
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.