{"id":429,"date":"2024-12-21T23:39:30","date_gmt":"2024-12-21T15:39:30","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=429"},"modified":"2024-12-21T23:39:30","modified_gmt":"2024-12-21T15:39:30","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-java-%e4%b8%ad%e8%ae%be%e7%bd%ae%e4%b8%80%e4%b8%aa%e8%ae%a1%e6%97%b6%e5%99%a8%ef%bc%8c%e9%99%90%e5%88%b6%e6%95%b0%e6%8d%ae%e5%ba%93%e8%bf%9e%e6%8e%a5%e7%9a%84%e6%9c%80","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/21\/%e5%a6%82%e4%bd%95%e5%9c%a8-java-%e4%b8%ad%e8%ae%be%e7%bd%ae%e4%b8%80%e4%b8%aa%e8%ae%a1%e6%97%b6%e5%99%a8%ef%bc%8c%e9%99%90%e5%88%b6%e6%95%b0%e6%8d%ae%e5%ba%93%e8%bf%9e%e6%8e%a5%e7%9a%84%e6%9c%80\/","title":{"rendered":"\u5982\u4f55\u5728 Java \u4e2d\u8bbe\u7f6e\u4e00\u4e2a\u8ba1\u65f6\u5668\uff0c\u9650\u5236\u6570\u636e\u5e93\u8fde\u63a5\u7684\u6700\u5927\u5c1d\u8bd5\u65f6\u95f4\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728 Java \u4e2d\u8bbe\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u5c1d\u8bd5\u7684\u8ba1\u65f6\u5668\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\u5b9e\u73b0\uff1a<\/p>\n<\/blockquote>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8bbe\u7f6e\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4<\/strong>\uff1a\u5728\u6570\u636e\u5e93\u8fde\u63a5\u7684\u914d\u7f6e\u4e2d\u8bbe\u7f6e\u8d85\u65f6\uff08<code>timeout<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u5b9a\u65f6\u5668<\/strong>\uff1a\u521b\u5efa\u4e00\u4e2a\u8ba1\u65f6\u5668\uff0c\u76d1\u63a7\u6570\u636e\u5e93\u8fde\u63a5\u7684\u5c1d\u8bd5\u65f6\u95f4\uff0c\u8d85\u8fc7\u4e00\u5b9a\u65f6\u95f4\u5219\u505c\u6b62\u8fde\u63a5\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528 <code>try-catch<\/code> \u548c <code>Thread.sleep<\/code><\/strong>\uff1a\u5728\u8fde\u63a5\u6570\u636e\u5e93\u65f6\uff0c\u4f7f\u7528\u7ebf\u7a0b\u7684\u7761\u7720\u65f6\u95f4\u6765\u6a21\u62df\u8d85\u65f6\u903b\u8f91\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u505a\u6cd5\uff0c\u4f7f\u7528 <code>DriverManager<\/code> \u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u5e76\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.SQLException;\nimport java.util.concurrent.*;\n\npublic class DatabaseConnectionTimer {\n    \/\/ \u6570\u636e\u5e93\u8fde\u63a5\u53c2\u6570\n    private static final String DB_URL = \"jdbc:mysql:\/\/localhost:3306\/mydb\";\n    private static final String DB_USER = \"root\";\n    private static final String DB_PASSWORD = \"password\";\n    private static final int TIMEOUT = 10; \/\/ \u8d85\u65f6\u8bbe\u7f6e\u4e3a 10 \u79d2\n\n    public static void main(String&#91;] args) {\n        \/\/ \u521b\u5efa\u8fde\u63a5\u8d85\u65f6\u4efb\u52a1\n        ExecutorService executorService = Executors.newSingleThreadExecutor();\n        Future&lt;Connection&gt; futureConnection = executorService.submit(() -&gt; {\n            return getDatabaseConnection();\n        });\n\n        try {\n            \/\/ \u7b49\u5f85\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u8d85\u65f6\u540e\u53d6\u6d88\u8fde\u63a5\n            Connection connection = futureConnection.get(TIMEOUT, TimeUnit.SECONDS);\n            if (connection != null) {\n                System.out.println(\"\u8fde\u63a5\u6210\u529f\uff01\");\n            }\n        } catch (TimeoutException e) {\n            System.out.println(\"\u8fde\u63a5\u8d85\u65f6\uff01\");\n        } catch (Exception e) {\n            e.printStackTrace();\n        } finally {\n            executorService.shutdown();\n        }\n    }\n\n    \/\/ \u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\u7684\u65b9\u6cd5\n    private static Connection getDatabaseConnection() throws SQLException {\n        System.out.println(\"\u6b63\u5728\u5c1d\u8bd5\u8fde\u63a5\u6570\u636e\u5e93...\");\n        try {\n            \/\/ \u4f7f\u7528 DriverManager \u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\n            return DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);\n        } catch (SQLException e) {\n            throw new SQLException(\"\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25: \" + e.getMessage());\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4ee3\u7801\u5206\u6790<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>ExecutorService \u548c Future<\/strong>\uff1a\u4f7f\u7528 <code>ExecutorService<\/code> \u542f\u52a8\u4e00\u4e2a\u72ec\u7acb\u7684\u7ebf\u7a0b\u6765\u5c1d\u8bd5\u8fde\u63a5\u6570\u636e\u5e93\u3002<code>Future.get(timeout, TimeUnit)<\/code> \u4f1a\u7b49\u5f85\u76f4\u5230\u8fde\u63a5\u6210\u529f\u6216\u8d85\u65f6\uff0c\u5982\u679c\u8d85\u65f6\u5219\u629b\u51fa <code>TimeoutException<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u8fde\u63a5\u8bbe\u7f6e<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>DB_URL<\/code>\uff1a\u6570\u636e\u5e93 URL\u3002<\/li>\n\n\n\n<li><code>DB_USER<\/code> \u548c <code>DB_PASSWORD<\/code>\uff1a\u6570\u636e\u5e93\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8d85\u65f6\u5904\u7406<\/strong>\uff1a\u901a\u8fc7 <code>Future.get(TIMEOUT, TimeUnit.SECONDS)<\/code> \u8bbe\u7f6e\u8fde\u63a5\u5c1d\u8bd5\u7684\u8d85\u65f6\u65f6\u95f4\u3002\u5982\u679c\u8d85\u65f6\uff0c\u5219\u629b\u51fa <code>TimeoutException<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u6570\u636e\u5e93\u8fde\u63a5<\/strong>\uff1a\u901a\u8fc7 <code>DriverManager.getConnection()<\/code> \u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u82e5\u65e0\u6cd5\u8fde\u63a5\uff0c\u4f1a\u629b\u51fa <code>SQLException<\/code>\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u6269\u5c55\u529f\u80fd<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528\u8fde\u63a5\u6c60<\/strong>\uff1a\u5728\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u5efa\u8bae\u4f7f\u7528\u6570\u636e\u5e93\u8fde\u63a5\u6c60\uff08\u5982 HikariCP\u3001C3P0 \u6216 DBCP\uff09\u6765\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/li>\n\n\n\n<li><strong>\u914d\u7f6e JDBC \u8d85\u65f6\u53c2\u6570<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ef\u4ee5\u5728 JDBC URL \u4e2d\u914d\u7f6e\u8fde\u63a5\u8d85\u65f6\u3001\u8bfb\u53d6\u8d85\u65f6\u7b49\u53c2\u6570\uff1a<br><code>java String dbUrl = \"jdbc:mysql:\/\/localhost:3306\/mydb?connectTimeout=10000&amp;socketTimeout=10000\";<\/code><\/li>\n\n\n\n<li><code>connectTimeout<\/code>\uff1a\u8fde\u63a5\u8d85\u65f6\uff0c\u5355\u4f4d\u6beb\u79d2\u3002<\/li>\n\n\n\n<li><code>socketTimeout<\/code>\uff1a\u8bfb\u53d6\u6570\u636e\u8d85\u65f6\uff0c\u5355\u4f4d\u6beb\u79d2\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u7ed3\u8bba<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 Java \u4e2d\u5b9e\u73b0\u6570\u636e\u5e93\u8fde\u63a5\u7684\u8d85\u65f6\u63a7\u5236\uff0c\u786e\u4fdd\u7a0b\u5e8f\u5728\u8fde\u63a5\u6570\u636e\u5e93\u65f6\u4e0d\u4f1a\u65e0\u9650\u671f\u7b49\u5f85\uff0c\u5e76\u4e14\u80fd\u6839\u636e\u8d85\u65f6\u60c5\u51b5\u505a\u51fa\u76f8\u5e94\u7684\u5904\u7406\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Java \u4e2d\u8bbe\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u5c1d\u8bd5\u7684\u8ba1\u65f6\u5668\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\u5b9e\u73b0\uff1a \u4e0b\u9762\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u505a\u6cd5\uff0c\u4f7f\u7528 Driver [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68],"tags":[],"class_list":["post-429","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=429"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/429\/revisions"}],"predecessor-version":[{"id":430,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/429\/revisions\/430"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}