Professional Documents
Culture Documents
JDBC
JDBC
N i Dung
Gi i thi u JDBC Cc ki n trc ng d ng trn c s d li u K t n i vo c s d li u Th c thi cc cu l nh SQL qua JDBC S d ng m hnh giao d ch trn c s d li u
Gi i Thi u JDBC
JDBC: Java Database Connectivity M t t p cc API trn Java k t n i n cc h qu n tr c s d li u Cc interface v class ch y u c a JDBC n m trong gi java.sql M t vi interface v class c cc ch c nng nng cao n m trong gi javax.sql
ng D ng S
D ng
ng D ng S
D ng
K t N i Vo C S D
Li u
k t n i vo c s d li u c n ph i ci t JDBC drivers M i h qu n tr c s d li u th ng c m t s JDBC driver ring Thng tin v cc JDBC driver c th xem y:
http://developers.sun.com/product/jdbc/drivers
D ng C u N i JDBC-OBDC
C th k t n i v i c s d li u s d ng c u n i JDBC-ODBC
ODBC Open Database Connectivity do Microsoft xy d ng
Cc B c K t N i
Load jdbc driver c n s d ng
Class.forName(sun.jdbc.odbc.JbdcOdbcDriver);
T o k t n i (s d ng c u n i JDBC-ODBC)
Connection conn=DriverManager.getConnection(jdbc:odbc:<DataSourceName>);
DataSourceName l tn c a ODBC data source c t o trn MS Windows Cc jdbc driver khc nhau s d ng cc chu i k t n i khc nhau. C n c h ng d n t nh cung c p bi t thng tin ny
V d : K t N i V i CSDL
try{ Class.forName(driver); conn =DriverManager.getConnection(connURL); }catch(ClassNotFoundException e){ e.printStackTrace(); conn = null; }catch(SQLException sqle){ sqle.printStackTrace(); conn = null; }
T o B ng Qua JDBC
public void createTable(){ String sqlQuery = "CREATE TABLE PRICELIST(NAME VARCHAR(20), PRICE NUMBER);"; if (conn != null){ try{ Statement st = conn.createStatement(); st.execute(sqlQuery); st.close(); }catch(SQLException e){ e.printStackTrace(); } } }
Nh p D
Li u Qua JDBC
nh h ng sau khi cu
public void insertData(String name, double val){ String insertQuery = "INSERT INTO PRICELIST VALUES('" + name + "'," + val + ");"; if (conn != null){ try{ Statement st = conn.createStatement(); int d = st.executeUpdate(insertQuery); System.out.println("So dong duoc cap nhat: " + d); st.close(); }catch(SQLException e){ e.printStackTrace(); } } }
cD Li u
Li u T
C S D
V d
public void selectAll(){ String selectQuery = "SELECT * FROM PRICELIST;"; if (conn != null){ try{ Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = st.executeQuery(selectQuery); System.out.println("NAME\t\tPRICE"); while (rs.next()){ String name = rs.getString("NAME"); double val = rs.getDouble("PRICE"); System.out.println(name + "\t\t" + val); } st.close(); }catch(SQLException e){ e.printStackTrace(); } } }
Cc ki u ResultSet
TYPE_FORWARD_ONLY
Con tr c a ResultSet ki u ny ch c di chuy n theo m t h ng t u n cu i
TYPE_SCROLL_INSENSITIVE
Con tr c th di chuy n t i lui tng i v i v tr hi n t i c a n, v cng c th di chuy n n m t v tr c th , khng b nh h ng n u k t qu c thay i ni khc
TYPE_SCROLL_SENSITIVE
Con tr c th di chuy n t i lui tng i v i v tr hi n t i c a n, v cng c th di chuy n n m t v tr c th , s b nh h ng n u k t qu b thay i ni khc
Ch Ho t ng ng Th i C a ResultSet
CONCUR_READ_ONLY
Xc nh ch ho t ng ng th i, k t qu lu trong i t ng ResultSet khng c thay i
CONCUR_UPDATABLE
Xc nh ch ho t ng ng th i, k t qu lu trong i t ng ResultSet c thay i
Cc Phng Th c C a ResultSet
next(): di chuy n con tr n dng k , tr v true n u c dng k ti p, false n u n cu i ResultSet previous(): di chuy n con tr n dng tr c first(): di chuy n con tr n dng u tin last(): di chuy n con tr n dng cu i cng beforeFirst(): di chuy n con tr n v tr tr c dng u tin afterLast(): di chuy n con tr n sau dng cu i cng relative(int rows): di chuy n con tr t ng i v i v tr hi n t i c a n v i s dng l rows absolute(int row): di chuy n con tr n dng th row
L y D Li u T object
ResultSet
V d :
String name = rs.getString("NAME"); double val = rs.getDouble("PRICE");
Ch nh S a D ResultSet
Li u S
D ng
D li u c a m t b ng trong CSDL c th c ch nh s a b ng cch s d ng cu l nh SQL UPDATE Ta cng c th ch nh s a d li u trn cc hng c a m t b ng t ResultSet lm c i u ny, ResultSet ph i c t ch CONCUR_UPDATABLE S d ng cc phng th c updateXXX() ch nh s a d li u trn ResultSet
XXX l ki u d li u c a c t c n c s a
V D Ch nh S a D
Li u
public void updateTable(){ String selectQuery = "SELECT * FROM PRICELIST;"; if (conn != null){ try{ Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = st.executeQuery(selectQuery); rs.next(); rs.updateDouble("PRICE", 1.5); rs.updateRow(); st.close(); }catch(SQLException e){ e.printStackTrace(); } } }
S D ng L p PreparedStatement
C nh ng tr ng h p ta c n th c hi n nhi u cu l nh SQL c c u trc tng t nhau, ch c gi tr l thay i PreparedStatement c th c s d ng so n tr c cu l nh c s n c u trc c n thi t Gi tr s c a vo nh nh ng i s khi cu l nh c th c thi
V d : PreparedStatement
public void prepareStatement(){ String insertQuery = "INSERT INTO PRICELIST VALUES(?,?);"; if (conn != null){ try{ PreparedStatement prest = conn.prepareStatement(insertQuery); prest.setString(1, "Biscuit"); prest.setDouble(2, 1.2); prest.executeUpdate(); prest.setString(1, "Pen"); prest.setDouble(2, 0.5); prest.executeUpdate(); prest.close(); }catch(SQLException e){ e.printStackTrace(); } } }
V d : Transaction
public void transaction(){ String insertQuery = "INSERT INTO PRICELIST VALUES(?,?);"; if (conn != null){ try{ conn.setAutoCommit(false); PreparedStatement prest = conn.prepareStatement(insertQuery); prest.setString(1, "Biscuit"); prest.setDouble(2, 1.2); prest.executeUpdate(); prest.setString(1, "Pen"); prest.setDouble(2, 0.5); prest.executeUpdate(); prest.close(); conn.commit(); conn.setAutoCommit(true); }catch(SQLException e){ e.printStackTrace(); } } }
H y M t Giao D ch
Trong qu trnh th c hi n m t giao d ch, n u c sai st no x y ra, ta c th h y giao d ch ang c th c hi n n a ch ng b ng cch s d ng rollback conn.rollback() s hy d li u n th i i m n c commit g n nh t
Ty thu c vo h qu n tr CSDL m cch rollback c th khc nhau
V D : rollback
public void rollbackTransaction(){ String insertQuery = "INSERT INTO PRICELIST VALUES(?,?);"; if (conn != null){ try{ conn.setAutoCommit(false); PreparedStatement prest = conn.prepareStatement(insertQuery); prest.setString(1, "Biscuit"); prest.setDouble(2, 1.2); prest.executeUpdate(); conn.commit(); prest.setString(1, "Pen"); Thng tin v Pen s prest.setDouble(2, 0.5); prest.executeUpdate(); khng c lu vo prest.close(); CSDL conn.rollback(); conn.commit(); conn.setAutoCommit(true); }catch(SQLException e){ e.printStackTrace(); } } }
ResultSetMetaData
l y thng tin (Metadata) v m t ResultSet no y
V d : ResultSetMetaData
public void showMetadata(){ String selectQuery = "SELECT * FROM PRICELIST;"; if (conn != null){ try{ Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = st.executeQuery(selectQuery); ResultSetMetaData meta = rs.getMetaData(); int iColumnCount = meta.getColumnCount(); for (int i =1 ; i <= iColumnCount ; i++){ System.out.println("Column Name: " + meta.getColumnName(i)); System.out.println("Column Type: " + meta.getColumnTypeName(i)); System.out.println("Display Size: " + meta.getColumnDisplaySize(i) ); System.out.println("Precision: " + meta.getPrecision(i)); System.out.println("Scale: " + meta.getScale(i) ); } st.close(); }catch(SQLException e){ e.printStackTrace(); } } }
K t Thc