{"id":1411,"date":"2024-03-28T22:49:59","date_gmt":"2024-03-28T14:49:59","guid":{"rendered":"http:\/\/zyg.luchers.cn\/?p=1411"},"modified":"2024-03-28T22:50:06","modified_gmt":"2024-03-28T14:50:06","slug":"java-jdbc","status":"publish","type":"post","link":"http:\/\/zyg.luchers.cn\/index.php\/2024\/03\/28\/java-jdbc\/","title":{"rendered":"Java-JDBC"},"content":{"rendered":"<h1>\u662f\u4ec0\u4e48\uff1f<\/h1>\n<h2>1\u3001\u7406\u89e3<\/h2>\n<p>1\u3001\u5c31\u662f\u5e2e\u52a9\u6211\u4eec\u8fde\u63a5\u548c\u7ba1\u7406\u6570\u636e\u5e93<\/p>\n<h1>\u600e\u4e48\u7528\uff1f<\/h1>\n<h2>1\u3001\u6b65\u9aa4<\/h2>\n<p>1\u3001\u6ce8\u518c\u9a71\u52a8<br \/>\n2\u3001\u83b7\u53d6\u8fde\u63a5<br \/>\n3\u3001\u7f16\u5199sql\u8bed\u53e5<br \/>\n4\u3001\u5173\u95ed\u6570\u636e\u5e93<\/p>\n<h2>2\u3001\u7b80\u5355\u7684\u4f8b\u5b50<\/h2>\n<h3>1\u3001\u4f8b\u5b501 \uff08\u666e\u901a\u8fde\u63a5\uff09<\/h3>\n<pre><code class=\"language-java\">package com.qtedu.jdbc;\nimport com.mysql.jdbc.*;\nimport java.sql.Connection;\nimport java.sql.SQLException;\nimport java.sql.Statement;\nimport java.util.Properties;\n\n\/**\n * \u8fd9\u662f\u7b2c\u4e00\u4e2ajdbc\u7a0b\u5e8f\uff0c\u5b8c\u6210\u7b80\u5355\u7684\u64cd\u4f5c\n *\/\npublic class Jdbc01 {\n    public static void main(String[] args) throws SQLException {\n\n\/\/    \u524d\u7f6e\u5de5\u4f5c\uff0c\u5b89\u88c5connector\u9a71\u52a8\n    \/\/1.\u6ce8\u518c\u9a71\u52a8\n        Driver driver = new Driver();\n\n        \/\/2.\u5f97\u5230\u8fde\u63a5\n        String url = &quot;jdbc:mysql:\/\/localhost:3306\/qt_db02&quot;;\n        Properties properties = new Properties();\n        properties.setProperty(&quot;user&quot;,&quot;root&quot;);\/\/\u7528\u6237\n        properties.setProperty(&quot;password&quot;,&quot;123456&quot;) ;\n        Connection connect =  driver.connect(url,properties);\n        \/\/    3.\u6267\u884csql\u8bed\u53e5\n        String sql = &quot;insert into actor values(null,&#039;\u7f3a\u6c27&#039;,&#039;0&#039;, &#039;1970-10-10&#039; ,&#039;110&#039;)&quot;;\n        \/\/\u53d1\u9001sql\u8bed\u53e5\u6267\u884c\n        Statement statement = connect.createStatement();\n        int rowupdate = statement.executeUpdate(sql);\/\/\u5982\u679c\u662fdml\u8bed\u53e5rowupdate\u662f\u8fd4\u56de\u7684\u51fd\u6570\n\n        System.out.println(rowupdate&gt; 0? &quot;\u6210\u529f&quot;:&quot;\u5931\u8d25&quot;);\n        \/\/    4.\u5173\u95ed\u8d44\u6e90\u8fde\u63a5\n        statement.close();\n        connect.close();\n\n    }\n}<\/code><\/pre>\n<h3>2\u3001\u4f8b\u5b502\u7528\u53cd\u5c04<\/h3>\n<pre><code class=\"language-java\">    \/\/\u65b9\u6cd5\u4e8c   \u52a8\u6001\u52a0\u8f7d\uff0c\u66f4\u52a0\u7075\u6d3b\n    @Test\n    public void connect02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {\n        Class aclass  = Class.forName(&quot;com.mysql.jdbc.Driver&quot;);\n        Driver driver =  (Driver) aclass.newInstance();\n        String url  = &quot;jdbc:mysql:\/\/localhost:3306\/qt_db02&quot;;\n        Properties properties = new Properties();\n        properties.setProperty(&quot;user&quot;,&quot;root&quot;);\n        properties.setProperty(&quot;password&quot;,&quot;123456&quot;);\n        Connection connection = driver.connect(url,properties);\n        System.out.println(&quot;\u65b9\u5f0f\u4e8c\uff1a&quot;+connection);\n    }<\/code><\/pre>\n<h3>3\u3001\u65b9\u6cd53\u4f7f\u7528DriverManager \u66ff\u4ee3 Driver \u8fdb\u884c\u7edf\u4e00\u7ba1\u7406<\/h3>\n<pre><code>public void connect03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {\n    \/\/\u53cd\u5c04\u52a0\u8f7dDriver\n    Class aclass = Class.forName(\"com.mysql.jdbc.Driver\");\n    Driver driver = (Driver) aclass.newInstance();\n    \/\/\u521b\u5efaurl \u548cuser \u548cpassword\n    String url = \"jdbc:mysql:\/\/localhost:3306\/qt_db02\";\n    String user = \"root\";\n    String password = \"123456\";\n\n    DriverManager.registerDriver(driver);\/\/\u6ce8\u518c\u9a71\u52a8\n\n    Connection connection = DriverManager.getConnection(url,user,password);\n    System.out.println(\"\u7b2c\u4e09\u79cd\u8fde\u63a5\u65b9\u5f0f\"+connection);\n}<\/code><\/pre>\n<h3>4\u3001\u4f7f\u7528Class.forName \u81ea\u52a8\u5b8c\u6210\u6ce8\u518c\u9a71\u52a8\uff0c\u7b80\u5316\u4ee3\u7801<\/h3>\n<pre><code class=\"language-java\">    public void connect04() throws ClassNotFoundException, SQLException {\n        \/\/\u52a0\u8f7dDriver \u7c7b\u65f6\uff0c\u4f1a\u81ea\u52a8\u5b8c\u6210\u9a71\u52a8\u6ce8\u518c\uff08Driver\u5e95\u5c42\u4ee3\u7801\u4e2d\u6709\u4e00\u4e2astatic\u4ee3\u7801\u5757\u7f16\u5199\u4e86DriverManager.registerDriver( new driver);\u65b9\u6cd5\n        Class aclass = Class.forName(&quot;com.mysql.jdbc.Driver&quot;);\n\n        \/\/\u521b\u5efaurl\u548cuser\u548cpassword\n        String url = &quot;jdbc:mysql:\/\/localhost:3306\/qt_db02&quot;;\n        String user = &quot;root&quot;;\n        String password = &quot;123456&quot;;\n\n        Connection connection = DriverManager.getConnection(url,user,password);\n        System.out.println(&quot;\u7b2c\u56db\u79cd\u8fde\u63a5\u65b9\u5f0f&quot;+connection);\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u662f\u4ec0\u4e48\uff1f 1\u3001\u7406\u89e3 1\u3001\u5c31\u662f\u5e2e\u52a9\u6211\u4eec\u8fde\u63a5\u548c\u7ba1\u7406\u6570\u636e\u5e93 \u600e\u4e48\u7528\uff1f 1\u3001\u6b65\u9aa4 1\u3001\u6ce8\u518c\u9a71\u52a8 2\u3001\u83b7\u53d6\u8fde\u63a5 3\u3001\u7f16\u5199 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-1411","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/1411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1411"}],"version-history":[{"count":0,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/1411\/revisions"}],"wp:attachment":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1411"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}