Presentation is loading. Please wait.

Presentation is loading. Please wait.

Murali Mani Web Interface. Murali Mani Options PHP with mySQL JSP/Servlets with JDBC on mySQL/Oracle Others …

Similar presentations


Presentation on theme: "Murali Mani Web Interface. Murali Mani Options PHP with mySQL JSP/Servlets with JDBC on mySQL/Oracle Others …"— Presentation transcript:

1 Murali Mani Web Interface

2 Murali Mani Options PHP with mySQL JSP/Servlets with JDBC on mySQL/Oracle Others …

3 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

4 Murali Mani PHP - Introduction #!/usr/local/bin/php <?php $myvar=“Hello World!”; echo $myvar; ?> Create the file test.php and keep it in your public_html directory Ensure that the file has permissions 755 Ensure that your root directory and your public_html directory have permissions 755 Now go to http://users.wpi.edu/~mmani/t est.php

5 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.

6 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.

7 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).

8 Murali Mani Setting up the webserver I will call the root of the installation $TOMCAT_DIR In your.cshrc setenv TOMCAT_DIR /home/mmani/jakarta-tomcat-5.0.18 Check the file $TOMCAT_DIR/conf/server.xml 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/servlet- api.jar

9 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

10 Murali Mani Servlets: Introduction Write the java code, and compile it. Configure the web server to recognize the servlet class. Restart the web server

11 Murali Mani First Java Servlet Check the directory $TOMCAT_DIR/webapps/servlets- examples/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

12 Murali Mani Configuring the web server Check $TOMCAT_DIR/webapps/servlets- examples/WEB-INF/web.xml Add the declarations MyTestServlet TestServlet MyTestServlet /servlet/FirstTestServlet

13 Murali Mani Test the servlet Restart the web server Go to the URL http://ccc2.wpi.edu:1200/servlets-examples/servlet/FirstTestServlet

14 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

15 Murali Mani Java Code with JDBC Steps import java.sql.* Load a driver instance Establish Connection Create a Statement Query

16 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

17 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”,, ); Create a Statement Statement stmt = conn.createStatement ();

18 Murali Mani JDBC with mySQL You need to install the driver mySQL Connector/J from www.mysql.com Setenv CLASSPATH /mysql-connector- java-3.1.0-stable-bin.jar

19 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/ ”,, ); Create a Statement Statement stmt = conn.createStatement ();

20 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);

21 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”)); }

22 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 ());

23 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


Download ppt "Murali Mani Web Interface. Murali Mani Options PHP with mySQL JSP/Servlets with JDBC on mySQL/Oracle Others …"

Similar presentations


Ads by Google