Professional Documents
Culture Documents
Ni dung bi hc
Tm hiu JDBC API v cc JDBC Drivers M hnh 2-tier v 3-tier Cc bc truy xut CSDL dng JDBC Cc v d minh ho Transactions Sau bi hc ny, sinh vin c th vit c cc chng trnh truy xut CSDL bng ngn ng Java
JDBC API
JDBC API vit tt ca Java Database Connectivity Application Programming Interface Cung cp cc lp v cc interface h tr Java program c th truy xut database
JDBC Drivers
Gip cho cc ng dng truy xut CSDL theo mt chun chung khng phc thuc vo DBMS JDBC Driver nhn cc requests t client, converts sang dng m Database c th hiu c. Ngc li, JDBC Driver s nhn cc response, dch sang Java data format, client application c th hiu c -> Java-to-SQL translator
java.sql package
Cha cc interfaces v classes c nh ngha trong JDBC API access database Cc Interfaces ca java.sql package:
CallableStatement Connection DatabaseMetaData Driver PreparedStatement ResultSet ResultSetMetaData Statement
SQLWarning
5
JDBC Drivers
JDBC driver thch hp vi m hnh client/server C 4 loi JDBC drivers: JDBC-ODBC Bridge
JDBC-ODBC Bridge
c h tr bi JavaSoft. Dng c cho nhiu databases. S dng dch v DataSource ODBC
7
Native-API-Partly-Java Driver
S dng local native libraries trao i vi database bng cch dng CLI (Call Level Interface) Khi client gi request, driver s dch JDBC request sang native method call v chuyn request cho native CLI
JDBC-Net-All-Java Driver
Khc vi 2 drivers trc l v tr ca native database access libraries Native CLI libraries c t trn remote server v driver dng network protocol cho vic trao i gia application v driver Driver c chia thnh 2 phn: mt phn cha tt c cc phn Java m c th download v cho client v phn server cha c hai Java v native methods
Native-Protocol-All-Java Driver
100% Java v khng dng CLI libraries C kh nng trao i vi database mt cch trc tip m khng cn translation
10
12
13
14
CC BC CSDL
1. 2. 3. 4. Import java.sql package Load v ng k driver Thit lp connection n database server To i tng statement: Statement/PreparedStat ement/CallableStatemen t Thc hin lnh SQL Nhn kt qu tr v v x l ng statement v connection
TRUY
XUT
5. 6. 7.
15
19
V d chng trnh to table Departments gm 2 field: DepID v DepName trong file HRManagement.mdb dng JDBC-ODBC driver 1. Tao file HRManagement.mdb 2. Thiet lap Data source ODBC vi data source name (DSN) la DATA (khong bat buoc phai trung ten file mdb) 3. Viet chng trnh Java e thc hien truy xuat CSDL
21
Lu
Neu cu lenh sql la lenh select th phai dung phng thc executeQuery() cua oi tng statement va ket qua tra ve se la mot oi tng ResultSet. Neu lenh sql khong la lenh select th phai dung phng thc executeUpdate() cua oi tng statement va ket qua tra ve se mot so nguyen. Xem them cac interface va cac lp Connection, Statement, PreparedStatement, CallableStatement, ResultSet
23
Interface ResultSet
c to ra t phng thc executeQuery() ca cc i tng Statement, PreparedStatement, CallableStatement.. Cha cc d liu c tr v t lnh select query cha mt cursor ch n record hin hnh. Khi resultset va c to ra, cursor s ch n before the first record Dng phng thc next() di chuyn cursor n record k tip, nu n after the last record th tr v gi tr false Default, ResultSet l readOnly v forward only
25
Interface ResultSetMetaData
Cung cp cc hng v cc methods c dng ly cc thng tin v i tng ResultSet.
int getColumnCount():Returns the number of columns in this ResultSet String getColumnLabel(int column) : Gets the designated column's suggested title for use in printouts and displays. String getColumnName(int column): Get the designated column's name. int getColumnType(int column): Retrieves the designated column's SQL type. String getColumnTypeName(int column) : Retrieves the designated column's database-specific type name. String getTableName(int column): Gets the designated column's table name.
31
V d dng ResultSetMetadata
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:data"); Statement st = cn.createStatement(); String sqlstr= "select * from departments"; ResultSet rs =st.executeQuery(sqlstr); ResultSetMetaData rsmd = rs.getMetaData(); while (rs.next()) { for (int i=1;i<=rsmd.getColumnCount();i++) { System.out.println(rsmd.getColumnLabel(i) + ": " + rs.getString(i)); } }
32
Interface PreparedStatement
L interface con ca Statement. Cu lnh SQL c precompiled v stored trong PreparedStatement object v c th c thc hin nhiu ln. Cu lnh SQL c th c nhiu tham s, v c nhiu lnh setXXX(index, value) t gi tr cho tham s. V d: PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES SET SALARY = ? WHERE ID = ?"); pstmt.setBigDecimal(1, 153833.00) ; pstmt.setInt(2, 110592) ; (Xem thm interface PreparedStatement)
33
35
36
37
Transaction
Khi chng ta mun thc hin mt dy cc lnh cp nht CSDL v mun rng lnh trc hon thnh ch khi lnh cc lnh sau cng phi hon thnh -> dng transaction Khi mt connection sau khi c to th ch Auto commit c bt ln. Mun to ra mt transaction, ta phi tt ch ny: con.setAutoCommit(false); Nu cc lnh trong transaction u thc hin thnh cng th gi phng thc commit ca connection Nu c mt lnh trong transaction khng thc hin c, dng phng thc rollback rollback transaction
38
v d v transaction
try{ con.setAutoCommit(false); // bt u transaction PreparedStatement updateSales = con.prepareStatement( "UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ?"); updateSales.setInt(1, 50); updateSales.setString(2, "Colombian"); updateSales.executeUpdate(); PreparedStatement updateTotal = con.prepareStatement( "UPDATE COFFEES SET TOTAL = TOTAL + ? WHERE COF_NAME LIKE ?"); updateTotal.setInt(1, 50); updateTotal.setString(2, "Colombian"); updateTotal.executeUpdate(); con.commit(); // kt thc transaction thnh cng con.setAutoCommit(true); }catch( Exception e){ con.rollback(); }
39