JDBC
Java連接步驟
// 1. 加載驅(qū)動
Class.forName("com.mysql.cj.jdbc.Driver");
// 2. 用戶信息和數(shù)據(jù)庫地址
// useUnicode=true&characterEncoding=utf8&useSSL=true
String url = "jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC";
String username = "root";
String password = "123456";
// 3. 獲取數(shù)據(jù)庫對象
Connection connection = DriverManager.getConnection(url,username,password);
// 4. 執(zhí)行SQL的對象
Statement statement = connection.createStatement();
// 5. 獲得結(jié)果
String sql = "SELECT * FROM account";
ResultSet resultSet = statement.executeQuery(
sql
);
while (resultSet.next()) {
System.out.println("id:" + resultSet.getObject("id"));
System.out.println("name:" + resultSet.getObject("name"));
System.out.println("cash:" + resultSet.getObject("cash"));
}
System.out.println(resultSet);
// 6. 釋放連接
resultSet.close();
statement.close();
connection.close();
-
加載驅(qū)動
加載驅(qū)動方法:
Class.forName("com.mysql.cj.jdbc.Driver");
//Dirver類里面只有一個靜態(tài)代碼塊眯娱,在加載的時候自動通過DriverManager注冊了驅(qū)動 public class Driver extends NonRegisteringDriver implements java.sql.Driver { public Driver() throws SQLException { } static { try { DriverManager.registerDriver(new Driver()); } catch (SQLException var1) { throw new RuntimeException("Can't register driver!"); } } }
-
用戶信息和數(shù)據(jù)庫地址
連接URL
MySQL jdbc:mysql://(數(shù)據(jù)庫地址):3306/(數(shù)據(jù)庫)參數(shù)=值&參數(shù)=值&...
Oracle jdbc:oracle:thin@(數(shù)據(jù)庫地址):1521/(數(shù)據(jù)表)
常用URL
useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC
-
獲取數(shù)據(jù)庫對象
Connection connection = DriverManager.getConnection(url,username,password);
獲得連接對象,
Connection
代表數(shù)據(jù)庫connection.setAutoCommit(); // 設(shè)置自動提交 connection.commit(); // 提交 connection.rollback(); // 回滾
-
執(zhí)行SQL的對象
Statement
執(zhí)行SQL的對象Statement statement = connection.createStatement();
PrepareStatement
執(zhí)行SQL的對象PrepareStatement prepareStatement = connection.prepareStatement()
// Statement的用法 statement.executeQuery(); // 查詢操作绕沈,返回ResultSet statement.execute(); // 執(zhí)行任何SQL贱除,返回執(zhí)行的結(jié)果boolean statement.executeUpdate(); // 更新插入刪除雾鬼,返回一個受影響的行數(shù) statement.executeBatch() // 執(zhí)行一系列的SQL // 使用executeQuery操作增刪改操作報錯:Can not issue data manipulation statements with executeQuery().
PrepareStatement的用法(效率更高,可以防止SQL注入,轉(zhuǎn)義字符直接被轉(zhuǎn)義) String sql = "INSERT INTO ACCOUNT(`name`,`cash`) VALUES(?,?)"; // 使用問號占位符代替參數(shù) PrepareStatement prepareStatement = connection.prepareStatement(sql) // 預(yù)編譯SQL preparedStatement = conn.prepareStatement(sql); preparedStatement.setString(1,"yhq"); preparedStatement.setBigDecimal(2,new BigDecimal(1000.00)); preparedStatement.executeUpdate(); // 無需參數(shù)百炬,其它查詢同Statement
-
獲得結(jié)果
ResultSet類 // 是查詢獲得的結(jié)果集合 resultSet.getObject() // 在不知道類型的情況下使用 resultSet.afterLast() // 移動到最后面 resultSet.beforeFirst() // 移動到最前面 resultSet.next() // 移動到下一個 resultSet.previous() // 移動到上一個 resultSet.absolute(row) // 移動到指定行
-
釋放連接
resultSet.close(); statement.close(); connection.close();
-
操作事務(wù)
connection.setAutoCommit(false); //關(guān)閉自動提交蚕脏,自動開啟事務(wù) connection.commit(); // 默認(rèn)不成功回滾侦副,也可以在catch里面顯式定義
try {
conn = JdbcUtils.getConnection();
conn.setAutoCommit(false); //關(guān)閉自動提交,自動開啟事務(wù)
// 數(shù)據(jù)庫操作邏輯代碼
conn.commit();
} catch (Exception e) {
// 默認(rèn)回滾
e.printStackTrace();
try { //顯式定義回滾
conn.rollback();
} catch (Exception e2) {
e2.printStackTrace();
}
} finally {
釋放資源;
}
```
-
數(shù)據(jù)庫連接池
數(shù)據(jù)庫連接池負(fù)責(zé)分配驼鞭、管理和釋放數(shù)據(jù)庫連接秦驯,它允許應(yīng)用程序重復(fù)使用一個現(xiàn)有的數(shù)據(jù)庫連接,而不是再重新建立一個挣棕。
連接池實(shí)現(xiàn)了
DataSource
接口開源數(shù)據(jù)源實(shí)現(xiàn):
DBCP
译隘、C3P0
亲桥、Druid
使用連接池之后,開發(fā)中就不需要寫連接服務(wù)代碼了