Working With Database Application in JSP


In this article, I am going to develop a database application with JSP. To develop that type of application, first we design a database and then develop a JSP application using NetBeans IDE. For this application we use the following steps.

Step 1: Create a new database

At first we click on the blank database option and click on create.

make a new database.jpg

Step 2: Design the database

In this step we select field name and data type for the those fields. In this application we select five fields (id, name, city, state and country).


design view.jpg

Step 3: Make DSN

In this step we make a DSN using the following process.

First we select the control panel option from start.

select the control panel.jpg

Then click on the administrative tools.

click on administration tools.jpg

Then Select Data Sources option.

select data source option.jpg

Then select Microsoft Access Driver (*.mdb,*.accdb) and click on the finish button.

create new data source.jpg

Then select the database from your computer and give it a specific name. Now the DSN is created.

select data base.jpg

Step 4: Create a New Project

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

create new project.jpg

Step 5: 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 6: 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 7: Server and Setting

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

server and setting.jpg
 

Step 8: Select Framework

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

selectframework.jpg

Step 9: Create JSP file

We create one JSP (index.jsp) file for this application.

create new jsp file.jpg

Index.jsp

<%@ page import="java.sql.*" %>
<% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); %>
<HTML>
      <HEAD>
      <TITLE>
Working With a Database Table </TITLE>
      </HEAD>
      <BODY
BGCOLOR="cyan">
               <H1>Action in a Database Table </H1>
               <FORM NAME="form1" ACTION="index.jsp" METHOD="POST">
               <%
                    int current = 1;
                    if(request.getParameter("hidden") != null) {
                    current = Integer.parseInt(request.getParameter("hidden"));
                    }
                    Connection connection = DriverManager.getConnection(
                    "jdbc:odbc:data");
                    Statement statement = connection.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
                    ResultSet resultset =
                    statement.executeQuery("select * from table1");
                    if(current < 1){
                       current = 1;
                      }
                    resultset.last();
                    int rows = resultset.getRow();
                    if(current <= rows){
                    resultset.absolute(current);
                     }
                %>
                <TABLE BORDER="1">
                   <TR>
                      <TH>ID</TH>
                      <TH>Name</TH>
                      <TH>City</TH>
                      <TH>State</TH>
                      <TH>Country</TH>
                  </TR>
                   <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>
                 <BR>
                 <INPUT TYPE="HIDDEN" NAME="hidden" VALUE="<%= current %>">
                 <INPUT TYPE="BUTTON" VALUE="Next Data" ONCLICK="moveNext()">
                 <INPUT TYPE="BUTTON" VALUE="Previous Data" ONCLICK="movePrevious()">
           </FORM>
          <SCRIPT LANGUAGE="JavaScript">
          <!--
                function moveNext()
                {
                 var counter = 0
                 counter = parseInt(document.form1.hidden.value) + 1
                 document.form1.hidden.value = counter
                 form1.submit()
                }
                  function movePrevious()
                {
                  var counter = 0
                  counter = parseInt(document.form1.hidden.value) - 1
                  document.form1.hidden.value = counter
                  form1.submit()
                }
               // -->
           </SCRIPT>
        </BODY>
</HTML>

Step 10: Compile and Run the application

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

Output

Index.jsp

index1.jsp.jpg

Index.jsp for next data

next.jsp.jpg

Index.jsp for previous data

prev.jsp.jpg

Resources related to this article


Similar Articles