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>

Comments

Popular posts from this blog

Write a java program to create Teacher table(TNo.TName, Sal, Desg) and insert a record in it.

Create a JSP page to accept a number from an user and display it in words: Example: 123 – One Two Three. The output should be in red color.

Write a multithreading program in java to display all the vowels from a given String.(Use Thread Class)