Search This Blog

Thursday, 4 September 2014

11. HTML and JSP Program to Read Student Information and Store into Database.

Filename: Stud11.html
<!-- 11. Program to read student information and store into database -->
<html>
 <head>

 <title>Student Information</title>
 </head>
 <body>
 <h3>Enter Student Details:<h3>
 <form action="Stud11.jsp" method="POST" >
 <table >
 <tr><td>Student Name:</td><td><input type="text" name="sname" autofocus></td></tr>
 <tr><td>Register Number:</td><td><input type="text" name="regno"></td></tr>
 <tr><td>Course:</td><td><input type="text" name="course"></td></tr>
 <tr><td>College:</td><td><input type="text" name="college"></td></tr>
 <tr><td>City:</td><td><input type="text" name="city"></td></tr>
 <tr><td>Phone:</td><td><input type="text" name="phone"></td></tr>
 <tr><td><input type="submit" value="Store in Database"></td><td><input type="reset" value="Reset"></td></tr>
 </table>
 
 </body>
</html>


Filename:Stud11.jsp
<!-- 11. JSP Program to store student information in the database. -->
<html>
<head>
<title>Student Information</title>
<%@page import="java.sql.*,java.util.*"%>
</head>
<body>
<%
    String sname=request.getParameter("sname");
    String regno=request.getParameter("regno");
    String course=request.getParameter("course");
    String college=request.getParameter("college");
    String city=request.getParameter("city");
    String phone=request.getParameter("phone");
    int len=phone.length();
   
   
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:stud1");
    Statement st=con.createStatement();
    if(len<=10)
    {
        try
        {
            out.println("Data successfully inserted.");
            String s="insert into Student values('"
            +sname+"','"+regno+"','"+course+"','"+college+"','"+city+"','"+phone+"');";
                st.executeQuery(s);
       
        }
        catch(Exception e)
        {
            //out.println("Insertion of data failed!");
            //out.println(e);
        }
    }
    else
    {
        out.println("Invalid Phone Number");
    }

%>

No comments :

Post a Comment

FREE Hit Counters