最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java自定義JDBC實(shí)現(xiàn)連接池

 更新時(shí)間:2024年02月22日 09:36:23   作者:Mr-Apple  
本文主要介紹了java自定義JDBC實(shí)現(xiàn)連接池,包含實(shí)現(xiàn)JDBC連接池以及SQLException?異常的處理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

簡(jiǎn)單上手

使用 JDBC 來(lái)執(zhí)行 SQL 查詢和更新操作

import java.sql.*;

public class Main {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/your_database_name";
        String username = "your_username";
        String password = "your_password";

        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, username, password);
            System.out.println("成功連接到數(shù)據(jù)庫(kù)");

            // 查詢操作
            String query = "SELECT * FROM your_table_name";
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(query);

            // 打印查詢結(jié)果
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                System.out.println("ID: " + id + ", Name: " + name);
            }

            // 更新操作
            String update = "UPDATE your_table_name SET name = 'New Name' WHERE id = 1";
            int rowsAffected = statement.executeUpdate(update);
            System.out.println("更新操作影響的行數(shù): " + rowsAffected);

        } catch (SQLException e) {
            System.out.println("數(shù)據(jù)庫(kù)操作失敗:" + e.getMessage());
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                    System.out.println("成功關(guān)閉數(shù)據(jù)庫(kù)連接");
                } catch (SQLException e) {
                    System.out.println("關(guān)閉數(shù)據(jù)庫(kù)連接失?。? + e.getMessage());
                }
            }
        }
    }
}

下面展示一些 內(nèi)聯(lián)代碼片。

// A code blockvar foo = 'bar';

// An highlighted blockvar foo = 'bar';

實(shí)現(xiàn)JDBC連接池

JDBC 數(shù)據(jù)庫(kù)連接池的示例代碼:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Vector;

public class ConnectionPool {
    private static final int MAX_CONNECTIONS = 10; // 最大連接數(shù)限制
    private static final long CONNECTION_TIMEOUT = 5000; // 連接超時(shí)時(shí)間,單位毫秒

    private Vector<Connection> availableConnections = new Vector<>();
    private Vector<Connection> usedConnections = new Vector<>();

    public ConnectionPool(String url, String username, String password) {
        initializeConnectionPool(url, username, password);
    }

    private void initializeConnectionPool(String url, String username, String password) {
        while (!checkIfConnectionPoolIsFull()) {
            availableConnections.add(createNewConnection(url, username, password));
        }
        System.out.println("數(shù)據(jù)庫(kù)連接池初始化完成,當(dāng)前連接數(shù): " + availableConnections.size());
    }

    private synchronized boolean checkIfConnectionPoolIsFull() {
        return (availableConnections.size() + usedConnections.size()) >= MAX_CONNECTIONS;
    }

