{"id":1827,"date":"2022-11-27T23:20:51","date_gmt":"2022-11-27T15:20:51","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1827"},"modified":"2022-11-27T23:20:52","modified_gmt":"2022-11-27T15:20:52","slug":"%e6%89%b9%e9%87%8f%e6%8f%92%e5%85%a5%e6%95%b0%e6%8d%ae%e7%9a%84%e6%93%8d%e4%bd%9c1%e3%80%81%e6%89%b9%e9%87%8f%e6%8f%92%e5%85%a5%e6%95%b0%e6%8d%ae%e7%9a%84%e6%93%8d%e4%bd%9c2","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1827","title":{"rendered":"\u6279\u91cf\u63d2\u5165\u6570\u636e\u7684\u64cd\u4f5c1\u3001\u6279\u91cf\u63d2\u5165\u6570\u636e\u7684\u64cd\u4f5c2"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.P32.blob;\r\n\r\nimport java.sql.Connection;\r\nimport java.sql.PreparedStatement;\r\nimport java.sql.SQLException;\r\n\r\nimport org.junit.Test;\r\n\r\nimport top.qaqaq.P14.util.JDBCUtils;\r\n\r\n\/*\r\n * \u4f7f\u7528PreparedStatement\u5b9e\u73b0\u6279\u91cf\u6570\u636e\u7684\u64cd\u4f5c\r\n * \r\n * update\u3001delete\u672c\u8eab\u5c31\u5177\u6709\u6279\u91cf\u64cd\u4f5c\u7684\u6548\u679c\u3002\r\n * \u6b64\u65f6\u7684\u6279\u91cf\u64cd\u4f5c\uff0c\u4e3b\u8981\u6307\u7684\u662f\u6279\u91cf\u63d2\u5165\u3002\u4f7f\u7528PreparedStatement\u5982\u4f55\u5b9e\u73b0\u66f4\u9ad8\u6548\u7684\u6279\u91cf\u63d2\u5165\uff1f\r\n * \r\n * \u9898\u76ee\uff1a\u5411goods\u8868\u4e2d\u63d2\u516520000\u6761\u6570\u636e\r\n * CREATE TABLE goods(\r\n    id INT PRIMARY KEY AUTO_INCREMENT,\r\n    name VARCHAR(25)\r\n\t);\r\n * \u65b9\u5f0f\u4e00\uff1a\u4f7f\u7528Statement\r\n * Connection conn = JDBCUtils.getConnection();\r\n * Statement st = conn.createStatement();\r\n * for(int i = 1; i &lt;= 20000; i++){\r\n * \t\tString sql = \"insert into goods(name) values('name_\" + i + \"')\"\r\n * \t\tst.execute(sql);\r\n * }\r\n * \r\n *\/\r\npublic class InsertTest {\r\n\t\/\/\u6279\u91cf\u63d2\u5165\u7684\u65b9\u5f0f\u4e8c\uff1a\u4f7f\u7528PreparedStatement\r\n\t@Test\r\n\tpublic void testInsert1() {\r\n\t\tConnection conn = null;\r\n\t\tPreparedStatement ps = null;\r\n\t\ttry {\r\n\t\t\t\r\n\t\t\tlong start = System.currentTimeMillis();\r\n\t\t\tconn = JDBCUtils.getConnection();\r\n\t\t\tString sql = \"insert into goods(name) values(?)\";\r\n\t\t\tps = conn.prepareStatement(sql);\r\n\t\t\tfor(int i = 1; i &lt;= 20000; i++) {\r\n\t\t\t\tps.setObject(1, \"name_\" + i);\r\n\t\t\t\t\r\n\t\t\t\tps.execute();\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tlong end = System.currentTimeMillis();\r\n\t\t\t\r\n\t\t\tSystem.out.println(\"\u82b1\u8d39\u7684\u65f6\u95f4\u4e3a\uff1a\" + (end - start));\/\/20000:68766\r\n\t\t} catch (Exception e) {\r\n\t\t\te.printStackTrace();\r\n\t\t} finally {\r\n\t\t\tJDBCUtils.closeResource(conn, ps);\r\n\t\t}\r\n\t\t\r\n\t}\r\n\t\r\n\t\/*\r\n\t * \u6279\u91cf\u63d2\u5165\u7684\u65b9\u5f0f\u4e09\uff1a\r\n\t * 1. addBatch()\u3001executeBatch()\u3001clearBatch()\r\n\t * 2. mysql\u670d\u52a1\u5668\u9ed8\u8ba4\u662f\u5173\u95ed\u6279\u5904\u7406\u7684\uff0c\u6211\u4eec\u9700\u8981\u901a\u8fc7\u4e00\u4e2a\u53c2\u6570\uff0c\u8ba9mysql\u5f00\u542f\u6279\u5904\u7406\u7684\u652f\u6301\u3002\r\n\t * \t\t?rewriteBatchedStatements=true \u5199\u5728\u914d\u7f6e\u6587\u4ef6\u7684url\u540e\u9762\r\n\t * 3. \u4f7f\u7528\u66f4\u65b0\u7684mysql \u9a71\u52a8\uff1amysql-connector-java-5.1.37-bin.jar\u53ca\u4ee5\u540e\u7684\u9a71\u52a8\r\n\t * \r\n\t *\/\r\n\t@Test\r\n\tpublic void testInsert2() {\r\n\t\tConnection conn = null;\r\n\t\tPreparedStatement ps = null;\r\n\t\ttry {\r\n\t\t\t\r\n\t\t\tlong start = System.currentTimeMillis();\r\n\t\t\tconn = JDBCUtils.getConnection();\r\n\t\t\tString sql = \"insert into goods(name) values(?)\";\r\n\t\t\tps = conn.prepareStatement(sql);\r\n\t\t\tfor(int i = 1; i &lt;= 1000000; i++) {\r\n\t\t\t\tps.setObject(1, \"name_\" + i);\r\n\t\t\t\t\r\n\t\t\t\t\/\/1. \"\u6512\"sql\r\n\t\t\t\tps.addBatch();\r\n\t\t\t\t\r\n\t\t\t\tif(i % 500 == 0) {\r\n\t\t\t\t\t\/\/2. \u6267\u884c\r\n\t\t\t\t\tps.executeBatch();\r\n\t\t\t\t\t\r\n\t\t\t\t\t\/\/3. \u6e05\u7a7abatch\r\n\t\t\t\t\tps.clearBatch();\r\n\t\t\t\t}\r\n\t\t\t\t\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tlong end = System.currentTimeMillis();\r\n\t\t\t\r\n\t\t\tSystem.out.println(\"\u82b1\u8d39\u7684\u65f6\u95f4\u4e3a\uff1a\" + (end - start));\/\/20000:68766 -- 1076\r\n\t\t} catch (Exception e) {\t\t\t\t\t\t\t\t \/\/1000000:19707\r\n\t\t\te.printStackTrace();\r\n\t\t} finally {\r\n\t\t\tJDBCUtils.closeResource(conn, ps);\r\n\t\t}\r\n\t\t\r\n\t}\r\n\t\r\n\t\/\/\u6279\u91cf\u63d2\u5165\u7684\u65b9\u5f0f\u56db\uff1a\u8bbe\u7f6e\u8fde\u63a5\u4e0d\u5141\u8bb8\u81ea\u52a8\u63d0\u4ea4\u6570\u636e\r\n\t@Test\r\n\tpublic void testInsert3() {\r\n\t\tConnection conn = null;\r\n\t\tPreparedStatement ps = null;\r\n\t\ttry {\r\n\t\t\t\r\n\t\t\tlong start = System.currentTimeMillis();\r\n\t\t\tconn = JDBCUtils.getConnection();\r\n\t\t\t\r\n\t\t\t\/\/\u6d4b\u8bd5\u4e0d\u5141\u8bb8\u81ea\u52a8\u63d0\u4ea4\u6570\u636e\r\n\t\t\tconn.setAutoCommit(false);\r\n\t\t\t\r\n\t\t\tString sql = \"insert into goods(name) values(?)\";\r\n\t\t\tps = conn.prepareStatement(sql);\r\n\t\t\tfor(int i = 1; i &lt;= 1000000; i++) {\r\n\t\t\t\tps.setObject(1, \"name_\" + i);\r\n\t\t\t\t\r\n\t\t\t\t\/\/1. \"\u6512\"sql\r\n\t\t\t\tps.addBatch();\r\n\t\t\t\t\r\n\t\t\t\tif(i % 500 == 0) {\r\n\t\t\t\t\t\/\/2. \u6267\u884c\r\n\t\t\t\t\tps.executeBatch();\r\n\t\t\t\t\t\r\n\t\t\t\t\t\/\/3. \u6e05\u7a7abatch\r\n\t\t\t\t\tps.clearBatch();\r\n\t\t\t\t}\r\n\t\t\t\t\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\t\/\/\u63d0\u4ea4\u6570\u636e\r\n\t\t\tconn.commit();\r\n\t\t\t\r\n\t\t\tlong end = System.currentTimeMillis();\r\n\t\t\t\r\n\t\t\tSystem.out.println(\"\u82b1\u8d39\u7684\u65f6\u95f4\u4e3a\uff1a\" + (end - start));\/\/20000:68766 -- 1076\r\n\t\t} catch (Exception e) {\t\t\t\t\t\t\t\t \/\/1000000:19707 -- 10662\r\n\t\t\te.printStackTrace();\r\n\t\t} finally {\r\n\t\t\tJDBCUtils.closeResource(conn, ps);\r\n\t\t}\r\n\t\t\r\n\t}\r\n\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","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-1827","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\/1827"}],"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=1827"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1827\/revisions"}],"predecessor-version":[{"id":1828,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1827\/revisions\/1828"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}