Download presentation
Presentation is loading. Please wait.
1
1 Foundations of Software Design Lecture 27: Java Database Programming Marti Hearst Fall 2002
2
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
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
4 Mysql DBMS Oracle DBMS Mysql DriverOracle Driver Driver Manager JDBC API Java Application
5
5 The Java Process Connects to the DBMS Server Java Application Process Database Server
6
6 Can also connect to a Web Server Java Application Process Database Server Web Server
7
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
8
8 SQL Query: Use
9
9 SQL Query: Show
10
10 SQL Query: Describe
11
11 SQL Query: Select
12
12 SQL Query: Describe
13
13 SQL Query: Select
14
14 SQL Query: Count(*)
15
15
16
16 SQL Query: Create and Insert
17
17 SQL Query: Load (Input text file is tab-delimited)
18
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”;
19
19
20
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
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
22
22
23
23
24
24
25
25
26
26
27
27
28
28 Assignment Write java code to create and then use the contents of a DBMS database Use supplied code; go from there.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.