Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 90 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
90
Dung lượng
267 KB
Nội dung
Môn Lập trình Java NC Môn Lập trình Java NC Chương 3: DatabaseProgrammingJDBCChương 3: DatabaseProgrammingJDBC 2Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( JDBC) Content Content 0. Introduction 0. Introduction 1. The design of JDBC 1. The design of JDBC 2. The Structured Query Language 2. The Structured Query Language 3. Installing JDBC3. Installing JDBC 4. Basic JDBCprogramming concepts 4. Basic JDBCprogramming concepts 5. Executing Queries 5. Executing Queries 6. Scrollable and Updatable ResultSet 6. Scrollable and Updatable ResultSet 7. Metadata 7. Metadata 8. Transactions 8. Transactions 9. Advanced Connection management 9. Advanced Connection management 3Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( JDBC) 0. Introduction 0. Introduction DatabaseDatabase Collection of data Collection of data DBMS DBMS Database management system Database management system Stores and organizes data Stores and organizes data SQL SQL Relational database Relational database Structured Query Language Structured Query Language 4Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( JDBC) 0. Introduction (cont.) 0. Introduction (cont.) RDBMS RDBMS Relational database management system Relational database management system Relational database Relational database Table Table Rows, columns Rows, columns Primary key Primary key Unique data Unique data 5Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( JDBC) 0. Introduction (cont.) 0. Introduction (cont.) ODBC overview ODBC overview ODBC is an API introduced by Microsoft that allows ODBC is an API introduced by Microsoft that allows applications to access databases by using SQL. applications to access databases by using SQL. By using ODBC, a single application can access remote By using ODBC, a single application can access remote databases under different DBMSs (i.e. Informix, Oracle, databases under different DBMSs (i.e. Informix, Oracle, Sybase) Sybase) ODBC relies on data drivers to convert the ODBC calls to ODBC relies on data drivers to convert the ODBC calls to different database formats. different database formats. 6Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( JDBC) 0. Introduction (cont.) 0. Introduction (cont.) ODBC overview (cont.) ODBC overview (cont.) At development time, the application developer only needs At development time, the application developer only needs to know the ODBC calls to connect to a database, execute to know the ODBC calls to connect to a database, execute SQL statements, and retrieve results. SQL statements, and retrieve results. Main Components of ODBC: Main Components of ODBC: Application Application Driver manager Driver manager Driver Driver Target-database middleware Target-database middleware 7Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( JDBC) 0. Introduction (cont.) 0. Introduction (cont.) ODBC overview (cont.) ODBC overview (cont.) Application : Application : Requests a connection with a target database Requests a connection with a target database Passes one or more SQL statements Passes one or more SQL statements Processes the produced results or error conditions Processes the produced results or error conditions Ends each transaction with a commit or rollback Ends each transaction with a commit or rollback Terminates the connection. Terminates the connection. 8Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( JDBC) 0. Introduction (cont.) 0. Introduction (cont.) ODBC overview (cont.) ODBC overview (cont.) Driver Manager Driver Manager : : Loads drivers on an as-needed basis for Loads drivers on an as-needed basis for applications applications Maps a target database name to a specific driver Maps a target database name to a specific driver Processes several ODBC initialization calls Processes several ODBC initialization calls Validates the parameters and sequences of ODBC Validates the parameters and sequences of ODBC calls calls 9Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( JDBC) 0. Introduction (cont.) 0. Introduction (cont.) ODBC overview (cont.) ODBC overview (cont.) Driver : Driver : Processes the ODBC function calls Processes the ODBC function calls Modifies them to the target database format Modifies them to the target database format Submits the SQL statements to the target database, Submits the SQL statements to the target database, receives the results, and presents the results to the receives the results, and presents the results to the application. application. 10Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( JDBC) 0. Introduction (cont.) 0. Introduction (cont.) ODBC overview (cont.) ODBC overview (cont.) Target-Database Middleware : Target-Database Middleware : Includes the database needed by the applications Includes the database needed by the applications and the associated middleware and the associated middleware Database may be located on the same machine or Database may be located on the same machine or remote machine. remote machine. [...]... your database from specifie folder Press OK to finish Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( 21 4 Basic JDBCprogramming concepts Database URLs: When connecting to a database, you must specify the data source and you may need to specify additional parameters Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( 22 4 Basic JDBCprogramming concepts Database. .. Oriented Programming 2 - Chapter 4: DatabaseProgramming ( 13 RDBMS 1 The design of JDBC (cont.) Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( 14 1 The design of JDBC (cont.) JDBC drivers are classified into the following types: type 1 driver translates JDBC to ODBC and relies on an ODBC driver to communicate with the database type 2 driver is a driver, written partly in the Java programming. .. “sun .jdbc. odbc.JdbcOdbcDriver”; Class.forName(url); 2 Putting database URL: String dbURL= "jdbc: odbc:yourDataSource"; 3 Create Connection object: Connection conn = DriverManager.getConnection(dbURL); Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( 24 4 Basic JDBCprogramming concepts (cont.) Connect to Microsoft Access Class.forName("sun .jdbc. odbc.JdbcOdbcDriver"); String database. .. data types are not exactly the same See below: Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( 30 4 Basic JDBCprogramming concepts (cont.) Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( 31 4 Basic JDBCprogramming concepts (cont.) Advanced SQL Types In addition to numbers, strings, and dates, many databases can store large objects such as images or other data... "jdbc: odbc:Driver= {Microsoft Access Driver (*.mdb)};DBQ=yourDB.mdb"; Connection conn = DriverManager.getConnection( database, "Admin","" ); //do something after connect… Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( 25 4 Basic JDBCprogramming concepts (cont.) Connect to Microsoft SQLServer 2000 Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( 26 4 Basic JDBC programming. .. design of JDBC JDBC is a Java-based counterpart to ODBC JDBC provides a standard API for tool /database developers and makes it possible to write database applications using a pure Java API Database developers will supply JDBC drivers for their databases so that Java programs may access them transparently Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( 11 1 The design of JDBC (cont.)... independent of the actual database, thus simplifying deployment type 4 driver is a pure Java library that translates JDBC requests directly to a database- specific protocol Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( 16 1 The design of JDBC (cont.) Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( 17 2 The Structured Query Language JDBC is an interface to... language Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( 19 3 Installing JDBC On Windows, it already include the JDBC/ ODBC bridge driver We can use driver for Ms Access without installing any thing With MS SQL Server 2000, we must install the driver to use for accessing data Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( 20 3 Installing JDBC (cont.) To configure... API would not be desirable (void pointers) JDBC is easier to learn A Java API like JDBC is needed in order to enable a “pure java” solution Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( 12 1 The design of JDBC (cont.) JDBC Objects and interfaces Java Appl JDBC Runtime Java to C JDBCODBC Driver DB specifics ODBC Driver ODBC APIs Windows 3G/4G Appl ODBC Interface Initialization, Driver... Syntax: jdbc: subprotocol name:other_stuff subprotocol is used to select the specific driver for connecting to the database The format for the other stuff parameter depends on the subprotocol used You need to look up your vendor's documentation for the specific format Example : jdbc: odbc:your_Datasource Object Oriented Programming 2 - Chapter 4: DatabaseProgramming ( 23 4 Basic JDBCprogramming . Programming ( JDBC) 1. The design of JDBC 1. The design of JDBC JDBC is a Java-based counterpart to ODBC JDBC is a Java-based counterpart to ODBC JDBC provides a standard API for tool/database JDBC. JDBC 1. The design of JDBC 2. The Structured Query Language 2. The Structured Query Language 3. Installing JDBC 3. Installing JDBC 4. Basic JDBC programming concepts 4. Basic JDBC programming concepts 5 Programming 2 - Chapter 4: Database Programming ( JDBC) 1. The design of JDBC (cont.) 1. The design of JDBC (cont.) Java Appl. JDBC Runtime JDBC- ODBC Driver ODBC Driver RDBMS ODBC Interface ODBC Runtime Windows 3G/4G Appl. DB specifics Java