1.11M Members

How Input parameter insert in DB through Java Bean

 
0
 

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:

<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("jdbc:odbc: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>

Please Help me..Thanks.!!!

 
0
 

Is the connection in autoCommit mode? If not, then you need to explicitly commit the transaction. Check the return value of the executeUpdate ; is it 1?

Does a standalone program which inserts some sample data in the table works?

 
0
 

Is the connection in autoCommit mode? If not, then you need to explicitly commit the transaction. Check the return value of the executeUpdate ; is it 1?

Does a standalone program which inserts some sample data in the table works?

Dear Sir,
I create a simple servlet program and create a table to insert the value in database.Database is sucessfully updated..

 
0
 

What about the first two questions? What return value do you get?

 
0
 

Dear Sir,
I create a simple servlet program to check database are updated.Its Succesfull.
But my these problem are not resolved.Please help me...

I want to 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 when I click on submit button 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, Pass both are text type.
Please review these code:
login.html:

<html>
<head>
<title>A simple JSP application</title>
<script language=javascript>
function f(k)
{
document.forms['frm'].mykey.value=k;
document.forms['frm'].submit();
}
</script>
<head>
<body>
<form method="get" action="tmp" name="frm">
Name: <input type="text" name="User">
Password: <input type="password" name="Pass">
<input type=hidden name="mykey" value="">
<input type="button" value="Submit" onclick="f('submit.jsp')">
<input type="button" value="Issue" onclick="f('issue.jsp')">
</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.setUser(User);
st.setPass(Pass);
 
 
request.setAttribute("User",st);
request.setAttribute("Pass",st);
RequestDispatcher dispatcher1 =request.getRequestDispatcher("/"+request.getParameter("mykey"));
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 SimpleBean(){}
 
public String getUser() {
return User;
}
public void setUser(String User) {
this.User = User;
}
public String getPass() {
return Pass;
}
public void setPass(String Pass) {
this.Pass = Pass;
}
 
public String getissueData() //method that create connection with database
{
try
{
System.out.println("Printed*************************************************************");
getUser();
getPass();
 
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Loading....");
Connection con=DriverManager.getConnection("jdbc:odbc:simple");
System.out.println("Connected....");
PreparedStatement st=con.prepareStatement("insert into Table1 values(?,?)");
System.out.println("~~~~~~~~~~~~~~~~~~~~");
String User=getUser();
st.setString(1,User);
String Pass=getPass();
st.setString(2,Pass);
 int y= st.executeUpdate();
System.out.println(y);
System.out.println("Query Executed");
con.commit();
 con.close();
 s=  "Your logging is saved in DB ";
System.out.println("Your logging is saved in DB *****************");
return this.s;
}
catch(Exception e)
{
e.printStackTrace();
return "failed";
}
}
}

submit.jsp:
This is Submit page

<html><body>
Hello
Student Name: <%= ((co.SimpleBean)request.getAttribute("User")).getUser() %>
<br>
Password: <%= ((co.SimpleBean)request.getAttribute("Pass")).getPass() %>
<br>
 
<jsp:useBean id="st" class="co.SimpleBean" scope="request"/>
<jsp:setProperty name="st" property="User" value="request.getParamaeter("Pass")"/>
        <jsp:setProperty name="st" property="Pass" value="request.getParamaeter("Pass")"/>
    
   <jsp:getProperty name="st" property="issueData"/>
 
<% st.getissueData(); %> 
</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>
 
0
 

is this IssueData or getIssueData() in jsp page?i don't know this is correct..but sometimes it will work

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article