## JDBC 驅動載入 => 連接創建 => 創建編譯 / 預編譯語句 => 獲取結果集 => 遍歷結果集 => 返回結果集 | 介面 | | | | | | Driver | 驅動 | | Connection | 連接 | | Statement | 操作 | | ResultSet | 結果 ...
JDBC
驅動載入 => 連接創建 => 創建編譯 / 預編譯語句 => 獲取結果集 => 遍歷結果集 => 返回結果集
介面 | |
---|---|
Driver | 驅動 |
Connection | 連接 |
Statement | 操作 |
ResultSet | 結果集 |
-
具體的實現不需要由Java的公司自己提供
-
所有開發者使用JDBC的規範都被統一定義
連接資料庫(mysql)
統一資源定位符URL:jdbc:mysql:// + ip地址 + 埠 + 訪問的資料庫 + 請求參數(配置信息)
mysql驅動類的完整路徑:com.mysql.jdbc.Driver
public class DBUtil {
// 定義用戶名
private static final String USER_NAME = "root";
// 定義密碼
private static final String PASSWORD = "123456";
// URL--統一資源定位符
// jdbc:mysql:// + ip地址 + 埠 + 訪問的資料庫 + 請求參數(配置信息)
private static final String URL = "jdbc:mysql://localhost:3306/iWeb?characterEncoding=utf8";
// 載入驅動(靜態代碼塊)
static {
// mysql驅動類的完整路徑
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("驅動載入失敗!");
}
}
// 定義一個獲取資料庫連接的方法
public static Connection getConnection(){
try {
return DriverManager.getConnection(URL,USER_NAME,PASSWORD);
} catch (SQLException e) {
System.out.println("連接獲取失敗!");
return null;
}
}
}
// 連接測試
public static void main(String[] args) {
// 定義sql語句
String sql = "insert into student(name) values('robot03')";
// try-with 代碼塊,在try後額外提供一個括弧,括弧裡面的資源在try-catch結束後自動釋放
// 只有實現了AutoCloseable介面的類,才支持使用try-with自動關閉
// 獲取資料庫連接 + 生成statement編譯語句
try(Connection connection = DBUtil.getConnection();
Statement statement = connection.createStatement()) {
// 執行語句
statement.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
代碼封裝(DAO)& CRUD
DAO層:負責提供JDBC支持,提供對應的DAO介面和實現類
ORM(object relation model):對象應用模型,對資料庫對象的映射
// 測試表對應的JavaBean
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private long id;
private String name;
private String gender;
private String address;
private long phone;
}
增(insert)
// 增(insert)
public void insert(Student student) {
// 空值判斷
if(student == null || student.getName() == null || "".equals(student.getName())){
System.out.println("參數有誤!");
return;
}
String sql = "insert into student(NAME,GENDER,ADDRESS,PHONE) values(?,?,?,?)";
try(Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sql)){
// 添加參數
preparedStatement.setString(1,student.getName());
preparedStatement.setString(2,student.getGender());
preparedStatement.setString(3,student.getAddress());
preparedStatement.setInt(4,student.getPhone());
// 執行語句
preparedStatement.execute();
}catch(SQLException e){
e.printStackTrace();
}
}
刪(delete)
// 刪(delete)
public void delete(Student student) {
String sql = "delete from student where 1=1";
if(student == null){
System.out.println("參數有誤!");
return;
}
if(student.getId() > 0 && (student.getName() == null || "".equals(student.getName()))){
// 使用 PreparedStatement 進行參數傳遞,只需要將 sql 中的參數標記為 ? 即可
sql = sql +" and id = ?";
// 使用預編譯對象的時候,在創建對象時就需要傳入sql
try(Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sql)){
// 數字代表參數序號
preparedStatement.setInt(1, student.getId());
// 執行sql語句
preparedStatement.execute();
}catch(SQLException e){
e.printStackTrace();
}
} else if (student.getId() <= 0 && student.getName() != null && (!"".equals(student.getName()))) {
sql = sql +" and NAME = ?";
try(Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sql)){
// 數字代表參數序號
preparedStatement.setString(1, student.getName());
// 執行sql語句
preparedStatement.execute();
}catch(SQLException e){
e.printStackTrace();
}
}else {
System.out.println("參數有誤!");
}
}
改(update)
public void update(Student student) {
String sql = "update student set name = ? where id = ?";
// 空值判斷
if(student == null || student.getName() == null || "".equals(student.getName())){
System.out.println("參數有誤!");
return;
}
try(Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sql)){
// 傳參
preparedStatement.setString(1,student.getName());
preparedStatement.setInt(2,student.getId());
// 執行sql語句
preparedStatement.execute();
}catch(SQLException e){
e.printStackTrace();
}
}
查(select)
// 普通查詢
public Collection<Student> listByPage(int start, int count) {
List<Student> result = new ArrayList<>();
String sql = "select * from student limit ?,?";
try(Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sql)){
// 傳參
preparedStatement.setInt(1,start);
preparedStatement.setInt(2,count);
// 執行查詢語句的時候,需要使用 ResultSet 接收結果
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
result.add(new Student(resultSet.getInt("id"),resultSet.getString("name"), resultSet.getString("gender"),resultSet.getString("address"),resultSet.getInt("phone")));
}
}catch(SQLException e){
e.printStackTrace();
}
if(result.size() == 0){
return null;
}else{
return result;
}
}
// 模糊查詢(字元串拼接)
public Collection<Student> listWithNameLike(String key) {
List<Student> result = new ArrayList<>();
String sql = "select * from student where name like ?";
try(Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sql)){
// 傳參
preparedStatement.setString(1,"%" + key + "%");
// 接收結果
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
result.add(new Student(resultSet.getInt("id"),resultSet.getString("name"), resultSet.getString("gender"),resultSet.getString("address"),resultSet.getInt("phone")));
}
}catch(SQLException e){
e.printStackTrace();
}
if(result.size() == 0){
return null;
}else{
return result;
}
}