{"id":121272,"date":"2018-09-29T08:53:05","date_gmt":"2018-09-29T00:53:05","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=121272"},"modified":"2018-09-25T10:59:06","modified_gmt":"2018-09-25T02:59:06","slug":"jdbc-2","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/jdbc-2.html","title":{"rendered":"JDBC Dbutils \u5c01\u88c5\u6846\u67b6\uff08\u4e8c\uff09"},"content":{"rendered":"

\u5bf9JDBC\u7684\u5c01\u88c5\uff0c\u4e3b\u8981\u5728\u4e09\u4e2a\u65b9\u9762\u7684\u4f18\u5316\uff1a<\/p>\n

\u8d44\u6e90\u7684\u5f00\u542f\u548c\u5173\u95ed\uff0c\u6bcf\u6b21\u4e0e\u6570\u636e\u5e93\u7684\u4ea4\u4e92\u90fd\u5199\u4e00\u904d\uff0c\u9020\u6210\u5927\u91cf\u7684\u91cd\u590d<\/p>\n

sql\u5bf9\u8c61\u548c\u53c2\u6570\u4f20\u9012\u7684\u7b80\u5316<\/p>\n

\u7ed3\u679c\u96c6\u4e0ejava\u5bf9\u8c61\u7684\u6620\u5c04<\/p>\n

Dbutils\u4f5c\u4e3a\u4e00\u79cd\u521d\u7ea7\u7684JDBC\u5c01\u88c5\u6846\u67b6\uff0c\u5bf9JDBC\u8fdb\u884c\u4e86\u4e00\u5b9a\u7684\u5c01\u88c5\uff0c\u4f46\u662f\u9762\u5bf9\u590d\u6742\u4e1a\u52a1\u7684\u652f\u6301\u5219\u4e0d\u591f\u3002\u6765\u770b\u4e00\u4e2a\u7b80\u5355\u7684demo\uff1a<\/p>\n

\u5148\u5b9a\u4e49\u4e2aConnection\u7684\u5e2e\u52a9\u7c7b\uff0c\u7528\u6765\u6ce8\u518c\u9a71\u52a8\u548c\u521b\u5efa\u8fde\u63a5<\/p>\n

\r\npublic class ConnectionUtil {\r\n\t\r\n\tstatic {\r\n\t\ttry {\r\n\t\t\tClass.forName(\"com.mysql.jdbc.Driver\");\r\n\t\t} catch (ClassNotFoundException e) {\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t}\r\n\r\n\tpublic static Connection getConnection() throws SQLException{\r\n\t\treturn DriverManager.getConnection(\"jdbc:mysql:\/\/192.168.1.160:3306\/lichao\", \"root\", \"root\");\r\n\t}\r\n}\r\n<\/pre>\n

Dbutils\u4ee5QueryRunner\u4e3a\u5165\u53e3\uff0c\u4f20\u5165Connection\u8fde\u63a5\u548csql\uff0c\u4ee5\u53ca\u7ed3\u679c\u96c6\u7684\u5904\u7406\u5668<\/p>\n

\r\nResultSetHandler rsh = new ResultSetHandler<\/user>() {\r\n\r\n\t@Override\r\n\tpublic User handle(ResultSet rs) throws SQLException {\r\n\t\trs.next();\r\n\t\tLong id = rs.getLong(\"id\");\r\n\t\tString name = rs.getString(\"name\");\r\n\t\tInteger age = rs.getInt(\"age\");\r\n\t\treturn new User(id, name, age);\r\n\t}\r\n};\r\n<\/user><\/pre>\n
\r\nQueryRunner queryRunner = new QueryRunner();\r\nUser user = queryRunner.query(ConnectionUtil.getConnection(), \"select * from user limit 1\", rsh);\r\nSystem.out.println(user);\r\n<\/pre>\n

\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2aResultSetHandler\u7684\u5b9e\u73b0\u7c7b\uff0c\u5e76\u5b9e\u73b0handler\u65b9\u6cd5\uff0c\u65b9\u6cd5\u4e2d\u5b8c\u6210ResultSet\u5230java\u5bf9\u8c61\u4e4b\u95f4\u7684\u8f6c\u6362\u3002<\/p>\n

Connection\u5e2e\u52a9\u7c7b\u51cf\u5c11\u4e86Connection\u521b\u5efa\u7684\u91cd\u590d\u4ee3\u7801\uff0cQueryRunner\u5e2e\u6211\u4eec\u5904\u7406\u4e86Statement\u7684\u64cd\u4f5c\u4ee5\u53ca\u8d44\u6e90\u7684\u5173\u95ed(Connection\u9ed8\u8ba4\u662f\u4e0d\u5173\u95ed\u7684)\uff0c\u4f46\u662fResultSet\u5230Java\u5bf9\u8c61\u7684\u6620\u5c04\u8fd8\u662f\u7531\u81ea\u5df1\u6765\u5b9e\u73b0\u3002\u53ef\u4e0d\u53ef\u4ee5\u5c06\u7ed3\u679c\u96c6\u548cjava\u5bf9\u8c61\u7684\u6620\u5c04\u518d\u7b80\u5316\u5462\uff1fDbutils\u63d0\u4f9b\u4e86ResultSetHandler\u7684\u4e00\u79cd\u5b9e\u73b0BeanHandler\uff0c\u652f\u6301\u5c06\u6570\u636e\u5e93\u5b57\u6bb5\u76f4\u63a5\u6620\u5c04\u5230java\u5bf9\u8c61\u4e2d\u3002<\/p>\n

\r\nResultSetHandler h = new BeanHandler<\/user>(User.class);\r\nQueryRunner queryRunner = new QueryRunner();\r\nUser user = queryRunner.query(ConnectionUtil.getConnection(), \"select * from user limit 1\", h);\r\nSystem.out.println(user);\r\n<\/user><\/pre>\n

BeanHandler\u628a\u6570\u636e\u5e93\u5b57\u6bb5\u540cUser\u7c7b\u4e2d\u7684\u5c5e\u6027\u8fdb\u884c\u5339\u914d\uff0c\u53cd\u5c04\u751f\u6210\u65b0\u7684\u5bf9\u8c61\u3002<\/p>\n

\u4f46\u662f\u5b58\u5728\u4e00\u4e2a\u95ee\u9898\uff0c\u6570\u636e\u5e93\u5b57\u6bb5\u4e00\u822c\u4ee5\u4e0b\u5212\u7ebf\u5206\u9694\uff0c\u800cJava\u5219\u662f\u9a7c\u5cf0\u5f0f\u547d\u540d\uff0c\u56e0\u6b64\u5b57\u6bb5\u540d\u79f0\u76f4\u63a5\u5339\u914d\u5f80\u5f80\u4e0d\u884c\u3002<\/p>\n

Dbutils\u63d0\u4f9b\u4e86BeanProcessor\u6765\u5904\u7406\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ea\u8981\u5c06\u6570\u636e\u5e93\u5b57\u6bb5\u540d\u548cjava\u7c7b\u4e2d\u7684\u5c5e\u6027\u540d\u901a\u8fc7Map\u4f20\u5165\uff0c\u5373\u53ef\u5b8c\u6210\u6620\u5c04\u5173\u7cfb\u3002\u6bd4\u5982\u589e\u52a0\u4e00\u4e2a\u5b57\u6bb5\u6708\u85aa(month_salary\uff0c\u5bf9\u5e94\u5c5e\u6027monthSalary)\u3002<\/p>\n

\r\nMap map = new HashMap<\/string>();\r\nmap.put(\"month_salary\", \"monthSalary\");\r\nBeanProcessor customBeanProcessor = new BeanProcessor(map);\r\nResultSetHandler h = new BeanHandler<\/user>(User.class,new BasicRowProcessor(customBeanProcessor));\r\n\r\nQueryRunner queryRunner = new QueryRunner();\r\nUser user = queryRunner.query(ConnectionUtil.getConnection(), \"select * from user limit 1\", h);\r\nSystem.out.println(user);\r\n\r\nDbutils\u5bf9\u67e5\u8be2\u591a\u6761\u6570\u636e\u4e5f\u63d0\u4f9b\u4e86\u652f\u6301\r\n\r\nResultSetHandler> h = new BeanListHandler(User.class);\r\nQueryRunner queryRunner = new QueryRunner();\r\nList<\/user> users = queryRunner.query(ConnectionUtil.getConnection(), \"select * from user\", h);\r\nfor(User user : users){\r\n\tSystem.out.println(user);\r\n}\r\n\r\nDbutils\u8fd8\u63d0\u4f9b\u4e86\u5f02\u6b65\u5904\u7406\u7684\u65b9\u5f0f\uff0cAsyncQueryRunner\u7c7b\u652f\u6301\u4e86\u8fd9\u4e2a\u7279\u6027\u3002\r\n\r\nResultSetHandler> rsh = new BeanListHandler(User.class);\r\nAsyncQueryRunner asyncQueryRunner = new AsyncQueryRunner(Executors.newCachedThreadPool());\r\n\r\nFuture> future = asyncQueryRunner.query(ConnectionUtil.getConnection(),  \"select * from user\", rsh);\r\n\r\nList users = null;\r\ntry {\r\n\tusers = future.get(5, TimeUnit.SECONDS);\r\n} catch (InterruptedException | ExecutionException | TimeoutException e) {\r\n\te.printStackTrace();\r\n}\r\n\r\nfor(int i=0;i rsh = new ResultSetHandler() {\r\n\r\n\t@Override\r\n\tpublic User handle(ResultSet rs) throws SQLException {\r\n\t\trs.next();\r\n\t\tLong id = rs.getLong(\"id\");\r\n\t\tString name = rs.getString(\"name\");\r\n\t\tInteger age = rs.getInt(\"age\");\r\n\t\treturn new User(id, name, age);\r\n\t}\r\n};\r\n\r\nDataSource dataSource = null;\/\/ establish DataSource\r\nQueryRunner queryRunner = new QueryRunner(dataSource);\r\nUser user = queryRunner.query(\"select * from user limit 1\", rsh);\r\nSystem.out.println(user);\r\n<\/user><\/users><\/user><\/list><\/user><\/list><\/user><\/list><\/user><\/string><\/pre>\n

Dbutils\u5bf9JDBC\u7684\u5c01\u88c5\u907f\u514d\u4e86\u5197\u6742\u7684\u4ee3\u7801\uff0c\u5e76\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u7684API\uff0c\u652f\u6301Connection\u548cDataSource\u4e24\u79cd\u65b9\u5f0f\u83b7\u5f97\u8fde\u63a5\u3002\u5728\u7b80\u5355\u7684\u9879\u76ee\u4e2d\uff0c\u4f7f\u7528Dbutils\u4f1a\u5e26\u6765\u5f88\u5927\u7684\u4fbf\u5229\u3002<\/p>\n

\n

\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.voidcn.com\/article\/p-byjhasik-bro.html<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttps:\/\/lrxjmw.cn\/jdbc-2.html<\/a>\u7f16\u8f91\uff1a\u4f55\u4e91\u8273\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u5bf9JDBC\u7684\u5c01\u88c5\uff0c\u4e3b\u8981\u5728\u4e09\u4e2a\u65b9\u9762\u7684\u4f18\u5316\uff1a \u8d44\u6e90\u7684\u5f00\u542f\u548c\u5173\u95ed\uff0c\u6bcf\u6b21\u4e0e\u6570\u636e\u5e93\u7684\u4ea4\u4e92\u90fd\u5199\u4e00\u904d\uff0c\u9020\u6210\u5927\u91cf\u7684\u91cd\u590d sql […]<\/p>\n","protected":false},"author":1479,"featured_media":121283,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-121272","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/121272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=121272"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/121272\/revisions"}],"predecessor-version":[{"id":121521,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/121272\/revisions\/121521"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/121283"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=121272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=121272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=121272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}