JSP and Database


JSP AND DATABASE

 

AIM:

To write a program to update a database and display it using JSP.

 

ALGORITHM:

1.    Start the program.

2.    Create the database with the required fields.

3.    Create a Connection type object variable "con" that will hold Connection type object, a ResultSet type object variable "rst" that will hold a result set returned by a database query and a Statement type object variable "stmt".

4.    Load database driver and make a connection

5.    A Statement object is created by calling createStatement() method on connection object con. 

6.    After creating a Statement, a method executeQuery() or  executeUpdate() is called on Statement object stmt and a SQL query string is passed in method executeQuery() or  executeUpdate().

7.    This will return a ResultSet rst related to the query string

8.    Read the values from the ResultSet and display the information

9.    Stop the program

 

 

PROGRAM:

<%@ page language="java" import="java.sql.*" %>

 

<%

     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

     Connection con=null;

     ResultSet rst=null;

     Statement stmt=null;

 

try

{

     String url="jdbc:odbc:DB1";

     con=DriverManager.getConnection(url);

     stmt=con.createStatement();

}  

catch(Exception e)

{

     System.out.println(e.getMessage());             

}

 

     if((request.getParameter("action")).compareTo("list")==0)

     {

          String c=request.getParameter("cd");

          String d=request.getParameter("dt");

          stmt.executeUpdate("insert into DC(dept,code) values('"+d+"','"+c+"')");

          rst=stmt.executeQuery("select * from DC");

     }

%>

 

<html>

<body>

<center>

     <h2>Department Details</h2>

     <table border="1" cellspacing="0" cellpadding="0">

          <tr>

              <td><b>S.No</b></td>

              <td><b>Code</b></td>

              <td><b>Dept</.b></td>

          </tr>

     <%

          int no=1;

          while(rst.next())

          {%>

          <tr>

              <td><%=no%></td>

              <td><%=rst.getString("code")%></td>

              <td> <%=rst.getString("dept")%> </td>

          </tr>

          <%    no++;

          }

          rst.close();

          stmt.close();

          con.close();

%>

</table>

</center>

</body>

</html>

 

<%}else{%>

<html>

<head>

<title>Department code Entry Form Document</title>

<script language="javascript">

function validate(objForm)

          {

               if(objForm.dt.value.length==0)

               {

                   alert("Please enter department name!");

                   objForm.dt.focus();

                   return false;

               }                      

               if(objForm.cd.value.length==0)

               {

                    alert("Please enter code value!");

                    objForm.cd.focus();

                   return false;

               }

               return true;

          }

</script>

</head>

<body>

<center>

<form action="dbaccess.jsp" method="post" name="entry" onSubmit="return validate(this)">

     <input type="hidden" value="list" name="action">

     <table border="1" cellpadding="0" cellspacing="0">

     <tr>

          <td colspan="2" align="center">

<h2>Department Code Entry Form</h2></td>

     </tr>

     <tr>

          <td colspan="2">&nbsp;</td>

     </tr>

     <tr>

          <td>Department:</td>

          <td><input name="dt" type="text" size="50"></td>

     </tr>

     <tr>

          <td>Code:</td>

<td><input name="cd" type="text" size="50"></td>

</tr>

     <tr>

          <td colspan="2" align="center">

<input type="submit" value="Submit"></td>

     </tr>

</table>

</td>      </tr>       </table>

</form>

</center>

</body>

</html>

<%}%>

OUTPUT:

 

Input Form:

 

Alert Message from Validate Function:

 


 

Information to be updated:

 

Result Display:

 

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu