{"id":1788,"date":"2022-11-26T22:31:19","date_gmt":"2022-11-26T14:31:19","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1788"},"modified":"2022-11-27T12:40:30","modified_gmt":"2022-11-27T04:40:30","slug":"preparedstatement%e9%92%88%e5%af%b9%e4%b8%8d%e5%90%8c%e8%a1%a8%e7%9a%84%e9%80%9a%e7%94%a8%e6%9f%a5%e8%af%a2%e6%93%8d%e4%bd%9c1%e3%80%81preparedstatement%e9%92%88%e5%af%b9%e4%b8%8d%e5%90%8c%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1788","title":{"rendered":"PreparedStatement\u9488\u5bf9\u4e0d\u540c\u8868\u7684\u901a\u7528\u67e5\u8be2\u64cd\u4f5c1\u3001PreparedStatement\u9488\u5bf9\u4e0d\u540c\u8868\u7684\u901a\u7528\u67e5\u8be2\u64cd\u4f5c2"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.P21.preparedstatement.crud;\r\n\r\nimport java.lang.reflect.Field;\r\nimport java.sql.Connection;\r\nimport java.sql.PreparedStatement;\r\nimport java.sql.ResultSet;\r\nimport java.sql.ResultSetMetaData;\r\nimport java.util.ArrayList;\r\nimport java.util.List;\r\n\r\nimport org.junit.Test;\r\n\r\nimport top.qaqaq.P14.util.JDBCUtils;\r\nimport top.qaqaq.P17.bean.Customer;\r\nimport top.qaqaq.P19.bean.Order;\r\n\r\n\/**\r\n * \r\n * @Description \u4f7f\u7528PreparedStatement\u5b9e\u73b0\u9488\u5bf9\u4e8e\u4e0d\u540c\u8868\u7684\u901a\u7528\u7684\u67e5\u8be2\u64cd\u4f5c\r\n * @author RichieZhang Email:18358892@qq.com\r\n * @version\r\n * @date 2022\u5e7411\u670826\u65e5\u4e0b\u53488:41:43\r\n * \r\n * \u9664\u4e86\u89e3\u51b3Statement\u7684\u62fc\u4e32\u3001sql\u6ce8\u5165\u95ee\u9898\u4e4b\u5916\uff0cPreparedStatement\u8fd8\u6709\u54ea\u4e9b\u597d\u5904\u5462\uff1f\r\n * 1. PreparedStatement\u64cd\u4f5cBlob\u7684\u6570\u636e\uff0c\u800cStatement\u505a\u4e0d\u5230\u3002\r\n * 2. PreparedStatement\u53ef\u4ee5\u5b9e\u73b0\u66f4\u9ad8\u6548\u7684\u6279\u91cf\u64cd\u4f5c\u3002\r\n *\r\n *\/\r\npublic class PreparedStatementQueryTest {\r\n\t\r\n\t@Test\r\n\tpublic void testGetForList() {\r\n\t\t\r\n\t\tString sql = \"select id,name,email from customers where id &lt; ?\";\r\n\t\tList&lt;Customer> list = getForList(Customer.class, sql, 12);\r\n\t\tlist.forEach(System.out::println);\r\n\t\t\r\n\t\tString sql1 = \"select order_id orderId,order_name orderName from `order`\";\r\n\t\tList&lt;Order> orderList = getForList(Order.class, sql1);\r\n\t\torderList.forEach(System.out::println);\r\n\t\t\r\n\t}\r\n\t\r\n\tpublic &lt;T> List&lt;T> getForList(Class&lt;T> clazz, String sql, Object ...args){\r\n\t\tConnection conn = null;\r\n\t\tPreparedStatement ps = null;\r\n\t\tResultSet rs = null;\r\n\t\ttry {\r\n\t\t\tconn = JDBCUtils.getConnection();\r\n\r\n\t\t\tps = conn.prepareStatement(sql);\r\n\t\t\tfor (int i = 0; i &lt; args.length; i++) {\r\n\t\t\t\tps.setObject(i + 1, args&#91;i]);\r\n\t\t\t}\r\n\r\n\t\t\trs = ps.executeQuery();\r\n\t\t\t\/\/ \u83b7\u53d6\u7ed3\u679c\u96c6\u7684\u5143\u6570\u636e : ResultSetMetaData\r\n\t\t\tResultSetMetaData rsmd = rs.getMetaData();\r\n\t\t\t\/\/ \u901a\u8fc7ResultSetMetaData\u83b7\u53d6\u7ed3\u679c\u96c6\u4e2d\u7684\u5217\u6570\r\n\t\t\tint columnCount = rsmd.getColumnCount();\r\n\t\t\t\/\/\u521b\u5efa\u96c6\u5408\u5bf9\u8c61\r\n\t\t\tArrayList&lt;T> list = new ArrayList&lt;T>();\r\n\t\t\twhile (rs.next()) {\r\n\t\t\t\tT t = clazz.newInstance();\r\n\t\t\t\t\/\/ \u5904\u7406\u7ed3\u679c\u96c6\u4e00\u884c\u6570\u636e\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5217:\u7ed9t\u5bf9\u8c61\u6307\u5b9a\u7684\u5c5e\u6027\u8d4b\u503c\r\n\t\t\t\tfor (int i = 0; i &lt; columnCount; i++) {\r\n\t\t\t\t\t\/\/ \u83b7\u53d6\u5217\u503c\r\n\t\t\t\t\tObject columnvalue = rs.getObject(i + 1);\r\n\r\n\t\t\t\t\t\/\/ \u83b7\u53d6\u6bcf\u4e2a\u5217\u7684\u5217\u540d\r\n\/\/\t\t\t\t\tString columnName = rsmd.getColumnName(i + 1);\r\n\t\t\t\t\tString columnLabel = rsmd.getColumnLabel(i + 1);\r\n\r\n\t\t\t\t\t\/\/ \u7ed9t\u5bf9\u8c61\u6307\u5b9a\u7684columnName\u5c5e\u6027\uff0c\u8d4b\u503c\u4e3acolumnvalue\uff1b\u901a\u8fc7\u53cd\u5c04\r\n\t\t\t\t\tField field = clazz.getDeclaredField(columnLabel);\r\n\t\t\t\t\tfield.setAccessible(true);\r\n\t\t\t\t\tfield.set(t, columnvalue);\r\n\r\n\t\t\t\t}\r\n\t\t\t\tlist.add(t);\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\treturn list;\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, rs);\r\n\t\t}\r\n\r\n\t\treturn null;\r\n\t}\r\n\t\r\n\t@Test\r\n\tpublic void testGetInstance() {\r\n\t\tString sql = \"select id,name,email from customers where id = ?\";\r\n\t\tCustomer customer = getInstance(Customer.class, sql, 12);\r\n\t\tSystem.out.println(customer);\r\n\t\t\r\n\t\tString sql1 = \"select order_id orderId,order_name orderName from `order` where order_id = ?\";\r\n\t\tOrder order = getInstance(Order.class, sql1, 1);\r\n\t\tSystem.out.println(order);\r\n\t}\r\n\t\r\n\t\/**\r\n\t * \r\n\t* @Description \r\n\t* @author RichieZhang\t\u9488\u5bf9\u4e8e\u4e0d\u540c\u7684\u8868\u7684\u901a\u7528\u7684\u67e5\u8be2\u64cd\u4f5c\uff0c\u8fd4\u56de\u8868\u4e2d\u7684\u4e00\u6761\u8bb0\u5f55\r\n\t* @date 2022\u5e7411\u670826\u65e5\u4e0b\u53489:04:58\r\n\t* @param &lt;T>\r\n\t* @param clazz\r\n\t* @param sql\r\n\t* @param args\r\n\t* @return\r\n\t *\/\r\n\tpublic &lt;T> T getInstance(Class&lt;T> clazz, String sql, Object... args) {\r\n\t\tConnection conn = null;\r\n\t\tPreparedStatement ps = null;\r\n\t\tResultSet rs = null;\r\n\t\ttry {\r\n\t\t\tconn = JDBCUtils.getConnection();\r\n\r\n\t\t\tps = conn.prepareStatement(sql);\r\n\t\t\tfor (int i = 0; i &lt; args.length; i++) {\r\n\t\t\t\tps.setObject(i + 1, args&#91;i]);\r\n\t\t\t}\r\n\r\n\t\t\trs = ps.executeQuery();\r\n\t\t\t\/\/ \u83b7\u53d6\u7ed3\u679c\u96c6\u7684\u5143\u6570\u636e : ResultSetMetaData\r\n\t\t\tResultSetMetaData rsmd = rs.getMetaData();\r\n\t\t\t\/\/ \u901a\u8fc7ResultSetMetaData\u83b7\u53d6\u7ed3\u679c\u96c6\u4e2d\u7684\u5217\u6570\r\n\t\t\tint columnCount = rsmd.getColumnCount();\r\n\r\n\t\t\tif (rs.next()) {\r\n\t\t\t\tT t = clazz.newInstance();\r\n\t\t\t\t\/\/ \u5904\u7406\u7ed3\u679c\u96c6\u4e00\u884c\u6570\u636e\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5217\r\n\t\t\t\tfor (int i = 0; i &lt; columnCount; i++) {\r\n\t\t\t\t\t\/\/ \u83b7\u53d6\u5217\u503c\r\n\t\t\t\t\tObject columnvalue = rs.getObject(i + 1);\r\n\r\n\t\t\t\t\t\/\/ \u83b7\u53d6\u6bcf\u4e2a\u5217\u7684\u5217\u540d\r\n\/\/\t\t\t\t\tString columnName = rsmd.getColumnName(i + 1);\r\n\t\t\t\t\tString columnLabel = rsmd.getColumnLabel(i + 1);\r\n\r\n\t\t\t\t\t\/\/ \u7ed9t\u5bf9\u8c61\u6307\u5b9a\u7684columnName\u5c5e\u6027\uff0c\u8d4b\u503c\u4e3acolumnvalue\uff1b\u901a\u8fc7\u53cd\u5c04\r\n\t\t\t\t\tField field = clazz.getDeclaredField(columnLabel);\r\n\t\t\t\t\tfield.setAccessible(true);\r\n\t\t\t\t\tfield.set(t, columnvalue);\r\n\r\n\t\t\t\t}\r\n\t\t\t\treturn t;\r\n\t\t\t}\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, rs);\r\n\t\t}\r\n\r\n\t\treturn null;\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-1788","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\/1788"}],"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=1788"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1788\/revisions"}],"predecessor-version":[{"id":1789,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1788\/revisions\/1789"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}