MySQL是一種流行的關係型資料庫,廣泛應用於各種應用程序中。在Java應用程序中,MySQL也是常用的資料庫之一。本教程將詳細介紹如何使用Java連接MySQL資料庫,提供多個方面的闡述,包括MySQL資料庫的連接、CRUD操作、事務處理等內容。
一、MySQL資料庫的連接
在Java應用程序中連接MySQL資料庫,需要先獲取資料庫連接。獲取資料庫連接的方式有多種,其中使用MySQL官方提供的JDBC驅動程序是較為常用的方式。以下是獲取MySQL資料庫連接的示例代碼:
try { // 載入MySQL JDBC驅動程序 Class.forName("com.mysql.cj.jdbc.Driver"); // 創建連接 String url = "jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC"; String user = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }
以上代碼中,首先使用Class.forName()方法載入MySQL JDBC驅動程序。接著創建連接時,需要指定MySQL伺服器地址、埠號、資料庫名、用戶名和密碼。在創建連接時可以通過修改URL的參數來控制連接的一些屬性,如是否使用SSL、時區等。
二、CRUD操作
1. 查詢操作
查詢操作是資料庫中最常用的操作之一。以下是查詢操作的示例代碼:
try { String sql = "SELECT * FROM users"; PreparedStatement statement = connection.prepareStatement(sql); ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("name"); int age = resultSet.getInt("age"); System.out.println("id:" + id + ", name:" + name + ", age:" + age); } resultSet.close(); statement.close(); } catch (SQLException e) { e.printStackTrace(); }
以上代碼中,使用PreparedStatement對象創建查詢SQL語句,並調用executeQuery()方法執行查詢操作。查詢結果保存在ResultSet對象中,通過調用ResultSet中的next()方法以逐行解析結果集。
2. 插入操作
插入操作是往資料庫中添加數據的操作。以下是插入操作的示例代碼:
try { String sql = "INSERT INTO users (name, age) VALUES (?, ?)"; PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, "Tom"); statement.setInt(2, 20); int count = statement.executeUpdate(); if (count >= 1) { System.out.println("Insert successfully!"); } statement.close(); } catch (SQLException e) { e.printStackTrace(); }
以上代碼中,使用PreparedStatement對象創建插入SQL語句,並調用executeUpdate()方法執行插入操作。插入操作執行成功後,會返回受影響的行數。
3. 更新操作
更新操作是修改資料庫中已有數據的操作。以下是更新操作的示例代碼:
try { String sql = "UPDATE users SET age = ? WHERE id = ?"; PreparedStatement statement = connection.prepareStatement(sql); statement.setInt(1, 25); statement.setInt(2, 1); int count = statement.executeUpdate(); if (count >= 1) { System.out.println("Update successfully!"); } statement.close(); } catch (SQLException e) { e.printStackTrace(); }
以上代碼中,使用PreparedStatement對象創建更新SQL語句,並調用executeUpdate()方法執行更新操作。更新操作執行成功後,同樣會返回受影響的行數。
4. 刪除操作
刪除操作是從資料庫中刪除數據的操作。以下是刪除操作的示例代碼:
try { String sql = "DELETE FROM users WHERE id = ?"; PreparedStatement statement = connection.prepareStatement(sql); statement.setInt(1, 1); int count = statement.executeUpdate(); if (count >= 1) { System.out.println("Delete successfully!"); } statement.close(); } catch (SQLException e) { e.printStackTrace(); }
以上代碼中,使用PreparedStatement對象創建刪除SQL語句,並調用executeUpdate()方法執行刪除操作。刪除操作執行成功後,同樣會返回受影響的行數。
三、事務處理
MySQL資料庫支持事務處理,可以保證多個操作作為一個整體完成或者失敗。Java程序中可以通過在Connection對象上設置事務隔離級別和提交或回滾事務來實現事務處理。以下是事務處理的示例代碼:
try { connection.setAutoCommit(false); String sql1 = "UPDATE users SET age = ? WHERE id = ?"; PreparedStatement statement1 = connection.prepareStatement(sql1); statement1.setInt(1, 20); statement1.setInt(2, 1); statement1.executeUpdate(); String sql2 = "UPDATE users SET age = ? WHERE id = ?"; PreparedStatement statement2 = connection.prepareStatement(sql2); statement2.setInt(1, 30); statement2.setInt(2, 2); statement2.executeUpdate(); connection.commit(); connection.setAutoCommit(true); System.out.println("Transaction successfully!"); } catch (SQLException e) { connection.rollback(); connection.setAutoCommit(true); e.printStackTrace(); }
以上代碼中,通過調用Connection對象的setAutoCommit(false)方法將自動提交事務的功能關閉。然後可以執行多個SQL語句,這些SQL語句將被作為一個事務提交到資料庫中。如果事務執行失敗,可以通過調用Connection對象的rollback()方法回滾事務。如果執行成功,最後再調用Connection對象的commit()方法提交事務。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/155009.html