1 Database Management Systems (II) Chapter 14. JDBC, Java Server Pages, and MySQL © 2005 by Dr. F. Lin.

Slides:



Advertisements
Similar presentations
Lecture plan Information retrieval (from week 11)
Advertisements

Java Database Connectivity (JDBC). 2/24 JDBC (Java DataBase Connectivity) - provides access to relational database systems JDBC is a vendor independent.
Distributed Application Development B. Ramamurthy.
Fundamentals, Design, and Implementation, 9/e Chapter 14 JDBC, Java Server Pages, and MySQL.
15 Chapter 15 Web Database Development Database Systems: Design, Implementation, and Management, Fifth Edition, Rob and Coronel.
B.Sc. Multimedia ComputingMedia Technologies Database Technologies.
28/1/2001 Seminar in Databases in the Internet Environment Introduction to J ava S erver P ages technology by Naomi Chen.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic Server Side Web Technologies: Part 2.
1 C. Shahabi Application Programming for Relational Databases Cyrus Shahabi Computer Science Department University of Southern California
DT228/3 Web Development Databases. Database Almost all web application on the net access a database e.g. shopping sites, message boards, search engines.
1 CS6320 – Why Servlets? L. Grewe 2 What is a Servlet? Servlets are Java programs that can be run dynamically from a Web Server Servlets are Java programs.
Java database Programming JDBC Trademarked name of a Java API that supports Java programs that access relational databases Stand for Java DataBase Connectivity.
1 Java Server Pages Can web pages be created specially for each user? What part does Java play?
Java Servlets. What Are Servlets? Basically, a java program that runs on the server Basically, a java program that runs on the server Creates dynamic.
ODBC and JDBC What are they – libraries of function calls that support SQL statements Why do we need them –Provide a way for an application to communicate.
Intro to Servlets Lec 26. Web-Based Enterprise Applications in Java Figure shows a simplified view of one application and its layers.
UFCE4Y UFCE4Y-20-3 Components and Services Julia Dawson.
Overview of JSP Technology. The need of JSP With servlets, it is easy to – Read form data – Read HTTP request headers – Set HTTP status codes and response.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
1 Web Database Processing. Web Database Applications Static Report Publishing a report is prepared from a database application and exported to HTML DB.
INTRODUCTION TO WEB DATABASE PROGRAMMING
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
Applets & Servlets.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
Database Management Systems 1 Oracle Programming.
4-1 INTERNET DATABASE CONNECTOR Colorado Technical University IT420 Tim Peterson.
Basics of Web Databases With the advent of Web database technology, Web pages are no longer static, but dynamic with connection to a back-end database.
© Wang Bin 2004 JDBC ----Java Database Connectivity.
CSE470 Software Engineering Fall Database Access through Java.
JDBC. JDBC Drivers JDBC is an alternative to ODBC and ADO that provides database access to programs written in Java.
Fundamentals of Database Chapter 7 Database Technologies.
MySQL, Java, and JDBC CSE 3330 Southern Methodist University.
HTML. Principle of Programming  Interface with PC 2 English Japanese Chinese Machine Code Compiler / Interpreter C++ Perl Assembler Machine Code.
Designing and Developing WS B. Ramamurthy. Plans We will examine the resources available for development of JAX-WS based web services. We need an IDE,
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Putting it all together Dynamic Data Base Access Norman White Stern School of Business.
Servlets Database Access. Agenda:  Setup Java Environment  Install Database  Install Database Drivers  Create Table and add records  Accessing a.
Chapter 6 Server-side Programming: Java Servlets
1 CS122B: Projects in Databases and Web Applications Spring 2015 Notes 03: Web-App Architectures Professor Chen Li Department of Computer Science CS122B.
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications1.
Accessing Database using JDBC. JDBC Objectives Gain basic knowledge of Java JDBC Become familiar with the basics of interacting with a database using.
1 MSCS 237 Overview of web technologies (A specific type of distributed systems)
JDBC Database Programming in Java Prepared by., Mrs.S.Amudha AP/SWE.
12/6/2015B.Ramamurthy1 Java Database Connectivity B.Ramamurthy.
Java and Databases. JDBC Architecture Java Application JDBC API Data Base Drivers AccessSQL Server DB2InformixMySQLSybase.
COMP 321 Week 4. Overview Normalization Entity-Relationship Diagrams SQL JDBC/JDBC Drivers hsqldb Lab 4-1 Introduction.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
CSI 3125, Preliminaries, page 1 JDBC. CSI 3125, Preliminaries, page 2 JDBC JDBC stands for Java Database Connectivity, which is a standard Java API (application.
Basics of JDBC.
Basics of JDBC Session 14.
Java Servlets and Java Server Pages
JDBC Chapter 1 JDBC Introduction
JDBC - Java Database Connectivity. JDBC provides Java applications with access to most database systems via SQL The architecture and API closely resemble.
Ch. NoNameMarks 01AWT24 02Networking18 03JDBC20 04Swing18 05Servlet20 Advance Java Programming.
JAVA SERVER PAGES -by Rubeena Memon Deepti Jain Jaya Thakar Jisha Vettuventra.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 Java Database Connectivity.
1 Download current version of Tomcat from: g/tomcat/ g/tomcat/ Install it in: C:\Program Files\Apache.
A Presentation Presentation On JSP On JSP & Online Shopping Cart Online Shopping Cart.
Chapter 14. JDBC, Java Server Pages, and MySQL
JDBC Database Management Database connectivity
HW#4 Making Simple BBS Using JDBC
PHP / MySQL Introduction
MSIS 655 Advanced Business Applications Programming
Mr. Harish Sharma Asst. Professor Dept. of CA & IT SGRRITS Dehradun
Introduction of Week 11 Return assignment 9-1 Collect assignment 10-1
JAVA DATABaSE CONNECTIVITY
Presentation transcript:

1 Database Management Systems (II) Chapter 14. JDBC, Java Server Pages, and MySQL © 2005 by Dr. F. Lin

2 JDBC, JSP and MySQL Alternative to Microsoft solutions for distributed database access –Open-source software JDBC (not an acronym) Java Server Pages (JSP) using Apache/ Tomcat MySQL driver (an open-source DBMS developed by M. Mathews) –Running in open environments Linux, and also Unix Proprietary systems such as Windows

3 JDBC –Allowing programmers to connect to many databases with JDBC drivers For a listing of drivers, see: –JDBC architecture and components (see figure) Objects in applications: Connection, Statement, ResultSet, ResultSetMetaData Driver manager Driver Database and DBMS

4 Driver Manager DBMS Driver 1 DBMS Driver 2 JDBC-ODBC Bridge DBMS 1 DBMS 2 ODBC Driver DB JDBC Architecture Applications Connection Statement ResultSet ResultSetMetaData CallableStatement PreparedStatement DB

5 JDBC driver types –Type 1: JDBC-ODBC bridge drivers –Type 2: connects to the native API of the DBMS –Type 3: translates JDBC into DBMS- independent network protocol Can be used for servlets and applets –Type 4: translates JDBC into DBMS-specific network protocol Can be used for servlets and applets

6 JDBC Driver Types Driver Type Characteristics 1JDBC-ODBC bridge drivers. Provides a Java API that interfaces to an ODBC driver. Enables processing of ODBC data sources from Java 2A Java API that connects to the native library of a DBMS product. The Java program and the DBMS must reside on the same machine, or the DBMS program must handle inter-machine communication, if not 3A Java API that connects to a DBMS-independent network protocol. Can be used for servlets and applets 4A Java API that connects to a DBMS-dependent network protocol. Can be used for servlets and applets

7 Java programming language –Java was designed to be portable A Java program is not compiled into machine language, instead, into machine-independent bytecode (portability) Bytecode is interpreted by the Java virtual machine (JVM) JVM: a bytecode interpreter that is needed to run Java programs Different interpreters for different machine environments (Intel, Alpha, etc.)

8 Java performance Be interpreted, not as fast as code that has been translated into machine language –This may or may not a problem, depending on the application’s workload.

9 Java servlet –A servlet is a Java program invoked via HTTP on the Web server –The servlet uses the JVM running on the server Java applet –An applet is Java bytecode that is designed to run on a client –Applet bytecode is sent to the client machine using HTTP. It is read and interpreted by the Web browser. Most, if not all, Web browsers now run the Java virtual machine servlets JVM Applet HTTP Web Browser Web Browser Web Browser

10 Using JDBC –No JDBC data source: a connection is built in Java codes via JDBC driver –Steps for using JDBC drivers Load the driver  Establish a connection to the database  Create a statement  Process the statement –Loading the driver The directory containing the driver library must be part of the CLASSPATH for the Java compiler and the Java virtual machine The JDBC-ODBC Bridge driver is installed automatically with the Java 2 SDK, Standard Edition, as package sun.jdbc.odbc

11 While there are several ways to load the drivers, the following is one of the most reliable… –Class.forName(“nameofdriver”).newInstance() –Establishing a connection to the database Create an object that has a connection to the database… –Connection Conn = DriverManager.getConnection(“connectionString”); –Example: DriverManager.getConnection(“jdbc:mysql: //localhost/db1?user=abc&password=pwd”);

12 Details regarding the JDBC connection string: “ connectionString” has three parts (each part is delimited with a colon) Part 1: jdbc: Part 2: a keyword that identifies the DBMS, e.g., mysql: Part 3: a URL for the database, e.g., //localhost/db1?user=abc&password=pwd –Creating a statement A statement is created by instantiating a new Statement object –Statement Stmt = Conn.createStatement();

13 –Processing the statement The driver’s API documentation will provide details on what statements are available. However, the core statements are standard –Note: a driver’s API is a set of functions that the program may call to receive services Examples (also see the codes): ResultSet RS = Stmt.executeQuery(“select * from tab”); int result = Stmt.executeUpdate(“update tab set name=‘abc’ where id = 1”); –Prepared statement and callable statement Used to invoke compiled queries and stored procedures in database Similar to command (but not supported by MySQL)

14 An example of JDBC-ODBC and Java 1.Build a simple database with Microsoft Access named db1.mdb, and create a table in db1.mdb: named CUSTOMER, which has three fields: CustomerID, Name, AreaCode, PhoneNumber, Street, City, State, and Zip; input some data into the table; save the database in somewhere in the disk, say C:\. 2. Create a data source for db1.mdb: open the “Control Panel” of Windows 2000/XP; From the item “Administrative Tools”, find the icon for “Data Sources (ODBC)”, and then click on the icon, from the window “ODBC Data Source Administrator” click on the tab “System DSN”,

15 An Example (con’t) –Add a data source, select “Microsoft access driver” and click OK, enter a name for the data source, “test”; and click the tab “select” and find and select the database we just built, db1.mdb. Press “OK”. Now we have created a data source in our Window environment. –Install Java 1.4 or higher on your machine and edit the example in Figure 14-3;

16 Figure 14-3 (modified) import java.io.*; import java.sql.*; public class GeneralTable { public static void main(String[] args) { if (args.length <1) { System.out.println(Insufficient data provided.”); return; } String varTableName= args[0]; varTableName = varTableName.toUpperCase(); System.out.println("Showing Table " + varTableName); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); String connString="jdbc:odbc:test"; Check for at least one parameter Set the variable varTableName to the input table name and convert that name to uppercase. JDBC-ODBC driver is loaded The Bridge driver uses the odbc subprotocol. URLs for this subprotocol are of the form: jdbc:odbc: [; = ]* The Bridge is installed automatically with the Java 2 SDK, Standard Edition, as package sun.jdbc.odbc

17 Figure 14-3 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(); String varColNames = ""; int varColCount = rsMeta.getColumnCount(); for (int col =1; col <= varColCount; col++) { varColNames = varColNames + rsMeta.getColumnName(col) + " "; } System.out.println(varColNames); while (rs.next()) { for (int col=1; col<=varColCount; col++) { System.out.print(rs.getString(col) + " "); } System.out.println(); } rs.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } }

18 An example (con’t) –Save it in C:\wan\GeneralTable.java –Compile: C:\wan>javac GeneralTable.java –Run it: C:\wan>java GeneralTable customer

19 Questions Can JDBC drivers be imported into Java Programs?

20 Figure 14-4 –Implement the logic for the View Ridge CustomerInsert procedure in Chapter 7: Accept four parameters: a new customer’s Name, AreaCode, LocalNumber, and the Nationality of all artists in whom the customer maintains an interest.

21 import java.io.*; import java.sql.*; public class CustomerInsert { public static void main(String[] args) { if (args.length<4) { System.out.println("insufficient data provided."); return; } String varName = args[0]; String varAreaCode = args[1]; String varLocalNumber= args[2]; String varNationality = args[3]; insertData(varName, varAreaCode, varLocalNumber, varNationality); } public static void insertData(String varName, String varAreaCode, String varLocalNumber, String varNationality) { System.out.println("Adding row for " + varName); To be transformed into a java bean

22 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(); // Now check for duplicated data String varSQL = "SELECT Name "; String varWhere = "FROM CUSTOMER WHERE Name= '"; varWhere = varWhere + varName + "' AND AreaCode = '"; varWhere = varWhere + varAreaCode + "' AND PhoneNumber = '"; varWhere = varWhere + varLocalNumber + "'"; varSQL = varSQL + varWhere; ResultSet rs= stmt.executeQuery(varSQL); while (rs.next()) { // if get here, there is duplicate data System.out.println("Data Duplicates an existing customer. No change Made."); rs.close(); stmt.close(); conn.close(); return; }

23 // OK to insert new data varSQL = "INSERT INTO CUSTOMER (Name, AreaCode, PhoneNumber)"; varSQL = varSQL + " VALUES ('" + varName + "', '"; varSQL = varSQL + varAreaCode + "', '"; varSQL = varSQL + varLocalNumber + "')"; int result = stmt.executeUpdate(varSQL); if (result == 0) { System.out.println("Problem with Insert"); rs.close(); stmt.close(); conn.close(); return; } C:\wan>javac CustomerInsert.java C:\wan>java CustomerInsert bush us C:\wan>java GeneralTable customer

24 Needs in Developing Web Applications Create simple interactive Web pages separate content generation from presentation take advantage of reusable, portable objects, simplifying the maintenance of Web applications. Support complex web sites that are fully integrated with enterprise-level applications

25 What is JSP? Java Server Pages (JSP) is a technology that lets you mix regular, static HTML with dynamically-generated HTML. JSP lets you create the two parts separately. Here's an example: Today is:.

26 JSP (Java Server Pages) JSP: providing a means of creating dynamic Web pages using HTTP, XML, and Java JSP only allows for Java coding, ASP only allows for JavaScript or VBScript coding JSP inherits the power and portability of Java –Since Java is powerful and platform-independent, so is JSP –JSP Specifications are located at:

27 JSP JSP technology separates content generation from presentation and takes advantage of reusable tags and objects, simplifying the maintenance of your web applications. JSP technology provides the scripting ability you need to create simple interactive Web pages, or it scales to support complex web sites that are fully integrated with enterprise class applications.

28 JSP and Servlets –JSP are transformed into standard Java code (in particular, Java servelet), then they are compiled into bytecode (just like a regular Java program) Similar to ASP but fundamentally different in underlying technology Machine independent JSP: subclasses of the HttpServlet class Consequently, due to inheritance, you may embed Java code enclosed by directly into JSP

29 What are Java Servlets? Servlets are Java technology's answer to CGI programming. They are programs that run on a Web server and build Web pages. Building Web pages on the fly is useful for a number of reasons: The Web page is based on data submitted by the user. For example the results pages from search engines are generated this way, and programs that process orders for e-commerce sites do this as well. The data changes frequently. For example, a weather-report or news headlines page The Web page uses information from corporate databases or other such sources. For example, you would use this for making a Web page at an on-line store that lists current prices and number of items in stock.

30 What are the Advantages of JSP? JSP vs. Pure Servlets. –JSP doesn't give you anything that you couldn't in principle do with a servlet. But it is more convenient to write (and to modify!) regular HTML than to have a zillion println statements that generate the HTML. –By separating the look from the content you can put different people on different tasks: your Web page design experts can build the HTML, leaving places for your servlet programmers to insert the dynamic content.

31 JSP vs. JavaScript JavaScript can generate HTML dynamically on the client. This is a useful capability, but only handles situations where the dynamic information is based on the client's environment. With the exception of cookies, HTTP and form submission data is not available to JavaScript. And, since it runs on the client, JavaScript can't access server-side resources like databases, catalogs, pricing information, and the like.

32 –Web Server Applications To run JSP, the Web server must implement: –Java servlet 2.1+, and –Java Server Pages 1.0+ Apache web server: not support servlets Apache TomCat: implemented the above specifications –Apache Tomcat Apache Tomcat: a common Web server application  a servlet processor – –May run in conjunction with Apache (in a production environment) or standalone (in a development environment)

33 Setting up Tomcat for JSP –Different ports for Apache and Tomcat –Directory structure