{"id":9819,"date":"2023-11-28T21:13:00","date_gmt":"2023-11-28T13:13:00","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=9819"},"modified":"2023-11-28T21:13:10","modified_gmt":"2023-11-28T13:13:10","slug":"java-zhongjdbc-neng-fou-chu-liblob-heclob","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/java-zhongjdbc-neng-fou-chu-liblob-heclob\/","title":{"rendered":"Java\u4e2dJDBC\u80fd\u5426\u5904\u7406Blob\u548cClob?"},"content":{"rendered":"<p>Blob\u662f\u6307\u4e8c\u8fdb\u5236\u5927\u5bf9\u8c61(Binary Large Object),\u800cClob\u662f\u6307\u5927\u5b57\u7b26\u5bf9\u8c61(Character Large Object)\uff0c\u56e0\u6b64\u5176\u4e2dBlob\u662f\u4e3a\u5b58\u50a8\u5927\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u800c\u8bbe\u8ba1\u7684\uff0c\u800cClob\u662f\u4e3a\u5b58\u50a8\u5927\u7684\u6587\u672c\u6570\u636e\u800c\u8bbe\u8ba1\u7684\u3002JDBC\u7684 PreparedStatement\u548cResultSet\u90fd\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u65b9\u6cd5\u6765\u652f\u6301Blob\u548cClob \u64cd\u4f5c\u3002\u4e0b\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528JDBC\u64cd\u4f5c LOB:<br \/>\n\u4e0b\u9762\u4ee5MySQL\u6570\u636e\u5e93\u4e3a\u4f8b\uff0c\u521b\u5efa\u4e00\u5f20\u6709\u4e09\u4e2a\u5b57\u6bb5\u7684\u7528\u6237\u8868\uff0c\u5305\u62ec\u7f16\u53f7(id) \u3001\u59d3\u540d(name\uff09\u548c\u7167\u7247(photo) \uff0c\u5efa\u8868\u8bed\u53e5\u5982\u4e0b:<br \/>\ncreate table tb_user(<br \/>\nid int primary key auto_increment,<br \/>\nname varchar(20) unique not null,<br \/>\nphoto longblob<br \/>\n);<br \/>\n\u4e0b\u9762\u7684Java\u4ee3\u7801\u5411\u6570\u636e\u5e93\u4e2d\u63d2\u5165\u4e00\u6761\u8bb0\u5f55:<br \/>\nimport java.io.FileInputStream;<br \/>\nimport java.io.IOException;<br \/>\nimport java.io.InputStream;<br \/>\nimport java.sql.Connection;<br \/>\nimport java.sql.DriverManager;<br \/>\nimport java.sql.PreparedStatement;<br \/>\nimport java.sql.SQLException;<\/p>\n<p>class JdbcLobTest {<br \/>\npublic static void main(String[] args){<br \/>\nConnection con = null;<br \/>\ntry {<br \/>\n\/\/1\u3001\u52a0\u8f7d\u9a71\u52a8(Java6\u4ee5\u4e0a\u7248\u672c\u53ef\u4ee5\u7701\u7565)<br \/>\nClass.forName(&quot;com.MySQL.jdbc.Driver&quot;);<br \/>\n\/\/2\u3001\u5efa\u7acb\u8fde\u63a5<br \/>\ncon = DriverManager.getConnection(&quot;jdbc:MySQL:\/\/localhost:3306\/test&quot;,&quot;root&quot;,&quot;123456&quot;);<br \/>\n\/\/3\u3001\u521b\u5efa\u8bed\u53e5\u5bf9\u8c61<br \/>\nPreparedStatement ps = con.prepareStatement(&quot;insert into tb_user values (default,?\uff0c?)&quot;);<br \/>\nps.setString(1,&quot;\u9a86\u660a&quot;);\/\/\u5c06SQL\u8bed\u53e5\u4e2d\u7b2c\u4e00\u4e2a\u5360\u4f4d\u7b26\u6362\u6210\u5b57\u7b26\u4e32<br \/>\ntry (InputStream in = new FilelnputStream(&quot;test.jpg&quot;)){ \/\/ Java 7\u7684 TWR<br \/>\nps.setBinaryStream(2, in);\/\/\u5c06SQL\u8bed\u53e5\u4e2d\u7b2c\u4e8c\u4e2a\u5360\u4f4d\u7b26\u6362\u6210\u4e8c\u8fdb\u5236\u6d41<br \/>\n\/\/4\u3001\u53d1\u51faSQL\u8bed\u53e5\u83b7\u5f97\u53d7\u5f71\u54cd\u884c\u6570<br \/>\nSystem.out.printIn(ps.executeUpdate() == 1?&quot;\u63d2\u5165\u6210\u529f&quot;:&quot;\u63d2\u5165\u5931\u8d25&quot;);<br \/>\n} catch(IOException e) {<br \/>\nSystem.out.println(&quot;\u8bfb\u53d6\u7167\u7247\u5931\u8d25!&quot;);<br \/>\n}<br \/>\n} catch (ClassNotFoundException | SQLException e){\/\/ Java 7\u7684\u591a\u5f02\u5e38\u6355\u83b7<br \/>\ne.printStackTrace();<br \/>\n} finally {\/\/\u91ca\u653e\u5916\u90e8\u8d44\u6e90\u7684\u4ee3\u7801\u90fd\u5e94\u5f53\u653e\u5728finally \u4e2d\u4fdd\u8bc1\u5176\u80fd\u591f\u5f97\u5230\u6267\u884c<br \/>\ntry {<br \/>\nif(con != null &amp;&amp; !con.isClosed()) {<br \/>\ncon.close();\/\/5\u3001\u91ca\u653e\u6570\u636e\u5e93\u8fde\u63a5<br \/>\ncon = null;\/\/\u6307\u793a\u5783\u573e\u56de\u6536\u5668\u53ef\u4ee5\u56de\u6536\u8be5\u5bf9\u8c61<br \/>\n}<br \/>\n} catch (SQLException e) {<br \/>\ne.printStackTrace();<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Blob\u662f\u6307\u4e8c\u8fdb\u5236\u5927\u5bf9\u8c61(Binary Large Object),\u800cClob\u662f\u6307\u5927\u5b57\u7b26\u5bf9\u8c61(Characte [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[99],"tags":[],"class_list":["post-9819","post","type-post","status-publish","format-standard","hentry","category-javabase"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9819","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=9819"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9819\/revisions"}],"predecessor-version":[{"id":9820,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9819\/revisions\/9820"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=9819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=9819"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=9819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}