    private Connection createNewConnection(String url, String username, String password) {
        try {
            return DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public synchronized Connection getConnection() throws SQLException {
        long startTime = System.currentTimeMillis();
        while (availableConnections.isEmpty()) {
            try {
                wait(CONNECTION_TIMEOUT);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if ((System.currentTimeMillis() - startTime) >= CONNECTION_TIMEOUT) {
                throw new SQLException("獲取數(shù)據(jù)庫(kù)連接超時(shí)");
            }
        }

        Connection connection = availableConnections.remove(availableConnections.size() - 1);
        usedConnections.add(connection);
        System.out.println("獲取數(shù)據(jù)庫(kù)連接,當(dāng)前連接數(shù): " + availableConnections.size());
        return connection;
    }

    public synchronized void releaseConnection(Connection connection) {
        if (connection != null) {
            usedConnections.remove(connection);
            availableConnections.add(connection);
            notifyAll();
            System.out.println("釋放數(shù)據(jù)庫(kù)連接,當(dāng)前連接數(shù): " + availableConnections.size());
        }
    }
}

在上面的示例代碼中,我們?cè)黾恿俗畲筮B接數(shù)限制和連接超時(shí)處理機(jī)制。當(dāng)連接池中的連接數(shù)達(dá)到最大值時(shí),新請(qǐng)求會(huì)等待一段時(shí)間,如果超過(guò)連接超時(shí)時(shí)間仍未獲取到連接,則會(huì)拋出 SQLException 異常表示獲取連接超時(shí)。同時(shí),我們還通過(guò)控制臺(tái)輸出連接池的狀態(tài)信息,方便跟蹤連接的獲取和釋放情況。

測(cè)試

測(cè)試數(shù)據(jù)庫(kù)連接池的最大連接數(shù)限制和連接超時(shí)處理機(jī)制:

public class ConnectionPoolTest {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/your_database_name";
        String username = "your_username";
        String password = "your_password";

        ConnectionPool connectionPool = new ConnectionPool(url, username, password);

        // 模擬多個(gè)線程同時(shí)請(qǐng)求數(shù)據(jù)庫(kù)連接
        for (int i = 0; i < 15; i++) {
            Thread thread = new Thread(new Worker(connectionPool, i));
            thread.start();
        }
    }

    static class Worker implements Runnable {
        private ConnectionPool connectionPool;
        private int workerId;

        public Worker(ConnectionPool connectionPool, int workerId) {
            this.connectionPool = connectionPool;
            this.workerId = workerId;
        }

        @Override
        public void run() {
            try (Connection connection = connectionPool.getConnection()) {
                System.out.println("Worker " + workerId + " 獲取到數(shù)據(jù)庫(kù)連接");
                // 模擬操作持有連接的時(shí)間
                Thread.sleep(3000);
                System.out.println("Worker " + workerId + " 完成操作,釋放數(shù)據(jù)庫(kù)連接");
            } catch (SQLException | InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

關(guān)于SQLException

SQLException 是 Java 中的一個(gè)異常類,用于表示與數(shù)據(jù)庫(kù)相關(guān)的異常。當(dāng)使用 JDBC 連接數(shù)據(jù)庫(kù)時(shí),很多操作都可能會(huì)拋出 SQLException 異常,比如連接數(shù)據(jù)庫(kù)、執(zhí)行查詢、更新數(shù)據(jù)等過(guò)程中出現(xiàn)的錯(cuò)誤。

SQLException 是 java.sql.SQLException 類的全名,它是 java.sql 包中的一個(gè)類,專門用于處理數(shù)據(jù)庫(kù)操作可能出現(xiàn)的異常情況。該異常類提供了一系列方法,用于獲取關(guān)于異常的詳細(xì)信息,比如異常消息、SQL 狀態(tài)碼、引起異常的原因等,方便開發(fā)人員進(jìn)行異常處理和調(diào)試。

在使用 JDBC 連接數(shù)據(jù)庫(kù)時(shí),通常會(huì)在代碼中捕獲 SQLException 異常,并根據(jù)具體情況進(jìn)行異常處理,比如輸出異常信息、回滾事務(wù)、關(guān)閉連接等操作,以確保程序的穩(wěn)定性和可靠性。

下面是一個(gè)簡(jiǎn)單的示例,演示了如何捕獲并處理 SQLException 異常:

import java.sql.*;

public class Main {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/your_database_name";
        String username = "your_username";
        String password = "your_password";

        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, username, password);
            // 執(zhí)行數(shù)據(jù)庫(kù)操作...
        } catch (SQLException e) {
            System.out.println("數(shù)據(jù)庫(kù)操作失?。? + e.getMessage());
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    System.out.println("關(guān)閉數(shù)據(jù)庫(kù)連接失敗:" + e.getMessage());
                    e.printStackTrace();
                }
            }
        }
    }
}

在上面的示例中,我們?cè)谶B接數(shù)據(jù)庫(kù)和關(guān)閉數(shù)據(jù)庫(kù)連接的過(guò)程中捕獲了 SQLException 異常,并通過(guò) e.getMessage() 方法獲取異常信息進(jìn)行輸出。這樣可以幫助我們更好地理解和處理與數(shù)據(jù)庫(kù)交互過(guò)程中可能出現(xiàn)的異常情況。

