Am a novice and i have been trying to write a simple program
to save three html form data ( name, email, age) to an access
database using javabean and jsp. After days of struggling and
reading tutorials from the net, i have succeded in debuging all
error messages. my program is runing but the form data entered
is not being saved into the access database. can some one examine
my codes and tell me what to do and where I went wrong. yur help
will be very much appreciated.
Thanks
Note: The progame is made up of five files A. B, C, D and E as ff:
A. Database (ms access databas)
Filename: savename --- Created with MS ACCESS 2007
table: tblname
nameid Autonumber Primary key
name Char(50)
email Char(80)
age Int
B. HTML FORM
Filename: index.html
Coded as ff:
<HTML>
<BODY>
<FORM METHOD=POST ACTION="saveName.jsp">
<table width="290" height="40" align="center">
<tr>
<td>Enter your name?</td>
<td></td>
<td><INPUT TYPE=TEXT NAME=name SIZE=20></td>
<tr>
<tr>
<td>Enter your email?</td>
<td></td>
<td><INPUT TYPE=TEXT NAME=email SIZE=20></td>
<tr>
<tr>
<td>Enter your age?</td>
<td></td>
<td><INPUT TYPE=TEXT NAME=age SIZE=4></td>
<tr>
<tr align="center">
<td></td>
<td></td>
<td><P><INPUT TYPE=SUBMIT value="Save"></td>
</tr>
</table>
</FORM>
</BODY>
</HTML>
C. FORMBEAN
Filename:userData.java
coded as:
package getname;
public class userData {
String name;
String email;
int age;
public void setName( String myname )
{
this.name = myname;
}
public void setEmail( String mymail )
{
this.email = mail;
}
public void setAge( int myage )
{
this.age = myage;
}
public String getName()
{
return name;
}
public String getEmail()
{
return email;
}
public int getAge()
{
return age;
}
}
D. DATA ACCESS JAVABEAN
filename: dbbean.java
Codes:
package getname;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.net.*;
public class dbbean
{
String name=null;
String email=null;
int age;
Connection con = null;
datasource dsndb="savename";
String url = "jdbcdbc:dsndb";
String driver = "jdbc.odbc.JdbcOdbcDriver";
public void dbbean()
{
try{
Class.forName(driver);
con = DriverManager.getConnection(url,"","");
try{
Statement st = con.createStatement();
int val = st.executeUpdate("INSERT INTO tblname(name, email, age) VALUES(?,?,?)");
}
catch (SQLException s)
{
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
E. Action jsp file
1.
filename: saveName.jsp
<jsp:useBean id="userform" class="getname.userData" scope="page"/>
<jsp:setProperty name="userform" property="*"/>
<jsp:useBean id="userdatabase" class="getname.dbbean" scope="request"/>
<jsp:setProperty name="userdatabase" property="*"/>
<HTML>
<BODY align="center">
<A HREF="NextPage.jsp">Continue</A>
</BODY>
</HTML>
2.
filename: nextPage.jsp
code:
<jsp:useBean id="userform" class="getname.UserData" scope="session"/>
<HTML>
<P>
<P>
<p>
<BODY align="center">
Hello,<BR>
Your Name is: <jsp:getProperty name="userform" property="username" /><BR>
Your Email is: <jsp:getProperty name="userform" property="email" /><BR>
Your Age is: <jsp:getProperty name="userform" property="age" /><BR>
<p>
<p><A HREF="index.jsp">Next Record</>
</BODY>
</HTML>