JSP Access to the Database to Display Unformatted Results Due: Wednesday, October 31 in 433 class

mobdescriptiveSoftware and s/w Development

Oct 28, 2013 (4 years and 2 months ago)

62 views

JSP Access to the Database to Display Unformatted Results

Due: Wednesday, October 31 in 433 class


JavaServer Pages (JSP) technology enables rapid development of web
-
based applications enabling you to
separate the dynamic part of your pages from the stati
c HTML.
JSPs look like a cross between
HTML

and
Java codes and allow one to embed intelligent data into normal
HTML

pages. For example, we cannot have
an if...then or for...loop inside Html page, but with JSP it is possible. Theoretically, you can have any

valid
Java code inside a JSP.


JSP

technology uses XML
-
like tags and scriptlets written in the Java programming language to encapsulate
the logic that generates the content for the page. Additionally, the application logic can reside in server
-
based
resou
rces, which the page can access with these tags. Any and all formatting (HTML or XML) tags are
passed directly back to the response page. By separating the page logic from its design and display and
supporting a reusable component
-
based design, JSP technol
ogy makes it faster and easier than ever to build
web
-
based applications.


In this assignment you will be developing JSPs to access the database and display unformatted results.


Phase I: Creating a connection to the database


Before you can access any da
ta from the database you need to create a connection to the database. To
begin we you can use any editor you wish (notepad will do fine). The connection can be done by:


<html>

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


<%

//define connection

Connection con = null;


try{

//get the class

Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");


//get the connection


con = DriverManager.getConnection("jdbc:db2:
database_name
","
user_name
","
user_passwd
");

}


//please catch your exceptions!

catch(Exception e){


out.println(e
.getMessage());

}

%>

</html>


As you can see the Java code can be placed inside the HTML code by using the following tags:

<%
place Java code here

%> . Between <% and %> tags, any valid Java code is called a Scriptlet.


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

a JSP directive that lets you import all the java.sql.* classes into the
current JSP page. A JSP directive gives special information about the page to the JSP Engine.
Directives do
not produce any visible output when the page is requested but change the w
ay the JSP Engine processes the
page.


You can get the class object associated with the given driver:
COM.ibm.db2.jdbc.app.DB2Driver

Then
establish a connection to the given database URL with the appropriate username and password:

jdbc:db2:
database_name
",
"
user_name
","
user_passwd



Phase II: Getting data from the database and displaying on the JSP page


Now that you have opened a connection you need to get data from this database and display it.

Query, insert, delete, modify data from one or more tables w
ith some application logic. Create a series of JSP
pages that link to each other. For example you could create one JSP for a login page and carry that user
name and password to the next employee page.


You can use your Stored Procedures you wrote in the p
revious assignment to execute queries, inserts,
delete, etc. and call it from your JSP to display the results.


A sample JSP code for getting the data out of the database:


<%

//define resultset and statement

ResultSet rs=null;

Statement stmt=null;


try {

//Using the current database connection create a statement

stmt=con.createStatement();


//the query to execute and execute it resulting in a ResultSet



String sql="SELECT * FROM cs433 ";



rs = stmt.executeQuery(sql);



//While there are studen
ts to print, print the students name from the column Student_Name

while(rs.next()) {

out.println(rs.getString("Student_Name"));


}




//close all your open resultsets, statements, and connection when you are done with them!

rs.close();


stmt.close();

c
on.close();

}


//catch all your exceptions

catch (SQLException e) {



out.println(e.getMessage());

}

%>


Save you JSP pages in your group folder (
\
\
egret.csuglab.cornell.edu
\
database_name
) as .jsp

You can execute your JSPs through the browser by typing the path where you stored your JSP files:
http://egret.csuglab.cornell.edu/
database_name
/
JSPFile.jsp


What to “submit”:

Create a file that includes the following information:


-

The names and

netids of all group members

-

For each JSP, write a brief description of the logic and what is displayed.


Also submit:


-

All you JSP pages (atleast 2 pages)


Put this in a folder named JSPAssign and place into your database directory on egret.


If you have
any further problems, contact the teaching assistants.