Sunday, April 19, 2009

Write a program to display a user details form to the user

Write a program to display a user details form to the user. After the user clicks Submit button, the details entered should be saved in the database. Display a message to the user after the data is saved to the database. The DSN name is user. The database name is Details. Identify the structure of UserDetails table:

CNo Number,
Fname Text,
Lname Text,
Email Text

The files used to run the application are:

1. Details.jsp
2. insert.jsp

Solution:
Details.jsp

<html>
<head>
<title>Add Customer Details</title>
</head>

<body>
<h1> User Details</h1>
<form action="insert.jsp" method="post">
<table>
<tr>
<td align="right">First Name:</td>
<td><input type="text" name="first" size="30"></td>
</tr>
<tr>
<td>Last Name:</td>
<td> <input type="text" name="last" size="30" /></td>
</tr>

<tr>
<td>Email:</td>
<td> <input type="text" name="email" size="30" /></td>
</tr>
</table><br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>

insert.jsp

<html>
<head><title>Adding customer details</title></head>
<%@ page import="java.io.*, java.sql.*"%>
<body>
<center>

<%
String first = request.getParameter("first");
String last = request.getParameter("last");
String email = request.getParameter("email");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection =
DriverManager.getConnection("jdbc:odbc:Details");

Statement Stmt = connection.createStatement();
String insert = "insert into UserDetails values ('" + first + "','" + last + "','" + email + "');";

int stmtInt = Stmt.executeUpdate(insert);
out.println(“Your Information is Added in our
Database”);
%>

<%
}
catch (ClassNotFoundException cnfe)
{
System.err.println(cnfe);
}
catch (SQLException ex )
{
System.err.println( ex);
}
catch (Exception er)
{
er.printStackTrace();
}
%>
</body>
</html>

The output of the program is as shown in the Figure 14.4.

Figure 14.4: User Details Form

After entering the details, when the user clicks on Submit button, the details are saved in database, and a message is displayed to the user as shown in Figure 14.5.

Figure 14.5: Message

0 nhận xét: