Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Download current version of Tomcat from: g/tomcat/ g/tomcat/ Install it in: C:\Program Files\Apache.

Similar presentations


Presentation on theme: "1 Download current version of Tomcat from: g/tomcat/ g/tomcat/ Install it in: C:\Program Files\Apache."— Presentation transcript:

1 1 Download current version of Tomcat from: http://jakarta.apache.or g/tomcat/ http://jakarta.apache.or g/tomcat/ Install it in: C:\Program Files\Apache Software Foundation\Tomcat 5.5\ Test it: http://localhost:8080/ Directory structure \Tomcat 5.5 \webapps\common\bin…\lib \ROOT…

2 2 An simple JSP example: test.jsp A Test of JSP Welcome! Today is:.

3 3 An simple JSP example (con’t) Edit it using an editor, e.g. WordPad. Save it in: C:\Program Files\Apache Software Foundation\Tomcat5.5\webapps\ROOT\test.jsp Start Tomcat: C:\Program Files\Apache Software Foundation\Tomcat5.5\bin\tomcat5 Open a Web browser and go to: http://localhost:8080/test.jsp

4 4 Running JSP JSP compilation process: parse, compile, load and execute JSP Page Request JSP Servlet Current? JSP Servlet in Memory? Parse JSP and Create Java Source File Compile Java Source File Execute the JSP servlet Load the JSP servlet JSP Page Response no yes no

5 5 Figure 14-6: GeneralTable.jsp Table Display Using JDBC and MySQL Database Access Example <%String varTableName= request.getParameter("Table"); varTableName = varTableName.toUpperCase(); %> Showing Data from MySQL Database db1

6 6 <% try { // Load JDBC driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); String connString="jdbc:odbc:test"; System.out.println("Trying connection with "+connString); Connection conn = DriverManager.getConnection(connString); Statement stmt = conn.createStatement(); String varSQL = "SELECT * FROM " + varTableName; ResultSet rs = stmt.executeQuery(varSQL); ResultSetMetaData rsMeta = rs.getMetaData(); %>

7 7 <% String varColNames = ""; int varColCount = rsMeta.getColumnCount(); for (int col =1; col <= varColCount; col++) { %> <% while (rs.next()) { %> <% for (int col=1; col<=varColCount; col++) { %> <FONT SIZE=2 FACE="Arial" COLOR=#000000 > <% } }

8 8 //clean up rs.close(); stmt.close(); conn.close(); } catch (ClassNotFoundException e) { out.println("Driver Exception " + e); } %> Example: Run it!Run it!

9 9 Java Bean Model 1.

10 10 JSP invoking Java Classes (Java Beans) Important and Useful: –It separates the tasks of writing program logic from generating HTML; –It Reduces the complexity of managing a Web site. Java Bean –A properly mannered Java class; –A Java class that has three properties: there are no public instance variables; All persistent values xxx are accessed using methods named getxxx and setxxx Bean classes must either have no constructors or it must have one explicitly defined zero- argument constructor

11 11 An example using Java Bean Save it into: C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT\WEB-INF\classes\ Compile it: >javac CustomerInsertBean.java We get CustomerInsertBean.class Edit NewCustomer.html Save it into: C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT\ Edit CustomerInsertUsingBean.jsp Save it into C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT\ Start Tomcat C:\Program Files\Apache Software Foundation\Tomcat 5.5\bin\tomcat5

12 12 An Example using Java Bean and JDBC JSP: Display the result set retrieved from Table Testinfo in the database db1 Use the Java Bean DBUtil_MySQL.class Java: Load the JDBC driver Set up the connection Get the ResultSet

13 13 An example using Java Bean and MySQL // A Java Bean source code: DBUtil_MySQL.java package lin; import java.io.*; import java.sql.*; public class DBUtil_MySQL { // For MySQL String sDBDriver="com.mysql.jdbc.Driver"; String sConnStr="jdbc:mysql://localhost:3307/"+"db1"+"?user=root"; // for MS ACCESS //String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver"; //String sConnStr="jdbc:odbc:test"; Connection conn = null; ResultSet rs = null; public DBUtil_MySQL() { try { //java.sql.DriverManager.registerDriver(); Class.forName(sDBDriver); } catch(java.lang.ClassNotFoundException e) { System.err.println("testJDBC(): " + e.getMessage()); } } It is necessary to use a package, Otherwise something will go wrong.

14 14 An example using Java Bean and MySQL public ResultSet executeQuery(String testJDBC) { rs = null; try { conn=DriverManager.getConnection(sConnStr); Statement stmt = conn.createStatement(); rs=stmt.executeQuery(testJDBC); } catch(SQLException ex) { System.err.println("aq.executeQuery:"+ex.getMessage()); } return rs; } Save it in: \webapps\ROOT\WEB-INF\classes\DBUtil_MySQL.java >javac –d \ DBUtil_MySQL.java Copy the file we got from the compilation DBUtil_MySQL.class in C:\lin to C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT\WEB- INF\classes\lin\

15 15 testDB_MySQL.jsp MY FAQ This is my name! <%@ page language="java" import ="java.sql.*" pageEncoding="gb2312" %> <% ResultSet RS = workM.executeQuery("SELECT * FROM testinfo"); while (RS.next()) { ResultSet rs = workM.executeQuery("SELECT * FROM testinfo"); String str_name, str_gender; out.println("Get data from a database:: "); %> <% while (rs.next()) { str_name=rs.getString("name"); str_gender = rs.getString("gender"); out.println(" name is: " + str_name); out.println(" gender is: " + str_gender); %> <% } } RS.close(); %> Save it to: C:\Program Files\ Apache Software Foundation \Tomcat 5.5\webapps\ROOT\ Go to: http://localhost:8080/testDB_MySQL.jsp

16 16 MySQL Downloading MySQL –An open-source DBMS product that runs on Unix, Linux, and Windows Provides quick and efficient query handling Allows you to create users, databases, tables, auto incrementing fields, etc. –The MySQL open-source driver may be downloaded from: http://worldserver.com/mm.mysql

17 17 Get and Install a Recent Version Download the 4.0 and/or 4.1 versions of the MySQL win32 distribution.4.04.1 Find the downloaded file, unzip it and start the setup program: By referring to http://www.analysisandsolutions.com/co de/mybasic.htm http://www.analysisandsolutions.com/co de/mybasic.htm Install it to C:\Program Files\mysql40\ Start it: C:\Program Files\mysql40\bin\

18 18 –Limitations While MySQL is extremely inexpensive and provides many capabilities, it is not as powerful as commercial products, such as Oracle and SQL Server –Using My SQL MySQL commands: use, show, describe

19 19 Using MySQL >mysql –P 3307 –u root mysql >Show databases; >sHow databases; (OK) >Use db1; >Show tables; >Describe testinfo; >Select * from testinfo; Or >\q Edit sql.txt >bin>mysql –P 3307 –u root mysql <C:\wan\sql.txt

20 20 MySQL Connector/J –The latest version of the driver is MySQL Connector/J (formerly MM.MySQL driver) is a Java driver that converts JDBC calls into the network protocol used by the MySQL database. is a Type 4 JDBC driver and has a complete JDBC feature set that supports the capabilities of MySQL. Download the driver from http://dev.mysql.com/downloads/connector/j/3.1.html Unzip it to get the jar file: mysql-connector-java- 3.0.16-ga-bin.jar copy it to a public place: …\Tomcat 5.5\common\lib\mysql-connector-java-3.0.16-ga-bin.jar

21 21 –JDBC connections JDBC connection to MySQL is different from other user connections –Same machine: via socket –Different machines: via TCP/IP

22 22 –A work-around allows you to grant access to the database@’%’ for the specific account »The wildcard % states that the database may be accessed from any host »Alternatively, you may supply the specific IP address for your local machine –this provides better security –Concurrency control Limited support: read/write locks –Backup and recovery Limited support: database/table saving, log files

23 23 Summary JDBC –JDBC driver types –Java Servlet and Applets –JDBC architecture and components –Applications of JDBC JSP (Java Server Pages) –JSP, Servlets and Apache Tomcat MySQL –MySQL and JDBC Connections; Concurrency control, and backup and recovery Questions?


Download ppt "1 Download current version of Tomcat from: g/tomcat/ g/tomcat/ Install it in: C:\Program Files\Apache."

Similar presentations


Ads by Google