Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to insert input parameter in database through Java Bean  RSS feed

 
vijay kumar dahiya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.java(It�s a Java Bean) 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:
<html>
<head>
<title>A simple JSP application</title>
<head>
<body>
<form method="get" action="tmp" >
Name: <input type="text" name="user">
Password: <input type="password" name="pass">
<input type="Submit" value="Submit">
</form>
</body>
</html>

LoginServlet.java:
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException{
try
{
String user=request.getParameter("user");
String pass=request.getParameter("pass");

co.SimpleBean st = new co.SimpleBean();

st.setUserName(user);
st.setPassword(pass);


request.setAttribute("user",st);
request.setAttribute("pass",st);
RequestDispatcher dispatcher1 =request.getRequestDispatcher("submit.jsp");
dispatcher1.forward(request,response);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
SimpleBean.java:
package co;
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;

public class SimpleBean {

private String user="";
private String pass="";
private String s="";

public String getUserName() {
return user;
}
public void setUserName(String user) {
this.user = user;
}
public String getPassword() {
return pass;
}
public void setPassword(String pass) {
this.pass = pass;
}

public String getIssueData() //method that create connection with database
{
try
{
System.out.println("Printed*************************************************************");
getUserName();
getPassword();

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Loading....");
Connection con=DriverManager.getConnection("jdbcdbc:simple");
System.out.println("Connected....");
PreparedStatement st=con.prepareStatement("insert into Table1 values(?,?)");
System.out.println("~~~~~~~~~~~~~~~~~~~~");
String User=getUserName();
st.setString(1,User);
String Password=getPassword();
st.setString(2,Password);
st.executeUpdate();
System.out.println("Query Executed");
con.close();
s= "Your logging is saved in DB ";
System.out.println("Your logging is saved in DB *****************");
return(s);
}
catch(Exception e)
{
e.printStackTrace();
return "failed";
}
}
}

submit.jsp:
This is Submit page

<html><body>
Hello
Student Name: <%= ((co.SimpleBean)request.getAttribute("user")).getUserName() %>
<br>
Password: <%= ((co.SimpleBean)request.getAttribute("pass")).getPassword() %>
<br>
<jsp:useBean id="st" class="co.SimpleBean" scope="request" />
<jsp:getProperty name="st" property="IssueData" />
</body></html>

web.xml:
<web-app>

<servlet>
<servlet-name>one</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>one</servlet-name>
<url-pattern>/tmp</url-pattern>
</servlet-mapping>

<jsp-file>issue.jsp</jsp-file>
<jsp-file>submit.jsp</jsp-file>
<url-pattern>*.do</url-pattern>

<welcome-file-list>
<welcome-file>Login.html</welcome-file>
</welcome-file-list>

</web-app>

I think property value of Java Bean are mismatching.
Please Help me..Thanks.!!!
--
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check your private messages for an important administrative matter.

Also, please be sure to use UBB code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the .
 
Shawny Starky
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you should reference
your getUser() and getPassword()
in the been with this.getUser()
and this.getPassword. Not sure
if the syntax is correct as my
mind is wrapped around PHP right
now. I am pretty sure that
is your problem.

use System.out.println()
to print to the stdOut log located
in the log folder in your Tomcat
installation directory. If you are
using Tomcat that is. This way you can
check within the been if the methods
getPass()and getUsr() are returning
anything before you continue processing
code in the servlet.
[ November 28, 2008: Message edited by: Shawny Starky ]
 
Sa'eed Waseem
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest hard-coding your "user" and "pass" variables in the SimpleBean class to some default values and see it these default values get written to the database. If yes, then there is something wrong with your logic and you are hitting the database before setting "user" and "pass". If no, then your database code needs fixing.

that's more of troubleshooting than a resolution


cheers
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!