Select a Specific Data From a Database in JSP


This is the next article in the series of database applications in JSP. In this article, I am going to describe how to select specific data from a database. First we construct a database and a DSN then generate a JSP application which selects specific data from a database. For constructing the database and DSN you may follow the first application of database ( working with database application in JSP). For developing this application we follow the following steps.

Step 1 : Create a New Project.

In this step we select New Project option from file menu.

create new project.jpg

Step 2 : Choose Project.

In this step we select web application from java web option and then click on the next button.

select new web application.jpg

Step 3 : Name and Location.

In this step we given it a specific name and set a specific location and click on the next button.

name and location.jpg

Step 4 : Server and Setting.

We select a specific server for this application and click on the next button.

server and setting.jpg

Step 5 : Select Framework.

There is no need to select any framework; for this application just click on the finish button.

selectframework.jpg

Step 6 : Create jsp file.

We create one jsp file for this application.

create new jsp file.jpg

selectdata.jsp

<%@ page import="java.sql.*" %>
<% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); %>
<HTML>
       <HEAD>
       <TITLE>
Select Data From a Database</TITLE>
       </HEAD>
       <BODY BGCOLOR="cyan">
            <H1>Select Data From a Database</H1>
            <%
                Connection connection = DriverManager.getConnection(
                "jdbc:odbc:data2");
                Statement statement = connection.createStatement() ;
                ResultSet resultset =
                statement.executeQuery("select * from table1 where City = 'Meerut'") ;
            %>
                <TABLE BORDER="1">
           <TR>
                 <TH>ID</TH>
                 <TH>Name</TH>
                 <TH>City</TH>
                 <TH>State</TH>
                 <TH>Country</TH>
          </TR>
           <% while(resultset.next()){ %>
           <TR>
                <TD> <%= resultset.getString(1) %></td>
               <TD> <%= resultset.getString(2) %></TD>
               <TD> <%= resultset.getString(3) %></TD>
               <TD> <%= resultset.getString(4) %></TD>
               <TD> <%= resultset.getString(5) %></TD>
          </TR>
           <% } %>
               </TABLE>
         </BODY>
</HTML>


Step 7 :
Compile and Run the application.

Now we compile the application and then run it on the server and find the following output.

Output

selectdata.jsp

index.jsp.jpg

Resources related to this article


Similar Articles