{"id":1825,"date":"2022-11-27T23:20:26","date_gmt":"2022-11-27T15:20:26","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1825"},"modified":"2022-11-27T23:20:26","modified_gmt":"2022-11-27T15:20:26","slug":"%e5%90%91%e6%95%b0%e6%8d%ae%e8%a1%a8%e4%b8%ad%e6%8f%92%e5%85%a5blob%e7%b1%bb%e5%9e%8b%e6%95%b0%e6%8d%ae%e3%80%81%e4%bb%8e%e6%95%b0%e6%8d%ae%e8%a1%a8%e4%b8%ad%e8%af%bb%e5%8f%96blob%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1825","title":{"rendered":"\u5411\u6570\u636e\u8868\u4e2d\u63d2\u5165Blob\u7c7b\u578b\u6570\u636e\u3001\u4ece\u6570\u636e\u8868\u4e2d\u8bfb\u53d6Blob\u7c7b\u578b\u6570\u636e\u3001\u63d2\u5165Blob\u5b57\u6bb5\u7279\u6b8a\u60c5\u51b5\u7684\u8bf4\u660e"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.P29.blob;\r\n\r\nimport java.io.File;\r\nimport java.io.FileInputStream;\r\nimport java.io.FileNotFoundException;\r\nimport java.io.FileOutputStream;\r\nimport java.io.IOException;\r\nimport java.io.InputStream;\r\nimport java.sql.Blob;\r\nimport java.sql.Connection;\r\nimport java.sql.Date;\r\nimport java.sql.PreparedStatement;\r\nimport java.sql.ResultSet;\r\nimport java.sql.SQLException;\r\n\r\nimport org.junit.Test;\r\n\r\nimport top.qaqaq.P14.util.JDBCUtils;\r\nimport top.qaqaq.P17.bean.Customer;\r\n\r\n\/**\r\n * \r\n* @Description\t\u6d4b\u8bd5\u4f7f\u7528PreparedStatement\u64cd\u4f5cBlob\u7c7b\u578b\u7684\u6570\u636e\r\n* @author RichieZhang Email:18358892@qq.com\r\n* @version\r\n* @date 2022\u5e7411\u670827\u65e5\u4e0b\u53482:45:11\r\n*\r\n *\/\r\npublic class BlobTest {\r\n\t\r\n\t\/\/\u5411\u6570\u636e\u8868customers\u4e2d\u63d2\u5165Blob\u7c7b\u578b\u7684\u5b57\u6bb5\r\n\t@Test\r\n\tpublic void testInsert() throws Exception {\r\n\t\tConnection conn = JDBCUtils.getConnection();\r\n\t\tString sql = \"insert into customers(name,email,birth,photo) values(?,?,?,?)\";\r\n\t\t\r\n\t\tPreparedStatement ps = conn.prepareStatement(sql);\r\n\t\t\r\n\t\tps.setObject(1, \"\u738b\u4e94\");\r\n\t\tps.setObject(2, \"zhang@126.com\");\r\n\t\tps.setObject(3, \"1992-09-08\");\r\n\t\tFileInputStream is = new FileInputStream(new File(\"\u7535\u8111\u58c1\u7eb8.jpg\"));\r\n\t\tps.setBlob(4, is);\r\n\t\t\r\n\t\tps.execute();\r\n\t\t\r\n\t\tJDBCUtils.closeResource(conn, ps);\r\n\t}\r\n\t\r\n\t\/\/\u67e5\u8be2\u6570\u636e\u8868customers\u4e2dBlob\u7c7b\u578b\u7684\u5b57\u6bb5\r\n\t@Test\r\n\tpublic void testQuery() {\r\n\t\tConnection conn = null;\r\n\t\tPreparedStatement ps = null;\r\n\t\tInputStream is = null;\r\n\t\tFileOutputStream fos = null;\r\n\t\tResultSet rs = null;\r\n\t\ttry {\r\n\t\t\tconn = JDBCUtils.getConnection();\r\n\t\t\tString sql = \"select id,name,email,birth,photo from customers where id = ?\";\r\n\t\t\tps = conn.prepareStatement(sql);\r\n\t\t\tps.setInt(1, 22);\r\n\t\t\t\r\n\t\t\trs = ps.executeQuery();\r\n\t\t\tif(rs.next()) {\r\n\t\t\t\t\/\/\u65b9\u5f0f\u4e00\uff1a\r\n\/\/\t\t\t\tint id = rs.getInt(1);\r\n\/\/\t\t\t\tString name = rs.getString(2);\r\n\/\/\t\t\t\tString email = rs.getString(3);\r\n\/\/\t\t\t\tDate birth = rs.getDate(4);\r\n\t\t\t\t\/\/\u65b9\u5f0f\u4e8c\uff1a\r\n\t\t\t\tint id = rs.getInt(\"id\");\r\n\t\t\t\tString name = rs.getString(\"name\");\r\n\t\t\t\tString email = rs.getString(\"email\");\r\n\t\t\t\tDate birth = rs.getDate(\"birth\");\r\n\t\t\t\t\r\n\t\t\t\tCustomer cust = new Customer(id, name, email, birth);\r\n\t\t\t\tSystem.out.println(cust);\r\n\t\t\t\t\r\n\t\t\t\t\/\/\u5c06Blob\u7c7b\u578b\u7684\u5b57\u6bb5\u4e0b\u8f7d\u4e0b\u6765\uff0c\u4ee5\u6587\u4ef6\u7684\u65b9\u5f0f\u4fdd\u5b58\u5728\u672c\u5730\r\n\t\t\t\tBlob photo = rs.getBlob(\"photo\");\r\n\t\t\t\tis = photo.getBinaryStream();\r\n\t\t\t\tfos = new FileOutputStream(\"zhang.jpg\");\r\n\t\t\t\tbyte&#91;] buffer = new byte&#91;1024];\r\n\t\t\t\tint len;\r\n\t\t\t\twhile((len = is.read(buffer)) != -1) {\r\n\t\t\t\t\tfos.write(buffer,0,len);\r\n\t\t\t\t}\r\n\t\t\t\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\ttry {\r\n\t\t\t\tif(is != null)\r\n\t\t\t\t\tis.close();\r\n\t\t\t} catch (IOException e) {\r\n\t\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\t\te.printStackTrace();\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\ttry {\r\n\t\t\t\tif(fos != null)\r\n\t\t\t\t\tfos.close();\r\n\t\t\t} catch (IOException e) {\r\n\t\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\t\te.printStackTrace();\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tJDBCUtils.closeResource(conn, ps, rs);\r\n\t\t}\r\n\t\t\r\n\t}\r\n\t\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-1825","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\/1825"}],"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=1825"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1825\/revisions"}],"predecessor-version":[{"id":1826,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1825\/revisions\/1826"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}