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

Need Help-How to insert input parameter in DB through Java Bean

P: 11
Hello Sir,
I have a simple Issue but It is not resolve by me i.e input parameter
are not store in Ms-Access.
I store the input parameter through Standard Action <jsp:useBean>.
jsp:useBean call a property IssueData. this property exist in
SimpleBean which create a connection from DB and insert the data.

At run time servlet and server also show that loggging are saved in DB.
But when I open the table in Access. Its empty.
Ms-Access have two fields- User, Password both are text type.
Please review these code:
login.html:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>A simple JSP application</title>
  4. <head>
  5. <body>
  6. <form method="get" action="tmp" >
  7. Name: <input type="text" name="user">
  8. Password: <input type="password" name="pass">
  9. <input type="Submit" value="Submit">
  10. </form>
  11. </body>
  12. </html>
LoginServlet.java:

Expand|Select|Wrap|Line Numbers
  1. import javax.servlet.*;
  2. import javax.servlet.http.*;
  3. public class LoginServlet extends HttpServlet{
  4. public void doGet(HttpServletRequest request, HttpServletResponse response)
  5. throws ServletException{
  6. try
  7. {
  8. String user=request.getParameter("user");
  9. String pass=request.getParameter("pass");
  10.  
  11. co.SimpleBean st = new co.SimpleBean();
  12.  
  13. st.setUserName(user);
  14. st.setPassword(pass);
  15.  
  16.  
  17. request.setAttribute("user",st);
  18. request.setAttribute("pass",st);
  19. RequestDispatcher dispatcher1 =request.getRequestDispatcher("submit.jsp");
  20. dispatcher1.forward(request,response);
  21. }
  22. catch(Exception e)
  23. {
  24. e.printStackTrace();
  25. }
  26. }
  27. }
SimpleBean.java:

Expand|Select|Wrap|Line Numbers
  1. package co;
  2. import java.util.*;
  3. import java.io.*;
  4. import javax.servlet.*;
  5. import javax.servlet.http.*;
  6. import java.sql.*;
  7. import java.util.*;
  8.  
  9. public class SimpleBean {
  10.  
  11. private String user="";
  12. private String pass="";
  13. private String s="";
  14.  
  15. public String getUserName() {
  16. return user;
  17. }
  18. public void setUserName(String user) {
  19. this.user = user;
  20. }
  21. public String getPassword() {
  22. return pass;
  23. }
  24. public void setPassword(String pass) {
  25. this.pass = pass;
  26. }
  27.  
  28. public String getIssueData() //method that create connection with database
  29. {
  30. try
  31. {
  32. System.out.println("Printed*************************************************************");
  33. getUserName();
  34. getPassword();
  35.  
  36. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  37. System.out.println("Loading....");
  38. Connection con=DriverManager.getConnection("jdbc:odbc:simple");
  39. System.out.println("Connected....");
  40. PreparedStatement st=con.prepareStatement("insert into Table1 values(?,?)");
  41. System.out.println("~~~~~~~~~~~~~~~~~~~~");
  42. String User=getUserName();
  43. st.setString(1,User);
  44. String Password=getPassword();
  45. st.setString(2,Password);
  46.  st.executeUpdate();
  47. System.out.println("Query Executed");
  48.  con.close();
  49.  s=  "Your logging is saved in DB ";
  50. System.out.println("Your logging is saved in DB *****************");
  51. return(s);
  52. }
  53. catch(Exception e)
  54. {
  55. e.printStackTrace();
  56. return "failed";
  57. }
  58. }
  59. }
  60.  
submit.jsp:
This is Submit page

Expand|Select|Wrap|Line Numbers
  1. <html><body>
  2. Hello
  3. Student Name: <%= ((co.SimpleBean)request.getAttribute("user")).getUserName() %>
  4. <br>
  5. Password: <%= ((co.SimpleBean)request.getAttribute("pass")).getPassword() %>
  6. <br>
  7. <jsp:useBean id="st" class="co.SimpleBean" scope="request" />
  8. <jsp:getProperty name="st" property="IssueData" />
  9. </body></html>
web.xml:

Expand|Select|Wrap|Line Numbers
  1. <web-app>
  2.  
  3. <servlet>
  4. <servlet-name>one</servlet-name>
  5. <servlet-class>LoginServlet</servlet-class>
  6. </servlet>
  7.  
  8. <servlet-mapping>
  9. <servlet-name>one</servlet-name>
  10. <url-pattern>/tmp</url-pattern>
  11. </servlet-mapping>
  12.  
  13. <jsp-file>issue.jsp</jsp-file>
  14. <jsp-file>submit.jsp</jsp-file>
  15. <url-pattern>*.do</url-pattern>
  16.  
  17. <welcome-file-list>
  18. <welcome-file>Login.html</welcome-file>
  19. </welcome-file-list>
  20.  
  21. </web-app>


Please Help me..Thanks.!!!
--
Oct 18 '08 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 392
It does not look like you are calling the getIssueData() method any where. Since that is where the code is that writes the data to the database if you do not call it the data will never be written.
Oct 20 '08 #2

P: 11
It does not look like you are calling the getIssueData() method any where. Since that is where the code is that writes the data to the database if you do not call it the data will never be written.
Dear Sir,
Can you write me How that code will be invoke which write in getIssueData().
Please Help me...
Oct 21 '08 #3

Post your reply

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