• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » JSP » Elements » Database To JSP Communication
Next →
← Prev

Database To JSP Communication

By Dinesh Thakur

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

HTML Program

                                                      

<html>

  <title>DB-JSP</title>

  <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’/>

    </center>

  </body>

  </form>

</html>

 

Output:

              

JSP Program

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

    <%

    try

       {

         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 (rs.next ())

           {

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

               </th><th>EMPNO</th><th>SAL</th>                  

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

               out.println(“<tr><td>”+rs.getString(1)+”</td>”);

               out.println(“<td>”+rs.getString(2)+”</td>”);

               out.println(“<td>”+rs.getString(3)+”</td>”);

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

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

               out.println(“<td>”+rs.getString(6)+”</td></tr>”);

               out.println(“</table></center>”);

           }

         else

           {

          out.println(“<center><font color=’red’ size=’7′>Invalid Id Try Again</font></center>”);

           }

       }

          catch(Exception ee)

          {

               System.out.println(ee);

          }

      %>

Output:

            

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(rs.next())

If (rs.next) 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 () ;.

else{

• out.println(“<center><font color=’red’ size=’7′>Invalid Id Try Again</font></center>”);

If (rs.next) 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.

You’ll also like:

  1. Html To JSP Communication
  2. Servlet to JSP Communication
  3. Jsp To Servlet Communication
  4. Servlet to Database Communication
  5. What is JSP?
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Server Pages

Java Server Pages

  • JSP - Home
  • JSP - Advantages
  • JSP - Architecture
  • JSP - Life Cycle
  • JSP - Servlet Disadvantages
  • JSP - PrintWriter Vs JspWriter
  • JSP - Session Tracking
  • JSP - Stateless Protocol
  • JSP - Hidden Form Fields
  • JSP - Program Structure
  • JSP - Bill Discount Program
  • JSP - Scripting Elements
  • JSP - fn:length()
  • JSP - fn:split()
  • JSP - fmt formatNumber Tag
  • JSP - Servlet Communication
  • JSP - fn:replace()
  • JSP - fmt:parseNumber Tag
  • JSP - fmt:parseDate Tag
  • JSP - fn:substring()

Other Links

  • JSP - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW