Documente Academic
Documente Profesional
Documente Cultură
This is the toughest thing I found when I was creating our project in TGMC10. Now, I know all the steps so its easy for me. I am sharing with you guys the steps for crossing the basic hurdles of starting the project. Follow the steps. Its very easy.
Basic Requirement: DB2 version 9.7 Eclipse IDE (Galileo) Apache-tomcat-6.0.16(Application server) or Any other Two .jar files I. db2jcc.jar II. db2jcc_license_cu.jar
#Note: You can download those .jar files from this two links http://www.java2s.com/Code/Jar/d/Downloaddb2jccjar.htm http://www.java2s.com/Code/Jar/d/Downloaddb2jcclicensecujar.htm
First do the following: Install db2 9.7. And during installation s/w ask you to install it creating a db2admin account .Check that. Then they ask you for password for that account. Give it and remember it . After entering that account you open the Eclipse IDE. (Remember that you should do all sort of work inside the db2admin account)
Example: Creating a simple register page and storing username and password into database.
Register.jsp
Check.jsp
No
Error.jsp
Code in Register.jsp :<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <form action="check.jsp"> Username: <input type="text" name="usern" /> </br> Password:<input type="password" name="pwd" /></br> <input type="submit" value="submit" /> </form> </body> </html>
Code in Check.jsp :<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <%@ page import="java.util.*,java.sql.*,java.io.*,java.io.IOException;" %> <% try { Class.forName("com.ibm.db2.jcc.DB2Driver"); }catch(ClassNotFoundException e) { e.printStackTrace(); } response.setContentType("text/html"); String usern=request.getParameter("usern"); String pwd=request.getParameter("pwd");
try { Connection con; con=DriverManager.getConnection("jdbc:db2://localhost:50000/abcd","db2admin","db2admin"); <!The previous line used as DB2 database connection string > String sql="insert into acc values(?,?)";<! SQL Query where acc is the table name> PreparedStatement pst= con.prepareStatement(sql); pst.setString(1, usern);<!set the value for first column of the acc table> pst.setString(2, pwd); <!set the value for second column of the acc table> pst.executeUpdate(); %> <jsp:forward page="Success.jsp"></jsp:forward><! Forward the execution to Success.jsp page > <% } catch(SQLException e) { System.out.println("Query is not executed"+e.getMessage()); %> <jsp:forward page="Error.jsp"></jsp:forward><! Forward the execution to Error.jsp page > <% } catch(Exception e) { e.printStackTrace(); } %> </body> </html>
con=DriverManager.getConnection("jdbc:db2://localhost:50000/abcd","db2admin","db2admin");
URL
Used for handling JDBC driver
Remember that :
When you are using this above code for your project just change the following: i) Change the database name (abcd) and add your database name ii) Change the username part and give the name of your accounts username where you installed the DB2 s/w iii) Change the password part and give the name of your accounts password where you installed the DB2 s/w
Code in Success.jsp :
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <b>Successfully added to the database..!!!!</b> </body> </html>
Code in Error.jsp :
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <b>Something wrong.Data can not be inserted..</b> </body> </html>
Registration page
Some important tips : When you are developing your own project remember your database name, account username, password of that account. And also remember your table name, columns data type, no. of columns. Query is not executed is shown in console view when there is a problem related to execute the query. It can be caused when tables data type missmatched with the parameters data type or wrong table name or no. of column specified in the query miss-matched with the original table. Sometimes though you added external jars to the project library it showed you Class not found error in the console view. You should do the following : a) Refresh the project pressing F5 b) Close the workspace and open it again c) Change the .jar files with newly downloaded ones d)Copy the jar files and go to the project location(project folder in hard drive) paste the jar files with in lib folder. (WebContent -> WEB-INF ->lib)
THANK YOU
Prepared By,
Nirmalya Roy CSE 4th year Student, Calcutta Institute of Technology