1/29/2012 N i dung B môn Công ngh Ph n m m Vi n CNTT & TT Tr ng i h c Bách Khoa Hà N i Chuyên đ L p trình JSP, Servlet Bài 02: C b n v JDBC JDBC gì? Cức b c s d ng API c a JDBC DataSource & k thu t Connection Pooling Transaction Prepared & Callable Statements Gi ng viên: ThS Tr nh Tu n t B môn CNPM Email: trinhtuandat.bk@gmail.com/dattt@soict.hut.edu.vn JDBC gì? JDBC gì? Là cức API Java chu n t c đ truy c p CSDL quan h ng d ng không c n quan tâm t i chi ti t c th c a CSDL N m Java SE (J2SE) Java SE có phiên b n JDBC 4 JDBC API nh ngh a m t t p cức Java Interfaces, đ c cài đ t b i cức vendor khức nhau, thành cức JDBC Drivers Là cài đ t c th c a cức JDBC interfaces Cức ng d ng s d ng t p cức giao di n đ th c hi n cức thao tức v i CSDL Tính portability Ph n l n API c a JDBC n m gói java.sql JDBC Driver T t c cức database server đ u có JDBC driver(s) t ng ng Có th xem danh sứch cức drivers có http://industry.java.sun.com/products/jdbc/drivers DriverManager, Connection, ResultSet, DatabaseMetaData, ResultSetMetaData, PreparedStatement, CallableStatement Types M t s ch c n ng nâng cao khức n m gói javax.sql package DataSource 1/29/2012 Database URL c s d ng đ t o m t k t n i t i database Cức b Cú pháp: Có th ch a server, port, protocol, etc jdbc:subprotocol_name:driver_dependant_databasename c s d ng JDBC API Ví d : Oracle thin driver Derby Pointbase jdbc:oracle:thin:@machinename:1521:dbname jdbc:derby://localhost:1527/sample jdbc:pointbase:server://localhost/sample Cức b c s d ng JDBC B1 Load JDBC driver cho t ng lo i CSDL B1 Load JDBC driver cho t ng lo i CSDL B2 L y đ i t ng Connection B3 L y đ i t ng Statement B4 Th c hi n câu truy v n, câu l nh update B5 c k t qu tr v B6 c cức Meta-data (tùy ch n) B7 óng đ i t ng Statement đ i t ng Connection load v driver cho CSDL đ ng ký v i DriverManager, c n load class t ng ng Class.forName() try { // This loads an instance of the Pointbase DB Driver // The driver has to be in the classpath Class.forName("org.apache.derby.jdbc.ClientDriver"); }catch (ClassNotFoundException cnfe){ System.out.println("" + cnfe); } B2 L y đ i t ng Connection DriverManager & Connection L p DriverManager ch u trứch nhi m t o k t n i t i CSDL 10 java.sql.DriverManager S d ng DataSource cứch hay dùng h n mu n l y m t đ i t ng connection (trình bày ph n sau) Ví d t o k t n i t i CSDL nh sau: try { Connection connection = DriverManager getConnection("jdbc:derby://localhost:1527/sample", “app"," app "); java.sql.Connection } catch(SQLException sqle) { System.out.println("" + sqle); } 11 getConnection(String url, String user, String password) throws SQLException Statement createStatement() throws SQLException void close() throws SQLException void setAutoCommit(boolean b) throws SQLException void commit() throws SQLException void rollback() throws SQLException 12 1/29/2012 B3 L y đ i t T om tđ it Connection ng T đ i t ng Statement, l nh đ d ng nhi u nh t cs (a) QUERY (SELECT) ResultSet executeQuery(string sql) int executeUpdate(String sql) ResultSet rs = statement.executeQuery("select * from customer_tbl"); Ví d : ng Statement t đ i t B4 Th c thi cức câu truy v n/cức l nh java.sql.Statement ng Statement Statement statement = connection.createStatement(); (b) ACTION COMMAND (UPDATE/DELETE) Cùng đ i t ng Statement có th đ c dùng cho nhi u queries không liên quan t i int iReturnValue = statement.executeUpdate("update manufacture_tbl set name = ‘IBM' where mfr_num = 19985678"); 14 13 B5 X lý k t qu nh n v Duy t ResultSet đ x lý thông tin boolean next() xxx getXxx(int columnNumber) xxx getXxx(String columnName) void close() u tiên, tr l p n m tiên java.sql.ResultSet B5 X lý k t qu nh n v (2) tr Khi có ResultSet, LTV d dàng x lý d li u L u ý: Ch s c a ResultSet b t đ u t while (rs.next()){ // Wrong this will generate an error String value0 = rs.getString(0); c hàng đ u // Correct! String value1 = rs.getString(1); int value2 = rs.getInt(2); LTV c n g i ph ng th c next() đ chuy n tr đ n hàng đ u tiên int value3 = rs.getInt(“ADDR_LN1"); } 15 16 B6 c metadata c a ResultSet metadata c a CSDL (tùy ch n) B5 X lý k t qu nh n v (3) Mu n l y d li u t ResultSet, s d ng ph ng th c getXXX() cho phù h p getString() getInt() getDouble() getObject() M i ki u d li u java.sql.Types, đ u có ph ng th c getXXX t ng ng Khi có đ i t ng ResultSet ho c Connection, LTV có th l y v metadata c a CSDL ho c c a câu truy v n em l i thông tin h u ích v d li u l y v , ho c v CSDL s d ng ResultSetMetaData rsMeta = rs.getMetaData(); DatabaseMetaData dbmetadata = connection.getMetaData(); 17 Có kho ng 150 ph ng th c l p DatabaseMetaData 18 1/29/2012 Ví d v ResultSetMetaData ResultSetMetaData meta = rs.getMetaData(); DataSource & k thu t Connection Pooling //Return the column count int iColumnCount = meta.getColumnCount(); for (int i =1 ; i ... Pointbase jdbc: oracle:thin:@machinename:1 521 :dbname jdbc: derby://localhost:1 527 /sample jdbc: pointbase:server://localhost/sample Cức b c s d ng JDBC B1 Load JDBC driver cho... Application Server (và GlassFish V2), m t data source đ c g i m t JDBC resource 24 1 /29 /20 12 Cức thu c tính c a m t DataSource đ đ nh ngh a đâu? DataSource (JDBC Resource) file domain.xml c... DriverManager.getConnection( "jdbc: oracle:thin:@machinename:1 521 :db name","username","password"); connection.setAutoCommit(false); { { 123 , 500} , { 124 , 25 0}, { 125 , 10}, { 126 , 350} }; int iRecordsUpdate