1. 程式人生 > >2020想學習JAVA的同學看過來,最基礎的程式設計CRUD你會了沒?

2020想學習JAVA的同學看過來,最基礎的程式設計CRUD你會了沒?

## 一 JDBC簡介 Java DataBase Connectivity Java語言連線資料庫 官方(Sun公司)定義的一套操作所有關係型資料庫的規則(介面) 各個資料庫廠商去實現這套介面 提供資料庫驅動JAR包 可以使用這套介面(JDBC)程式設計 真正執行的程式碼是驅動JAR包中的實現類 ## 二 JDBC初體驗 **1. 新建一個Maven專案** ```java pom.xml ``` **sql** ```java CREATE TABLE account ( aid INT PRIMARY KEY, aname VARCHAR(100), amoney DOUBLE ); ``` **2. 插入** ```java @Test public void test01() { Connection connection = null; PreparedStatement statement = null; try { // 註冊驅動 MySQL5之後的驅動JAR包可以省略該步驟 Class.forName("com.mysql.cj.jdbc.Driver"); // 獲取資料庫連線物件 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo_hy", "root", "root"); // 開啟事務 connection.setAutoCommit(false); // 定義SQL String sql = "insert into account values(?, ?, ?)"; // 獲取執行SQL的物件 PreparedStatement statement = connection.prepareStatement(sql); // 設定引數 statement.setInt(1, 1); //'?' 位置的編號 從1開始 statement.setString(2, "No1"); //'?' 位置的編號 從1開始 statement.setDouble(3, 2000); //'?' 位置的編號 從1開始 // 執行SQL 返回受影響的行數 int count = statement.executeUpdate(); // 提交事務 connection.commit(); // 處理結果 System.out.println("count = " + count); } catch (Exception e) { e.printStackTrace(); // 回滾事務 if (null != connection) { try { connection.rollback(); } catch (SQLException exception) { exception.printStackTrace(); } } } finally { // 釋放資源 if (null != statement) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (null != connection) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ``` **3. 刪除** ```java @Test public void test02() { Connection connection = null; PreparedStatement statement = null; try { // 註冊驅動 MySQL5之後的驅動JAR包可以省略該步驟 //Class.forName("com.mysql.cj.jdbc.Driver"); // 獲取資料庫連線物件 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo_hy", "root", "root"); // 開啟事務 connection.setAutoCommit(false); // 定義SQL String sql = "delete from account where aid = ?"; // 獲取執行SQL的物件 PreparedStatement statement = connection.prepareStatement(sql); // 設定引數 statement.setInt(1, 1); //'?' 位置的編號 從1開始 // 執行SQL 返回受影響的行數 int count = statement.executeUpdate(); // 提交事務 connection.commit(); // 處理結果 System.out.println("count = " + count); } catch (Exception e) { e.printStackTrace(); // 回滾事務 if (null != connection) { try { connection.rollback(); } catch (SQLException exception) { exception.printStackTrace(); } } } finally { // 釋放資源 if (null != statement) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (null != connection) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ``` **4. 修改** ```java @Test public void test03() { Connection connection = null; PreparedStatement statement1 = null; PreparedStatement statement2 = null; try { // 註冊驅動 MySQL5之後的驅動JAR包可以省略該步驟 Class.forName("com.mysql.cj.jdbc.Driver"); // 獲取資料庫連線物件 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo_hy", "root", "root"); // 開啟事務 connection.setAutoCommit(false); // 定義SQL String sql1 = "update account set amoney = amoney + ? where aid = ?"; String sql2 = "update account set amoney = amoney - ? where aid = ?"; // 獲取執行SQL的物件 PreparedStatement statement1 = connection.prepareStatement(sql1); statement2 = connection.prepareStatement(sql2); // 設定引數 statement1.setDouble(1, 500); //'?' 位置的編號 從1開始 statement1.setInt(2, 1); //'?' 位置的編號 從1開始 statement2.setDouble(1, 500); //'?' 位置的編號 從1開始 statement2.setInt(2, 2); //'?' 位置的編號 從1開始 // 執行SQL 返回受影響的行數 statement1.executeUpdate(); int i = 3 / 0; //模擬異常 statement2.executeUpdate(); // 提交事務 connection.commit(); } catch (Exception e) { e.printStackTrace(); // 回滾事務 if (null != connection) { try { connection.rollback(); } catch (SQLException exception) { exception.printStackTrace(); } } } finally { // 釋放資源 if (null != statement2) { try { statement2.close(); } catch (SQLException e) { e.printStackTrace(); } } if (null != statement1) { try { statement1.close(); } catch (SQLException e) { e.printStackTrace(); } } if (null != connection) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ``` **5. 查詢** ```java @Test public void test04() { Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; try { // 註冊驅動 MySQL5之後的驅動JAR包可以省略該步驟 Class.forName("com.mysql.cj.jdbc.Driver"); // 獲取資料庫連線物件 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo_hy", "root", "root"); // 開啟事務 connection.setAutoCommit(false); // 定義SQL String sql = "select * from account"; // 獲取執行SQL的物件 PreparedStatement statement = connection.prepareStatement(sql); // 執行SQL 返回結果集 resultSet = statement.executeQuery(); // 提交事務 connection.commit(); // 處理結果 while (resultSet.next()) { int id = resultSet.getInt(1); //代表列的編號 從1開始 String name = resultSet.getString("aname"); //代表列的名稱 double money = resultSet.getDouble(3); //代表列的編號 從1開始 System.out.println(id + "---" + name + "---" + money); } } catch (Exception e) { e.printStackTrace(); // 回滾事務 if (null != connection) { try { connection.rollback(); } catch (SQLException exception) { exception.printStackTrace(); } } } finally { // 釋放資源 if (null != resultSet) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if (null != statement) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (null != connection) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ``` ## 三 資料庫連線池 一個存放資料庫連線的容器 當系統初始化後 容器被建立 容器中會申請一些連線物件 當用戶訪問資料庫時 從容器中獲取連線物件 使用者訪問完之後 會將連線物件歸還給容器 這樣可以節約資源 提高訪問效率 常見的資料庫連線池有 Druid C3P0... **Druid初體驗** druid.properties ```java url=jdbc:mysql://localhost:3306/demo_hy driverClassName=com.mysql.cj.jdbc.Driver username=root password=root maxActive=10 minIdle=5 ``` **XTest.java** ```java @Test public void test05() { InputStream stream = null; Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; try { // 載入配置檔案 Properties properties = new Properties(); stream = XTest.class.getClassLoader().getResourceAsStream("druid.properties"); properties.load(stream); // 獲取連線池物件 DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); // 獲取資料庫連線物件 Connection connection = dataSource.getConnection(); // 開啟事務 connection.setAutoCommit(false); // 定義SQL String sql = "select * from account"; // 獲取執行SQL的物件 PreparedStatement statement = connection.prepareStatement(sql); // 執行SQL 返回結果集 resultSet = statement.executeQuery(); // 提交事務 connection.commit(); // 處理結果 while (resultSet.next()) { int id = resultSet.getInt(1); //代表列的編號 從1開始 String name = resultSet.getString("aname"); //代表列的名稱 double money = resultSet.getDouble(3); //代表列的編號 從1開始 System.out.println(id + "---" + name + "---" + money); } } catch (Exception e) { e.printStackTrace(); // 回滾事務 if (null != connection) { try { connection.rollback(); } catch (SQLException exception) { exception.printStackTrace(); } } } finally { // 釋放資源 if (null != resultSet) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if (null != statement) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (null != connection) { try { connection.close(); //歸還連線 } catch (SQLException e) { e.printStackTrace(); } } if (null != stream) { try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } } } ``` ## 最後 > 學習java不易,需要持續的堅持,如果有想學習java的基礎知識或者進階java的可以私信“學習”獲取學習聯絡方式 ![file](https://img2020.cnblogs.com/other/1973721/202007/1973721-20200726202255305-1886317