Professional Documents
Culture Documents
2
--TGMC workshop team, SOC.
If you need, you could create a sample database. Or, you could create your own database from First Steps (this window) Or you could launch Control Center and create your own database from there
Control Center could be launched either from Taskbar (Right Corner) or from Programs IBM DB2 DB2COPY1 General Administration Tools Control Center
Choose IBM-1252 (default option) if you are using the database to store only English If you want localization choose UTF-8 Click Next
Click on Show Command to see the database creation command generated by Control Center Click Finish to create database
Click on Show Command to see the database creation command generated by Control Center Click Finish to create database
Click on Add Primary to add a primary key for the table. Similarly for Add Unique and Add Foreign (if needed)
Click on Show SQL to see the table creation command generated by Control Center Click Finish to create table
Give a suitable workspace here, where all your work will be Stored!
Click next
The error symbol should disappear to a warning symbol after choosing the correct location
Click browse button, to choose the home directory where WAS CE is installed i.e., the parent-directory for the bin folder of WAS CE
Click next. Please note the user id and password for WAS CE
Click finish!
Make similar selections and it will Guide you to the project creation Wizard
Click next!
Click finish
You should have such hierarchy Of files inside your dynamic web Projects node in the tree shown in Project Explorer Pane
Click start
Wait until the server finishes starting You can see a message in the console! [it happens sometimes that the server Has some problems due to internet connection (wi-fi). So, disable wi-fi, if your server throws Exceptions at startup.]
Click this link(first one) This is for creating a link with the Database created at DB2
Give a name(and remember it) for the database pool you are creating. FYI: a pool is a set of connections with the database. It avoids the program To communicate with the database directly for accessing it each time. Instead, by creating this pool, it gets a set of connections from the database so that they can be used one-at-a-time basis, whenever needed. This not only saves time for the program, but also for the database!
The name of the database is testdb in our case, and a snapshot of the control center is also shown
Give the database name correctly as it is the one you are going to make transactions through the program. The database should already exist and should be running(at least before testing the connection)
If you see this message, leave the fields as such and click the Test Connection button
Now, the server is connected with the database and we can deploy the pool permanently. Click Deploy
Now, to connect the pool with our application, Click usage link to the right of the pool, which has connections with the needed database
The next page has a picture, which is the screen shot of the page that appears after clicking the usage link of the database pool. It also has a clipped image of the project explorer pane of eclipse with the web content folder of the project expanded You can find two sub folders within it. Among which WEB-INF has two files. Those two files are needed to be modified, in order to make our application communicate with the pools, which in turn communicate with the database.
To rectify the error, change the nam shown in the highlight to naming
Now, we shall see, how to code in eclipse, retrieve data from DB2 and test-and-run it in WAS CE.
FYI: All packages needed should be placed inside the src folder for its usage as beans or as importable classes. All jsp,html pages should be placed inside the WebContent folder. In order to make a html page, right click WebContent and make selections as shown.
Click finish
You can type the html codes here, save it. The sample, we are having has the following objectives: 1. Gets the user id and password from user through a html page 2. Verifies it with the data stored in the database and 3. Finally direct the right user to welcome screen and others back to Login
The values of inputs in this form will lead the page mentioned in its action property. If the page mentioned is not found, WAS CE reports error
Now, to run the page, right click the file on the project explorer pane and select the options as shown.
Make sure that the project in which the file to run exist in the list of configured projects and click finish
<<sample`s output>>
<<sample`s output>>
The following two slides shows the codes in this page as snapshots. To understand it, the forthcoming pages at the last will consist of Explanations to each necessary statement.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import="javax.naming.*,javax.sql.*,java.sql.*"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Hai!</title> </head> Here, the first few lines are taken. <%@ page> tag needs attention for the last import property. As value(s) for the property you can give any number of valid java packages, separated by commas. The pageEncoding field can be changed to any encoding schemes like UTF-8,UTF-16 etc. <!doctype> tag plays importance only in case of using XML. We can apply particular style for custom tags using XSLs in XML. It can be ignored right now. Next is the <head> tag, within which the <title> is used to display the text at the title bar of the browser.
<script language="javascript"> function fun() { alert("welcome user!your details are found in the database!!"); } function foon() { alert("Sorry! you are not a valid user!! click here to go back to login!!!"); history.back(1); } </script>
The power of J2EE lies in the use of HTML, java, JavaScript, XML etc. in a single source file. This shows two javascript functions with the output of fun() as shown in (i) and foon() produces the message as shown in(ii) and leads to the page visited before.
(i)
(ii)
Inside the <%...%> tag, we can type all java regarding statements. The request.getParameter(<name_of_the_input>); will return as String the value of the input in the previous page with the corresponding name given, when the submit action was performed in the form, which led to this page. When there are more than one inputs with the same name in a form, their values can be collectively retrieved as a String array using request.getParameterValues(name);
try {
boolean flag=false; InitialContext ic=new InitialContext(); DataSource ds=(DataSource)ic.lookup("java:comp/env/jdbc/testpool"); Connection con=ds.getConnection(); Statement stmt=con.createStatement();
For simplicity, it is not necessary to understand ever line of this jdbc block. These are the statements for making connection with the pool. These statements need Javax.naming.*, javax.sql.*, java.sql.* ; boolean flag=false; is a statement to be used to indicate the validity of the users login. In the 5 th line, the testpool should be replaced by the appropriate pool names given in the WEB-INF/web.xml and WEB_INF/geronimo-web.xml files. Statement is a class, which has methods for executing queries and it cannot be directly instantiated. The first statement try{ will be referred in slide 82.
ResultSet rs=stmt.executeQuery("select * from log_table"); while(rs.next()) { String temp=rs.getString(1); String temp1=rs.getString("pass"); if(temp.equals(user)&&temp1.equals(pas)) { flag=true; break; } }
To execute select queries, we use stmt.executeQuery() whose result is returned in a ResultSet object. Insert, update & delete queries should be executed as stmt.executeUpdate(query); From the ResultSet object , we should know about 2 general functions. next() function takes the cursor to the next row of the result and returns true on success and if it is already in the last row, returns false. get<type>() method returns the cell value specified by the column name as String or column index as integer with the specified type. The logic involves checking the user id and password entered with those retrieved from the log_table in the database.
if(flag) { out.println("hai!!!"+user); %> <script language="javascript">fun();</script> <% } else { %> <script language="javascript"> document.write("Invalid parameters"); foon(); </script> <% }
The logic here is, for valid users, fun() is called and for others foon() is called. This piece shows how interoperable languages are! We can close java abruptly, begin html, which in turn begins javascript. We can publish the text in a page by all 3 means. Using <%out.println();%> in java Or document.writeln() in JavaScript Or using ordinary markups. It is to be noted fun() and foon() have their codes and explanation in slide number:77
Every try{} block should have a catch(){} block or finally{} block or both. As java is very cryptic In errors and exceptions it is necessary to use them. However J2EE has a choice. But to keep track of errors, it is advised to use try blocks wherever needed. The statement e.printStackTrace(); will have references to the line in which the error occurred by showing a lengthy stack trace at the servers console, which will help in debugging. Finally, the code is to be tested and run. As seen, this page needs the sample.html page to be run, in order to check for bugs. So, run sample.html.