Professional Documents
Culture Documents
Ni dung
Gii thiu JDBC
JDBC
JDBC (Java database connectivity) cung cp cho java developer tp cc interface chun dng truy xut d liu quan h. JDBC c pht trin bi JavaSoft
Java developer khng cn phi quan tm n s khc nhau khi giao tip vi cc HQTCSDL khc nhau.
JDBC Drivers
Database
Java Application
SQLServer Driver
MySQL Driver
Oracle Driver
DB2 Driver
Access Driver
dBase Driver
Access
dBase
SQL Server
MySQL
Oracle
DB2
JDBC
Java Application 1. M kt ni DBMS 1. To phin kt ni
2. Gi SQL
3. Rt trch d liu 4. ng kt ni
JDBC API
JDBC Interfaces
Class v Interface ca JDBC API thuc gi java.sql
Statement: Gn kt vi mt connection n c s d liu v cho php thc thi cc cu lnh SQL. CallableStatement tng t Statement nhng p dng cho Store procedures.
10
JDBC API
PreparedStatement: Tng t nh Statement nhng p dng cho Precompiled SQL. ResultSet: Cung cp thng tin rt trch t c s d liu , cho php truy xut cc dng d liu.
ResultSetMetaData: Cung cp cc thng tin nh kiu d liu v cc thuc tnh trong Resultset.
DatabaseMetaData: Cung cp cc thng tin ca c s d liu kt ni. SQLException: Cung cp thng tin cc ngoi l xy ra khi tng tc vi c s d liu.
11
JDBC API
DriverManager
Connection
Connection
Connection
Statement
Statement
ResultSet
ResultSet
12
CC BC S DNG JDBC
13
Cc bc c bn s dng JDBC
ng k Driver
M kt ni n c s d liu
To v thc cc cu lnh SQL X l cc ngoi l ng kt ni c s d liu
14
ng k Driver
Bc 1: Thm th vin JDBC Driver vo ng dng
Bc 2: ng k:
Class.forName : Class.forName(org.gjt.mm.mysql.Driver); DriverManager.registerDriver Driver = new org.gjt.mm.mysql.Driver() DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
15
M kt ni c s d liu
i tng Connection c to t getConnection ca DriverManager
Connection getConnection (String url); Connection getConnection (String url, String user, String password); Connection getConnection (String url, java.util.Properties info);
16
M kt ni c s d liu
Driver driver = new org.gjt.mm.mysql.Driver()
DriverManager.registerDriver(driver);
String conString = "jdbc:mysql://localhost:3306/QuanLyNhanVien"; Properties info= new Properties(); info.setProperty("characterEncoding", "utf8"); info.setProperty("user", "root"); info.setProperty(password", "root"); Connection connection = DriverManager.getConnection(conString, info);
17
Cc cu lnh SQL
INSERT : Thm d liu vo bng
18
19
String sql = INSERT INTO PHONGBAN (MAPHONGBAN, TENPHONGBAN) VALUES (PBHC, Hnh Chnh);
int n = statement.executeUpdate (sql); if (n == 1){ System.out.println(Thm phng ban thnh cng); }else{ System.out.println(Thm phng ban tht bi); }
20
String sql = UPDATE PHONGBAN SET TENPHONGBAN = K Thut WHERE MAPHONGBAN = PBHC;
int n = statement.executeUpdate (sql); if (n == 1){ System.out.println(Cp nht thnh cng); }else{ System.out.println(Cp nht tht bi); }
21
Xa d liu t bng
Statement statement = connection.createStatement();
22
Ly d liu t bng
Statement statement = connection.createStatement();
23
Ly d liu t bng
rs.next()
Row
Result Set
24
ng kt ni c s d liu
connection.close()
25
X l ngoi l
java.sql.SQLException k tha t java.lang.Exception
26
X l ngoi l
try{ - ng k driver, M kt ni c s d liu - To v thc thi cc cu lnh SQL, ng kt ni }catch (SQLException ex){ while (ex != null) { System.out.println(SQLState: + ex.getSQLState()); System.out.println(Message: + ex.getMessage()); System.out.println(ErrorCode: + ex.getErrorCode()); System.out.println(); ex.getNextException(); } }catch(java.lang.Exception ex) { ex.printStackTrace(); }
27
MT S K THUT KHC
28
29
30
Statement.executeUpdate();
31
PreparedStatement
statement.setInt (int parameterIndex, int x)
32
33
34
35
Lu : parameterIndex : Bt u t 1 parameterName
36
: Tn tham s
MySQLDataAccessHelper.java
public class MySqlDataAccessHelper { private Connection connection; public void open() { try { Driver driver = new org.gjt.mm.mysql.Driver(); DriverManager.registerDriver(); String conString = jdbc:mysql://localhost:3306/QuanLyNhanVien"; Properties pros = new Properties(); info.setProperty("characterEncoding", "utf8"); info.setProperty("user", "root"); this.connection = DriverManager.getConnection(conString, info); } catch (Exception ex) { System.out.println(ex.getMessage()); } }
37
MySQLDataAccessHelper.java
public class MySqlDataAccessHelper { - - - - - - - - - - - - - - - - - /** * ng kt ni c s d liu */ public void close() { try { this.connection.close(); } catch (Exception ex) { System.out.println(ex.getMessage()); } } - - - - - - - - - - - - - - - - -
38
MySQLDataAccessHelper
public class MySqlDataAccessHelper { - - - - - - - - - - - - - - - - - /** * Rt trch d liu */ public ResultSet executeQuery(String sql) { ResultSet rs = null; try { Statement sm = this.connection.createStatement(); rs = sm.executeQuery(sql); } catch (Exception ex) { System.out.println(ex.getMessage()); } return rs; }
39
MySQLDataAccessHelper
public class MySqlDataAccessHelper { - - - - - - - - - - - - - - - - - /** * Thm, xa, cp nht d liu */ public int executeUpdate(String sql) { int num = -1; try { Statement sm = this.connection.createStatement(); num = sm.executeUpdate(sql); } catch (Exception ex) { System.out.println(ex.getMessage()); } return num; }
40
41
42
HI V P
43