1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002.
Published byModified over 4 years ago
Presentation on theme: "1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002."— Presentation transcript:
1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002
2 Accessing DBMSs DBMS: DataBase Management System SQL: standard query language –Typed at command line –Forms-based interfaces –Direct-manipulation interfaces Programming APIs: –ODBC is an implementation API for SQL Open DataBase Connectivity A platform-independent standard JDBC is a java version of this –Special scripting languages also exist PHP, Cold Fusion, etc.
3 Programming DBMSs The DBMS Server –A continuously running process –Supports many different databases simultaneously The data can be owned by different people Each database within the DBMS has its own name, owners, password, tables –The application program connects to the DBMS server First makes a connection Then enters a loop of issuing queries and receiving results Somewhat like a web server in this respect
4 Mysql DBMS Oracle DBMS Mysql DriverOracle Driver Driver Manager JDBC API Java Application
5 The Java Process Connects to the DBMS Server Java Application Process Database Server
6 Can also connect to a Web Server Java Application Process Database Server Web Server
7 Using SQL Issue a query to the database Types of queries: –Find out which tables are available –Add a table –Add data to a table –Update data in a table –Select rows from a table –Join rows across two or more tables –… many more
17 SQL Query: Load (Input text file is tab-delimited)
18 Sample SQL Join A join combines rows from different tables select t1.item, t1.name from items as t1, item_nature as t2, nature as t3 where t1.item = t2.item and t2.id = t3.id and t3.name = “hare”;
20 Using the JDBC API import java.sql.*; http://java.sun.com/j2se/1.4/docs/api/java/sql/package-summary.html Facilities: –Making a connection with a database via the DriverManager facility –Sending SQL statements to a database –Retrieving and updating the results of a query –Standard mappings for SQL types to classes and interfaces in the Java programming language –Custom mapping an SQL user-defined type (UDT) to a class in the Java programming language –Metadata –Exceptions
21 Using SQL in JDBC Compose a Query –Represented as a String Create a Statement Instantiate Statement with a Query Execute the Query –Send it to the DBMS Receive the ResultSet –Optionally extract the ResultSetMetaData Process the contents of the ResultSet Repeat