Tuesday, June 25, 2013

Creating A simple DB2 Database Application with jsp

Creating a 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"%>
<%
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  :)










                            

You might also Like:

SENDING SMS VIA WAY2SMS USING JAVA

way2sms using Java

UPDATED CODE

This tutorial shows that how you can use the http://www.indyarocks.com/ for sending message using your java application 

Steps to Follow
  1. create a account on http://www.indyarocks.com/ 
  2. after getting indiarocks username and password use the code given below.
  3. modify the following details
    1. put your email id in Email string
    2. put your indiarocks username
    3. put your indiarocks password
    4. put number as the number on which you want to send sms
    5. put your messgae in messgae string , i limited the length to 110 char for stopping the misuse
  4. if you copy the code in your application's jsp page don't forget to import java.net.*,java.io.*,java.net.URLEncoder
Example JSP code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@page
 language="java" import="java.net.*,java.io.*,java.net.URLEncoder;" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>

<html>
<head>
<title>index</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<%
/* This api is only for developemnt purpose.

info : make sure to use valid email otherwise it won't work,message char limit is 110

**/
String email="put your email id here"; 
String user=" your Indiarocks username";
String pass=" your Indiarocks password";
String number="some number on which you want to send msg";
String msg="your message to send" ; 
String mURL="http://www.onl9class.com/smsapi/smsir.php?email="+email+"&user="+user+"&pass="+pass+"&number="+number+"&msg="+URLEncoder.encode(msg, "UTF-8");;
URL url = new URL(mURL);
BufferedReader reader = null;

try {
    reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));

    for (String line; (line = reader.readLine()) != null;) {
        out.println(line);
    }
} finally {
    if (reader != null) try { reader.close(); } catch (IOException ignore) {}
}
 %>
</body>
</html>


You might also like:

10 Fastest Supercomputers In The World



SENDING SMS VIA WAY2SMS IN ASP.NET C#