Thursday, November 24, 2011

comment_icon 40 Creating A simple DB2 Database Application with jsp


in this session we are going to see that How to Create A simple DB2 Database Application with JSP (java server pages) . Application which we are going to build is login form
Topics Covered :
1. creating database for an application

2.Creating A form and sending data to server

3. connection to database with jsp

Steps to Follow
1.    Create database"VOTERS"



  •       create table "VOTER_DETAILS"
  •       insert column "name" Character30 and "voter_id" Varchar 20
  •       set voter_id as primary key
  •        insert values in table

2. open RAD
3. creat dynamic web project "project1"
4.  Right click on project and  -> new  -> webpage -> give page name as  index
5. index.jsp is opened if not then explore web Content folder of project1 and double click on index.jsp
6. in that page  place the code in between body tag

Voter name: Voter id:

7. save that page
8. now  we need to create the page where the form data is passed i.e. check.jsp
9. create new page as check.jsp
10  open that page source n delete all n paste the code given below
<%@page import="java.sql.*" language="java" contentType="text/html;
charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>


check



<%
Class.forName("com.ibm.db2.jcc.DB2Driver");
System.out.println("driver loaded sucesssssssssssssssssss");
Connection Conn=DriverManager.getConnection("jdbc:db2://localhost:50000/VOTERS","username","password");
String voter_name=request.getParameter("name");
String voter_id=request.getParameter("voter_id");
PreparedStatement Stmt=Conn.prepareStatement("SELECT NAME FROM 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...");
%>




11. now save this page
12 now what left is creating the database connection
13  open data perspective  goto window -> open  perspective ->select data  perspective
14 creating connection

  •     right click on database select new a new window prompt
  •     fill the details as shown in image 

test the connection :

15. now start the server

16. run the application

17. if you t\run and submit the form you will get error .....









18. cause of the above error is the .. library file missing for db2
19. To solve this follow the steps given below

  •     expand the project
  • project->webcontent->web inf -> select lib folder
  • right click on lib  -> select import -> expand general -> select file system 
  • click on next then browse the path  C:\Program Files\IBM\SQLLIB\java
  • select java folder now check db2jcc.jar 
20. Now Import completed you can run the project 
21. Everything i shown in pictures just follow them 
22. its always good to here the reply so please comment n share this  :)