到此這篇關(guān)于java自定義JDBC實(shí)現(xiàn)連接池的文章就介紹到這了,更多相關(guān)java JDBC連接池內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring?Boot?中使用@KafkaListener并發(fā)批量接收消息的完整代碼

    Spring?Boot?中使用@KafkaListener并發(fā)批量接收消息的完整代碼

    kakfa是我們?cè)陧?xiàng)目開發(fā)中經(jīng)常使用的消息中間件。由于它的寫性能非常高,因此,經(jīng)常會(huì)碰到讀取Kafka消息隊(duì)列時(shí)擁堵的情況,這篇文章主要介紹了Spring?Boot?中使用@KafkaListener并發(fā)批量接收消息,需要的朋友可以參考下
    2023-02-02
  • Hadoop的安裝與環(huán)境搭建教程圖解

    Hadoop的安裝與環(huán)境搭建教程圖解

    這篇文章主要介紹了Hadoop的安裝與環(huán)境搭建教程圖解,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-06-06
  • SpringBoot處理接口冪等性的兩種方法詳解

    SpringBoot處理接口冪等性的兩種方法詳解

    接口冪等性處理算是一個(gè)非常常見的需求了,我們?cè)诤芏囗?xiàng)目中其實(shí)都會(huì)遇到。本文為大家總結(jié)了兩個(gè)處理接口冪等性的兩種常見方案,需要的可以參考一下
    2022-06-06
  • Maven編譯錯(cuò)誤:程序包c(diǎn)om.sun.*包不存在的三種解決方案

    Maven編譯錯(cuò)誤:程序包c(diǎn)om.sun.*包不存在的三種解決方案

    J2SE中的類大致可以劃分為以下的各個(gè)包:java.*,javax.*,org.*,sun.*,本文文章主要介紹了maven編譯錯(cuò)誤:程序包c(diǎn)om.sun.xml.internal.ws.spi不存在的解決方案,感興趣的可以了解一下
    2024-02-02
  • java數(shù)據(jù)輸出打印流PrintStream和PrintWriter面試精講

    java數(shù)據(jù)輸出打印流PrintStream和PrintWriter面試精講

    這篇文章主要為大家介紹了java數(shù)據(jù)輸出打印流PrintStream和PrintWriter面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • Spring File Storage文件的對(duì)象存儲(chǔ)框架基本使用小結(jié)

    Spring File Storage文件的對(duì)象存儲(chǔ)框架基本使用小結(jié)

    在開發(fā)過(guò)程當(dāng)中,會(huì)使用到存文檔、圖片、視頻、音頻等等,這些都會(huì)涉及存儲(chǔ)的問(wèn)題,文件可以直接存服務(wù)器,但需要考慮帶寬和存儲(chǔ)空間,另外一種方式就是使用云存儲(chǔ),這篇文章主要介紹了Spring File Storage文件的對(duì)象存儲(chǔ)框架基本使用小結(jié),需要的朋友可以參考下
    2024-08-08
  • 解決Weblogic部署war找不到spring配置文件的問(wèn)題

    解決Weblogic部署war找不到spring配置文件的問(wèn)題

    這篇文章主要介紹了解決Weblogic部署war找不到spring配置文件的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。
    2021-07-07
  • SpringBoot實(shí)現(xiàn)文件上傳功能

    SpringBoot實(shí)現(xiàn)文件上傳功能

    這篇文章主要為大家詳細(xì)介紹了SpringBoot實(shí)現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • JAVA中跳出當(dāng)前多重嵌套循環(huán)的方法詳解

    JAVA中跳出當(dāng)前多重嵌套循環(huán)的方法詳解

    今天在看面試題時(shí),發(fā)現(xiàn)了這個(gè)問(wèn)題,因?yàn)樵赑HP中跳出多次循環(huán)可以使用break數(shù)字來(lái)跳出多層循環(huán),但這在java中并不好使,這篇文章主要給大家介紹了關(guān)于JAVA中跳出當(dāng)前多重嵌套循環(huán)的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • Java線程池用法實(shí)戰(zhàn)案例分析

    Java線程池用法實(shí)戰(zhàn)案例分析

    這篇文章主要介紹了Java線程池用法,結(jié)合具體案例形式分析了java線程池創(chuàng)建、使用、終止等相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下
    2019-10-10

最新評(píng)論

策勒县| 鞍山市| 双城市| 梧州市| 古浪县| 克拉玛依市| 当涂县| 集贤县| 铜陵市| 河东区| 合阳县| 彰武县| 湘西| 新宁县| 宽甸| 淄博市| 繁峙县| 昭通市| 正镶白旗| 都匀市| 元朗区| 英超| 江华| 万源市| 罗山县| 呼伦贝尔市| 沛县| 高雄县| 阳东县| 荆门市| 威远县| 汉中市| 海晏县| 淳安县| 江永县| 威海市| 从江县| 无为县| 醴陵市| 汝城县| 铁岭县|