{"id":1770,"date":"2022-11-26T22:25:43","date_gmt":"2022-11-26T14:25:43","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1770"},"modified":"2022-11-28T20:06:00","modified_gmt":"2022-11-28T12:06:00","slug":"%e8%8e%b7%e5%8f%96%e6%95%b0%e6%8d%ae%e5%ba%93%e8%bf%9e%e6%8e%a5%e7%9a%84%e6%96%b9%e5%bc%8f%e4%b8%80%e3%80%81%e8%8e%b7%e5%8f%96%e6%95%b0%e6%8d%ae%e5%ba%93%e8%bf%9e%e6%8e%a5%e7%9a%84%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1770","title":{"rendered":"\u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\u7684\u65b9\u5f0f\u4e00\u3001\u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\u7684\u65b9\u5f0f\u4e8c\u3001\u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\u7684\u65b9\u5f0f\u4e09\u3001\u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\u7684\u65b9\u5f0f\u56db\u3001\u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\u7684\u65b9\u5f0f\u4e94"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.P7.connection;\n\nimport java.io.IOException;\nimport java.io.InputStream;\nimport java.sql.Connection;\nimport java.sql.Driver;\nimport java.sql.DriverManager;\nimport java.sql.SQLException;\nimport java.util.Properties;\n\nimport org.junit.jupiter.api.Test;\n\npublic class ConnectionTest {\n\t\n\t\/\/\u65b9\u5f0f\u4e00\uff1a\n\t@Test\n\tpublic void testConnection() throws SQLException {\n\t\t\/\/\u83b7\u53d6Driver\u5b9e\u73b0\u7c7b\u5bf9\u8c61\n\t\tDriver driver = new com.mysql.cj.jdbc.Driver();\n\t\t\n\t\t\/\/url:http:\/\/localhost:8080\/gmall\/keyboard.jpg\n\t\t\/\/jdbc:mysql\uff1a\u534f\u8bae\n\t\t\/\/localhost\uff1aip\u5730\u5740\n\t\t\/\/3306\uff1a\u9ed8\u8ba4mysql\u7684\u7aef\u53e3\u53f7\n\t\t\/\/test\uff1atest\u6570\u636e\u5e93\n\t\tString url = \"jdbc:mysql:\/\/localhost:3306\/test\";\n\t\t\/\/\u5c06\u7528\u6237\u540d\u548c\u5bc6\u7801\u5c01\u88c5\u5728Properties\u4e2d\n\t\tProperties info = new Properties();\n\t\tinfo.setProperty(\"user\",\"root\");\n\t\tinfo.setProperty(\"password\", \"123456\");\n\t\t\n\t\tConnection conn = driver.connect(url, info);\n\t\t\n\t\tSystem.out.println(conn);\n\t\t\n\t}\n\t\n\t\/\/\u65b9\u5f0f\u4e8c\uff1a\u5bf9\u65b9\u5f0f\u4e00\u7684\u8fed\u4ee3\uff1a\u5728\u5982\u4e0b\u7684\u7a0b\u5e8f\u4e2d\u4e0d\u51fa\u73b0\u7b2c\u4e09\u65b9\u7684api,\u4f7f\u5f97\u7a0b\u5e8f\u5177\u6709\u66f4\u597d\u7684\u53ef\u79fb\u690d\u6027\n\t@Test\n\tpublic void testConnection2() throws Exception {\n\t\t\/\/1.\u83b7\u53d6Driver\u5b9e\u73b0\u7c7b\u5bf9\u8c61\uff1a\u4f7f\u7528\u53cd\u5c04\n\t\tClass clazz = Class.forName(\"com.mysql.cj.jdbc.Driver\");\n\t\t\/\/jdk8\u53ca\u4e4b\u524d\n\t\tDriver driver = (Driver)clazz.newInstance();\n\t\t\/\/jdk9\u53ca\u4ee5\u540e\n\/\/\t\tDriver driver = (Driver)clazz.getDeclaredConstructor().newInstance();\n\t\t\n\t\t\/\/2.\u63d0\u4f9b\u8981\u8fde\u63a5\u7684\u6570\u636e\u5e93\n\t\tString url = \"jdbc:mysql:\/\/localhost:3306\/test\";\n\t\t\n\t\t\/\/3.\u63d0\u4f9b\u8fde\u63a5\u9700\u8981\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\n\t\tProperties info = new Properties();\n\t\tinfo.setProperty(\"user\", \"root\");\n\t\tinfo.setProperty(\"password\", \"123456\");\n\t\t\n\t\t\/\/4.\u83b7\u53d6\u8fde\u63a5\n\t\tConnection conn = driver.connect(url, info);\n\t\tSystem.out.println(conn);\n\t\t\n\t}\n\t\n\t\/\/\u65b9\u5f0f\u4e09\uff1a\u4f7f\u7528DriverManager\u66ff\u6362Driver\n\t@Test\n\tpublic void testConnection3() throws Exception{\n\t\t\/\/1.\u83b7\u53d6Driver\u5b9e\u73b0\u7c7b\u7684\u5bf9\u8c61\n\t\tClass clazz = Class.forName(\"com.mysql.cj.jdbc.Driver\");\n\t\tDriver driver = (Driver) clazz.newInstance();\n\t\t\n\t\t\/\/2.\u63d0\u4f9b\u53e6\u5916\u4e09\u4e2a\u8fde\u63a5\u7684\u57fa\u672c\u4fe1\u606f\uff1a\n\t\tString url = \"jdbc:mysql:\/\/localhost:3306\/test\";\n\t\tString user = \"root\";\n\t\tString password = \"123456\";\n\t\t\n\t\t\/\/\u6ce8\u518c\u9a71\u52a8\n\t\tDriverManager.registerDriver(driver);\n\t\t\n\t\t\/\/\u83b7\u53d6\u8fde\u63a5\n\t\tConnection conn = DriverManager.getConnection(url, user, password);\n\t\tSystem.out.println(conn);\n\t\t\n\t}\n\t\n\t\/\/\u65b9\u5f0f\u56db\uff1a\u53ef\u4ee5\u53ea\u662f\u52a0\u8f7d\u9a71\u52a8\uff0c\u4e0d\u7528\u663e\u793a\u7684\u6ce8\u518c\u9a71\u52a8\u4e86\n\t@Test\n\tpublic void testConnection4() throws Exception{\n\t\t\/\/1.\u63d0\u4f9b\u4e09\u4e2a\u8fde\u63a5\u7684\u57fa\u672c\u4fe1\u606f\uff1a\n\t\tString url = \"jdbc:mysql:\/\/localhost:3306\/test\";\n\t\tString user = \"root\";\n\t\tString password = \"123456\";\n\t\t\n\t\t\/\/2.\u52a0\u8f7dDriver\n\t\tClass.forName(\"com.mysql.cj.jdbc.Driver\");\n\t\t\/\/\u76f8\u8f83\u4e8e\u65b9\u5f0f\u4e09\uff0c\u53ef\u4ee5\u7701\u7565\u5982\u4e0b\u7684\u64cd\u4f5c\n\/\/\t\tDriver driver = (Driver) clazz.newInstance();\n\/\/\t\t\/\/\u6ce8\u518c\u9a71\u52a8\n\/\/\t\tDriverManager.registerDriver(driver);\n\t\t\/\/\u4e3a\u4ec0\u4e48\u53ef\u4ee5\u7701\u7565\u4e0a\u8ff0\u64cd\u4f5c\u5462\uff1f\n\t\t\/*\n\t\t * \u5728mysql\u7684Driver\u5b9e\u73b0\u7c7b\u4e2d\uff0c\u58f0\u660e\u4e86\u5982\u4e0b\u7684\u64cd\u4f5c\uff1a\n\t\t *  static {\n\t\t        try {\n\t\t            java.sql.DriverManager.registerDriver(new Driver());\n\t\t        } catch (SQLException E) {\n\t\t            throw new RuntimeException(\"Can't register driver!\");\n\t\t        }\n\t\t    }\n\t\t *\/\n\t\t\n\t\t\/\/\u83b7\u53d6\u8fde\u63a5\n\t\tConnection conn = DriverManager.getConnection(url, user, password);\n\t\tSystem.out.println(conn);\n\t\t\n\t}\n\t\n\t\/\/\u65b9\u5f0f\u4e94\uff08final\u7248\uff09\uff1a\u5c06\u6570\u636e\u5e93\u8fde\u63a5\u9700\u8981\u76844\u4e2a\u57fa\u672c\u4fe1\u606f\u58f0\u660e\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u901a\u8fc7\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u7684\u65b9\u5f0f\uff0c\u83b7\u53d6\u8fde\u63a5\n\t\/*\n\t * \u6b64\u79cd\u65b9\u5f0f\u7684\u597d\u5904\uff1f\n\t * 1.\u5b9e\u73b0\u4e86\u6570\u636e\u4e0e\u4ee3\u7801\u7684\u5206\u79bb\u3002\u5b9e\u73b0\u4e86\u89e3\u8026\n\t * 2.\u5982\u679c\u9700\u8981\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\u4fe1\u606f\uff0c\u53ef\u4ee5\u907f\u514d\u7a0b\u5e8f\u91cd\u65b0\u6253\u5305\u3002\n\t *\/\n\t@Test\n\tpublic void getConnection5() throws Exception {\n\t\t\n\t\t\/\/1.\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u4e2d\u76844\u4e2a\u57fa\u672c\u4fe1\u606f\n\t\tInputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream(\"jdbc.properties\");\n\t\t\n\t\tProperties pros = new Properties();\n\t\tpros.load(is);\n\t\t\n\t\tString user = pros.getProperty(\"user\");\n\t\tString password = pros.getProperty(\"password\");\n\t\tString url = pros.getProperty(\"url\");\n\t\tString driverClass = pros.getProperty(\"driverClass\");\n\t\t\n\t\t\/\/2.\u52a0\u8f7d\u9a71\u52a8\n\t\tClass.forName(driverClass);\n\t\t\n\t\t\/\/3.\u83b7\u53d6\u8fde\u63a5\n\t\tConnection conn = DriverManager.getConnection(url, user, password);\n\t\tSystem.out.println(conn);\n\t\t\n\t\t\n\t}\n\n}\n<\/code><\/pre>\n\n\n\n<p>jdbc.properties<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>user=root\npassword=123456\nurl=jdbc:mysql:\/\/localhost:3306\/test?rewriteBatchedStatements=true\ndriverClass=com.mysql.cj.jdbc.Driver<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>jdbc.properties<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[48],"class_list":["post-1770","post","type-post","status-publish","format-standard","hentry","category-jdbc-code","tag-jdbc"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1770"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1770"}],"version-history":[{"count":3,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1770\/revisions"}],"predecessor-version":[{"id":1859,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1770\/revisions\/1859"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}