by Dinesh Thakur Category: JSP Elements

In the below JSP program the aim is to understand how JSP communicates with the database. The designing is done by HTML.

HTML Program




  <form action= "DB-JSP.jsp">

  <body bgcolor= 'white'>

     <h1 align="center"><font color= 'Black' size='8' >Search Employee Details</font></h1>

     <br><Center><font color= 'Gray' size= '6' >Enter Employee Id</font>

     <input type='text ' size= '20' name='t1'>

     <input type='Submit' value='Submit' name="b1" />

    &nbsp;<input type='Reset' value= 'RESET' name= 'b2'/>







              Database To JSP Communication

JSP Program

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




         String driver = "com.mysql.jdbc.Driver";

         String url = "jdbc:mysql://localhost/dbase";

         String uid = "root";

         String psw = "root";

         Class.forName(driver) ;

         Connection con=DriverManager.getConnection (url,uid,psw) ;

         Statement st=con.createStatement ();

         String empno=request.getParameter("t1");

         ResultSet rs = st.executeQuery("select * from emp where EMPNO="+empno);

         if ( ())


              out.println("<center><table border='15' color='red'><tr><th>EMPNAME   


               <th>EMPADD</th><th>MAILID</th><th>PHNO.</th><  /tr>");




               out.println("<td>"+rs.getString(4)+"</td>") ;

               out.println("<td>"+rs.getString(5)+"</td>") ;






          out.println("<center><font color='red' size='7'>Invalid Id Try Again</font></center>");



          catch(Exception ee)






            Database To JSP Communication

Explanation of the above program

• Class.forName("oracle.jdbe.driver.Oracle Driver");

It loads the type-4 driver.

• Connection

con=DriverManager.getConnection(" jdbe:oraele:thin:@localhost:1521:xe","system","manager");

It establishes the connection.

• Statement st=con.createStatement();

It creates the statement.

• String empno=request.getParameter("tl");

It reads the employee number through the getParameter(); of the request interface.

• ResuItSet rs=st.exeeuteQuery("select * from EMP where EMPNO=" +empno);

This statement executes the query and retrieves the information from the EMP table and stores all the value in the ResultSet object.

• if(

If ( returns true then a table will be formed and all the values which are retrieved from the EMP table of the database are stored in that table and displayed on the browser through the out.println () ;.


• out.println("<center><font color='red' size='7'>Invalid Id Try Again</font></center>");

If ( returns falls then it print "Invalid Id Try again" on the browser window as the response.

• catch(Exception ee)


• System.out.println(ee);


The generated exception is handled by the catch block.

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.