Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Web Interface
Murali Mani
Options
PHP with mySQL
JSP/Servlets with JDBC on mySQL/Oracle
Others …
Murali Mani
PHP with mySQL
PHP (Hypertext Pre Processor)
Server Side scripting language
Provides library functions to connect to
databases (mySQL, Oracle, etc…)
CCC provides support for PHP with mySQL
Check
http://www.wpi.edu/Academics/CCC/Help/Uni
x/Webdev
Also check http://www.php.net
Murali Mani
PHP - Introduction
• Create the file test.php
#!/usr/local/bin/php
and keep it in your
<html>
public_html directory
• Ensure that the file has
<body>
permissions 755
<?php
• Ensure that your root
$myvar=“Hello World!”; directory and your
public_html directory have
echo $myvar;
permissions 755
• Now go to
?>
http://users.wpi.edu/~mmani/t
</body></html>
est.php
Murali Mani
PHP with mySQL (CLI: Call
Level Interface)
PHP provides functions such as
$db = mysql_connect ($db_host, $db_username,
$db_pw);
mysql_select_db ($db_name, $db);
$result = mysql_query ($query, $db);
Look at handouts for sample code for
displaying results on a web page, and for
inputting values using a HTML form.
Murali Mani
Java Servlets
Steps
Install a web server, such as Apache Tomcat
Learn about servlets
Learn about HTML forms
Learn how to use JDBC
Integrate them into your project.
Murali Mani
Installing a web server
Download it from jakarta.apache.org/tomcat
You might need about 50 MB of space for the
installation
For example, get the .tar.gz file (You may
want to keep it in the temp directory, rather
than your personal disk space).
tar –xvzf file.tar.gz (untar it directly without
unzipping it to save space).
Murali Mani
Setting up the webserver
I will call the root of the installation $TOMCAT_DIR
Check the file $TOMCAT_DIR/conf/server.xml
In your .cshrc
setenv TOMCAT_DIR /home/mmani/jakarta-tomcat-5.0.18
You will see a line <Connector port=“8080”
You can renumber the port, say between 1200 and 20000
For your .cshrc
setenv PATH ${PATH}:${TOMCAT_DIR}/bin
setenv CLASSPATH
${CLASSPATH}:${TOMCAT_DIR}/common/lib/servletapi.jar
Murali Mani
Test the webserver
Run the script startup.sh
Open the page: http://ccc2.wpi.edu:1200
You ran the startup.sh from ccc2
Your web server is configured to port 1200
(default was 8080)
To check for errors etc, check
$TOMCAT_DIR/logs
To shut down, run the script shutdown.sh
Check what processes are running: ps -u mmani
Kill unnecessary Java processes: killall java
Murali Mani
Servlets: Introduction
Write the java code, and compile it.
Configure the web server to recognize the
servlet class.
Restart the web server
Murali Mani
First Java Servlet
Check the directory
$TOMCAT_DIR/webapps/servletsexamples/WEB-INF/classes
There exist example servlets here
Create a test servlet with the method doGet
Compile it, let our test servlet be
TestServlet.class
Murali Mani
Configuring the web server
Check $TOMCAT_DIR/webapps/servletsexamples/WEB-INF/web.xml
Add the declarations
<servlet>
<servlet-name>MyTestServlet</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyTestServlet</servlet-name>
<url-pattern>/servlet/FirstTestServlet</url-pattern>
</servlet-mapping>
Murali Mani
Test the servlet
Restart the web server
Go to the URL
http://ccc2.wpi.edu:1200/servlets-examples/servlet/FirstTestServlet
Murali Mani
JDBC: CLI (Call Level
Interface)
JDBC (Java Database Connetivity) is a
standard API for connecting to databases
from Java programs (such as servlets).
Different vendors provide JDBC drivers
implementing the JDBC API for different
DBMS: Oracle, mySQL etc
Murali Mani
Java Code with JDBC
Steps
import java.sql.*
Load a driver instance
Establish Connection
Create a Statement
Query
Murali Mani
JDBC with Oracle
JDBC driver comes with database server
Check $ORACLE_HOME/jdbc/Readme.txt
setenv CLASSPATH
${CLASSPATH}:${ORACLE_HOME}/jdbc/lib/
ojdbc14.jar
Murali Mani
JDBC: Oracle
Loading a Driver
Class.forName (“oracle.jdbc.driver.OracleDriver”);
Establishing a Connection
Connection conn = DriverManager.getConnection
(“jdbc:oracle:thin:@oracle.wpi.edu:1521:CS”,
<userName>, <password>);
Create a Statement
Statement stmt = conn.createStatement ();
Murali Mani
JDBC with mySQL
You need to install the driver mySQL
Connector/J from www.mysql.com
Setenv CLASSPATH <dir>/mysql-connectorjava-3.1.0-stable-bin.jar
Murali Mani
JDBC: mySQL
Loading a Driver
Class.forName (“com.mysql.jdbc.Driver”);
Establishing a Connection
Connection conn = DriverManager.getConnection
(“jdbc:mysql://mysql.wpi.edu/<dbName>”,
<userName>, <password>);
Create a Statement
Statement stmt = conn.createStatement ();
Murali Mani
Queries using JDBC
Queries: SQL DDL
String sql = “CREATE TABLE a (a1 int, a2 int)”;
stmt.executeUpdate (sql)
Queries: SQL DML (Updates)
String sql = “INSERT INTO a values (1, 1)”;
stmt.executeUpdate (sql)
Queries: SQL DML (Retrieval)
String sql = “SELECT * FROM a”;
ResultSet r = stmt.executeQuery (sql);
Murali Mani
JDBC Result Set: Iteration
We can iterate over a result set, r as:
/* fetch the next tuple from r and ensure that it is not
empty */
while (r.next ()) {
System.out.println (“a1 = “ + r.getString (“a1”));
}
Murali Mani
Close the statement and
connection
try {
stmt.close ();
} catch (SQLException sqlEx) {
System.out.println (“Could not close statement:” +
sqlEx.toString ());
try {
conn.close ();
} catch (SQLException sqlEx) {
System.out.println (“Could not close connection:”
+ sqlEx.toString ());
Murali Mani
Using Servlets with JDBC
Ensure that the JDBC driver can be
downloaded by our servlet.
The servlet sees only the classes available at
$TOMCAT_DIR/shared/lib
$TOMCAT_DIR/common/lib
Create a symbolic link, for example, for
Oracle JDBC driver, from the directory
$TOMCAT_DIR/shared/lib
ln –s $ORACLE_HOME/jdbc/lib/ojdbc.jar ojdbc.jar
Murali Mani