You are on page 1of 33

DB2 Database Connectivity from JSP

Simple Login Web Application

Right click on all databases-create database-standard

Give an easy to understand database name and click on next


Click on next

Click on next
Click on finish

The screen looks as above when database creation is successful.


The database name given by you will appear in the list
The schema name is same as your system name or login name.
Add required columns by clicking on add button.
Enter the name of the column,its size and the data type
corresponding to it.

If there are no errors the screen looks like above.


You can also define any constraints on the required columns.
The respective command is as shown in above screen shot.
Minimize DB2 Control Centre and open RAD
Click on Yes
First Insert a form and then insert two text fields and one submit
button in the form.

The form tag in home.html should be as follows:

<form method=”post” action=”check.jsp”>

…….

</form>
Select two text fields by pressing Ctrl then right click and group Align
– Left
For Running home.html
Web Sphere 7.0 starts as shown below.. wait until it starts
Now the page home.html is running as shown below
Click on Database Perspective as shown below
Enter the user name/password as db2admin/db2admin

When the connection is created click on web perspective

Note down the JDBC driver details shown above


After selecting open with JSP editor screen looks as follows
Write the following code in <body></body>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@page import="java.sql.*" language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>check</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<%
Class.forName("com.ibm.db2.jcc.DB2Driver");
System.out.println("driver loaded sucesssssssssssssssssss");
Connection
Conn=DriverManager.getConnection("jdbc:db2://localhost:50000/VOTERS","db2admi
n","db2admin");
String voter_name=request.getParameter("name");
String voter_id=request.getParameter("voter_id");
PreparedStatement Stmt=Conn.prepareStatement("SELECT NAME FROM
HP.VOTER_DETAILS WHERE NAME='"+voter_name+"' AND VOTER_ID='"+voter_id+"'");
System.out.println("Statement loaded sucesssssssssssssssssss");

Stmt.executeQuery();
ResultSet rs=Stmt.getResultSet();
System.out.println("query execute loaded sucesssssssssssssssssss");

String name= null;


while(rs.next())
{ name=rs.getString(1);
}
rs.close();Stmt.close();Conn.close();
if(name != null)
out.println("Welcome to "+name);
else
out.println("Please check your voter name and voter id...");

%>
</body>
</html>

Compile the JSP file with the option as shown below

Import db2 .jar files into web-inf ,library folder


Choose the path as shown in the following screen shot.
Select all three .jar files
Run .jsp file with option Run on Server ,select wAS 7.0

If it runs without any error then run home.html with


the option- Run on Server

1. Enter voter name and password


2. Click on LOGIN button
Following screen is shown if the details are true.
If the details are not true then above screen shot is
shown.

Exporting in to .EAR format(Enterprise Archieve Format)


When the .ear file is created you can import into other
computers using import option of RAD.

You might also like