1 Java: 1.1 Installation
1 Java: 1.1 Installation
Java is just another programming language, that’s similar to C/C++ syntax (and C#1 ).
It is an interpreted language that has been applied to a variety of domains—such as client
side programming via Applets and Swing applications, and server side via Java Enterprise
Edition.
Java still remains relatively popular for database/server type applications. Many would
argue that the glory days of Java on the client side (Applets/Applications) is long gone...
1.1 Installation
If you’re using Windows, you probably already have a way to execute Java programs.
It is called the JavaVM, and is usually included with Internet Explorer. If you want
to compile programs though, you need to download the Java SDK. You can find it at
http://java.sun.com.
After you install the SDK, you should setup your $JAVA HOME and $PATH variables. The
$JAVA HOME needs to point to the directory where you installed the SDK, typically something
like c:\j2sdk, and $PATH should point to the ‘bin’ directory, typically c:\j2sdk\bin
2 JDBC
Java uses JDBC2 to access databases of various sorts. It is a portable database interface:
all databases do the same things anyway in the same way—so why not create a library that
abstracts minor differences between databases?
In this document, we will develop a web-site using JSP, using Microsoft Access at first,
and then quickly (without hassle) switch to using Oracle.
3 JSP
JSP stands for Java Server Pages. It is a form of a Servlet. A Servlet is a piece of Java code
that is setup to process requests (ie: it acts like a server—but not a full fledged server3 .)
JSP is sort of like ASP, or PHP, only it uses Java.
1
click on Jakarta link). After downloading the binary distribution (get Tomcat version 5),
you unzip it into something like: C:\jakarta-tomcat.
To start Tomcat, you run: C:\jakarta-tomcat\startup, and to stop it, you run:
C:\jakarta-tomcat\shutdown. There are other ways of starting it up (you can even have
it run as a Windows service); you can read up on various configuration options in the
documentation, which (now that you’ve installed it and it’s running), can be found on
http://localhost:8080/tomcat-docs/ or alternatively on the Jakarta website.
4 Microsoft Access
Yes!, you read it right. Microsoft Access. Why are we studying it? Because it’s always
there... and sometimes it’s sorta useful as a quick proof-of-concept thing. Anyway, don’t
worry if you don’t have Microsoft Access... since you already have it: it’s built into Windows.
In Windows: go to your control panel (maybe also “Advanced Tools”) and then double
click on “Data Sources (ODBC)”. This is a central Windows thing that allows you to con-
figure ODBC interfaces. Once there, pick the “System DSN” tab, and click “Add...” From
the list of drivers that come up, select “Microsoft Access Driver (*.mdb)” and click “Finish”
button.
Click on the “Create” button to crate a new database; and place it some directory like
C:\Temp. Name it gbookdsn, and give it any description you like. Click ok, etc., and
congratulations, you’ve just setup a Microsoft Access database!
2
5 Architecture
Just as before, we will break up the whole application into components, so that changing
things is relatively easy. For example, for our GuestBook, we will have a “value” class such
as:
package gbook;
import java.util.*;
import java.sql.*;
import java.util.*;
// gets a db connection
public static Connection getConnection() throws SQLException {
Connection conn = null;
try{
// Microsoft Access PART
// load db driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
// connect with a Data Source Name (DSN) of a database.
conn = DriverManager.getConnection("jdbc:odbc:gbookdsn");
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(InstantiationException e){
5
The actual source code contains comments on how to get Oracle to work.
3
e.printStackTrace();
}catch(IllegalAccessException e){
e.printStackTrace();
}
return conn;
}
statement.execute(command);
statement.close();
connection.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(statement.execute(command)){
ResultSet rs = statement.getResultSet();
while(rs.next()) {
GBook g = new GBook();
g.gbookid = rs.getInt("GBOOKID");
g.name = rs.getString("NAME");
g.email = rs.getString("EMAIL");
g.message = rs.getString("MESSAGE");
4
g.postdate = rs.getDate("POSTDATE");
v.addElement(g);
}
}
statement.close();
connection.close();
}catch(SQLException e){
e.printStackTrace();
}
return v;
}
}
<%
java.util.Enumeration enum = DbHelper.getGuestBook().elements();
if(enum.hasMoreElements()){
%><table border="1" width="60%"
bgcolor="#DDDDDD"
cellspacing="1" cellpadding="5">
<%
while (enum.hasMoreElements()){
GBook g = (GBook)enum.nextElement();
%><tr>
<td width="20%" bgcolor="#EEEEEE"
valign="top">
<nobr><%=g.name%></nobr><br>
<small><%=g.email%></small><br>
5
<small><%=g.postdate%></small>
</td>
<td width="80%" bgcolor="#EEEEEE"
valign="top">
<%=g.message%>
</td>
</tr><%
}
%></table><%
}else{
%><h3 style="color:red">Sorry,
database Error.</h3><%
}
%>
<p> </p>
6
</table>
</form>
<p> </p>
<p>© 2004, CIS45</p>
</center>
</body>
</html>
Notice that right now we use the <% and %> instead of the PHP ones (and also that we have
Java instead of PHP code).
Another important thing to notice is that our JSP code has no database code at all. All
we do is display results we got from some other class. There is a way to make this even
cleaner using JSP tag libraries (i.e.: not have any Java code at all, just special tags that do
all these things), but we don’t have time to go into that. Tomcat documentation (which you
should already have installed) has everything you need to build tag libraries yourself.
The submission code (the “action”) is just:
<%@ page import="gbook.GBook,gbook.DbHelper"%><%
GBook g = new GBook();
g.name = request.getParameter("name");
g.email = request.getParameter("email");
g.message = request.getParameter("message");
DbHelper.addGuestBook(g);
response.sendRedirect("index.jsp");
%>
Note that it doesn’t do any error checking, etc., and in fact has a very serious bug6 , which
we will discuss when we deal with security a bit later in the course.
7 Setting it up
To get the sample application working, you need to place it in the webapps7 directory, and
then point your web-browser (after starting Tomcat of course) to http://localhost:8080/gbook/
8 Oracle
You don’t need much to get it to run under Oracle. You already have most of the code.
From the Oracle installation directory, you need to copy the JDBC “thin” drivers, and place
them in the /WEB-INF/lib directory, as a JAR file8 .
You then need to change the connection code in DbHelper, to that of Oracle:
6
What happens when someone enters a quotation character as part of their name or message?
7
You’d have: C:\jakarta-tomcat\webapps\gbook or something similar.
8
You may need to rename it to .jar, since it is likely a .zip
7
String driverName = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverName).newInstance();
8
INSERT INTO
GBOOK (GBOOKID,NAME,EMAIL,POSTDATE,MESSAGE) VALUES (
gbook_sequence.CURRVAL,
’John Doe’,
’[email protected]’,
SYSDATE,
’And this is a message!’
);
9 Conclusion
And basically that’s it. Once you write an application, changing the database shouldn’t be
too hard. Even between vastly different databases, like Microsoft Access and Oracle.