Write a JSP program to accept the details of Account (ANo, Type, Bal) and store it into database and display it in tabular form. (Use PreparedStatement interface)
SaveAccount.html
<html>
<body>
<form method=get action="saveAccount.jsp">
Enter Account No. : <input type=text name=ano><br><br>
Enter Account Type:<input type=text name=type><br><br>
Enter Balance : <input type=text name=bal><br><br>
<input type=submit value="Save">
</form>
</body>
</html>
saveAccount.jsp
<html>
<body>
<%@ page import="java.sql.*;" %>
<%! int ano,bal;
String type; %>
<%
ano=Integer.parseInt(request.getParameter("ano"));
type=request.getParameter("type");
bal=Integer.parseInt(request.getParameter("bal"));
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn=DriverManager.getConnection("jdbc:odbc:acnt","","");
PreparedStatement s=cn.prepareStatement("insert into Account values(?,?,?)");
s.setInt(1,ano);
s.setString(2,type);
s.setInt(3,bal);
s.executeUpdate();
out.println("Record is saved");
Statement st=cn.createStatement();
ResultSet rs=st.executeQuery("select * from Account");
%>
<table border="1" width="40%">
<% while(rs.next())
{
%>
<tr> <td><%= rs.getInt("ano") %></td>
<td><%= rs.getString("type") %></td>
<td><%= rs.getInt("bal") %></td>
</tr>
<%
}
cn.close();
}catch(Exception e)
{
out.println(e);
}
%>
</body>
</html>
<html>
<body>
<form method=get action="saveAccount.jsp">
Enter Account No. : <input type=text name=ano><br><br>
Enter Account Type:<input type=text name=type><br><br>
Enter Balance : <input type=text name=bal><br><br>
<input type=submit value="Save">
</form>
</body>
</html>
saveAccount.jsp
<html>
<body>
<%@ page import="java.sql.*;" %>
<%! int ano,bal;
String type; %>
<%
ano=Integer.parseInt(request.getParameter("ano"));
type=request.getParameter("type");
bal=Integer.parseInt(request.getParameter("bal"));
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn=DriverManager.getConnection("jdbc:odbc:acnt","","");
PreparedStatement s=cn.prepareStatement("insert into Account values(?,?,?)");
s.setInt(1,ano);
s.setString(2,type);
s.setInt(3,bal);
s.executeUpdate();
out.println("Record is saved");
Statement st=cn.createStatement();
ResultSet rs=st.executeQuery("select * from Account");
%>
<table border="1" width="40%">
<% while(rs.next())
{
%>
<tr> <td><%= rs.getInt("ano") %></td>
<td><%= rs.getString("type") %></td>
<td><%= rs.getInt("bal") %></td>
</tr>
<%
}
cn.close();
}catch(Exception e)
{
out.println(e);
}
%>
</body>
</html>
Comments
Post a Comment