{"id":812,"date":"2024-12-29T21:50:37","date_gmt":"2024-12-29T13:50:37","guid":{"rendered":"https:\/\/blog.sssn.tech\/?p=812"},"modified":"2025-03-19T00:37:12","modified_gmt":"2025-03-18T16:37:12","slug":"%e5%90%89%e5%a4%a7-%e4%bc%81%e4%b8%9a%e7%ba%a7%e6%95%b0%e6%8d%ae%e5%ba%93%e5%ba%94%e7%94%a8%e5%bc%80%e5%8f%91","status":"publish","type":"post","link":"https:\/\/blog.sssn.tech\/?p=812","title":{"rendered":"[\u771f\u9898+\u5168\u7a0b\u5b66\u4e60\u7b14\u8bb0] -\u4f01\u4e1a\u7ea7\u6570\u636e\u5e93\u5e94\u7528\u5f00\u53d1"},"content":{"rendered":"<h1>\u5409\u6797\u5927\u5b66\u8f6f\u4ef6\u5b66\u9662 \u4f01\u4e1a\u7ea7\u6570\u636e\u5e93\u5e94\u7528\u5f00\u53d1<\/h1>\n<h2>\u8003\u9898\u56de\u5fc6<\/h2>\n<p>\u5409\u6797\u5927\u5b66\u8f6f\u4ef6\u5b66\u9662\u4f01\u4e1a\u7ea7\u6570\u636e\u5e93\u5e94\u7528\u5f00\u53d1, \u603b\u5171\u5c31\u590d\u4e60\u4e861\u5929, \u5185\u5bb9\u4e0d\u7b97\u591a, \u4f46\u662f\u8003\u8bd5\u51fa\u9898\u80cc\u523a\u4e86<\/p>\n<p>\u6574\u7406\u4e86\u4e00\u4e0b\u77e5\u8bc6\u70b9, \u4e3b\u8981\u6765\u6e90\u662fPPT, \u5b9e\u9a8c\u62a5\u544a, \u5386\u5e74\u9898<\/p>\n<p><strong>\u5386\u5e74\u9898\u51e0\u4e4e\u53ea\u51fa\u5b9e\u9a8c\u5185\u5bb9, 22\u7ea7\u8003\u7684\u548c\u5f80\u5e74\u975e\u5e38\u4e0d\u4e00\u6837<\/strong>, \u5f80\u5e74\u9898\u91cc\u5fc5\u8003\u7684\u8fde\u63a5\u6570\u636e\u5e93, \u52a0\u8f7d\u9a71\u52a8\u6ca1\u8003, db2\u76f8\u5173\u6ca1\u8003, Java\u4e5f\u6ca1\u8981\u6c42, \u5168\u7a0b\u53ea\u51fa\u5e94\u7528\u9898, \u81ea\u9009\u8bed\u8a00+\u5b9e\u73b0\u65b9\u5f0f, \u611f\u89c9\u6574\u4e2a\u5377\u5b50\u548cdb2\u6ca1\u5565\u5173\u7cfb, \u53cd\u800c\u6709\u70b9\u8d34\u8fd1\u8bfe\u7a0b\u540d\u4e86<\/p>\n<p>\u56de\u5fc6\u4e00\u4e0b\u8003\u9898<\/p>\n<p><strong>\u7b2c\u4e00\u5927\u9898, 5\u5c0f\u9898, 15\u5206\u4e00\u9053, \u5171\u8ba175\u5206<\/strong><\/p>\n<p>\u9898\u5e72: \u7ed9\u4e86\u4e00\u4e2aemployee\u8868, \u7ed3\u6784(No, name, bitrhday, age, salary, bonus, resume, photo)<\/p>\n<ol>\n<li>\u5f00\u5e55\u96f7\u51fb, \u95ee\u63d2\u5165\u5927\u91cf\u6570\u636e\u5982\u4f55\u6bcf500\u884c\u63d0\u4ea4\u4e00\u6b21, \u4f46\u662f\u6ca1\u8ba9\u6062\u590d\u72b6\u6001, \u7b80\u5355\u4e86\u4e00\u70b9<\/li>\n<\/ol>\n<blockquote>\n<p>\u7b54\u6848\u6211\u8fd9\u4e2a\u7b14\u8bb0\u91cc\u6709, \u4e5f\u6709\u540c\u5b66\u7528\u5b50\u67e5\u8be2\u505a, \u8003\u573a\u4e0a\u6211\u662f\u5148SELECT\u51fa\u6765, \u7136\u540ers\u904d\u5386, \u7528wasNull\u5224\u65ad, \u518d\u63d2, \u8ba1\u6570\u5668%500\u63d0\u4ea4<\/p>\n<\/blockquote>\n<ol start=\"2\">\n<li>\u67e5\u8868, \u628a\u5956\u91d1=0\u7684\u5458\u5de5\u7ee9\u6548\u8bbe\u7f6e\u4e3a\u4e0d\u5408\u683c<\/li>\n<\/ol>\n<blockquote>\n<p>for update &amp; where current of, \u5e74\u5e74\u8003<\/p>\n<\/blockquote>\n<ol start=\"3\">\n<li>\u63d2\u5165\u65b0\u5458\u5de5\u4fe1\u606f, \u4ece\u952e\u76d8\u8bfb\u5165<\/li>\n<\/ol>\n<blockquote>\n<p>Scanner, setNull()<\/p>\n<\/blockquote>\n<ol start=\"4\">\n<li>\u5f02\u5e38\u5904\u7406, \u63d2\u5165\u6570\u636e, \u7ed9\u4e86\u8fdd\u53cd\u7ea6\u675f\u7684SQLState, \u8ba9\u5904\u7406, \u5176\u5b9e\u5c31\u662f\u6253\u5370\u5b57\u7b26\u4e32<\/li>\n<\/ol>\n<blockquote>\n<p>try-catch, &quot;XXX&quot;.equals(e.getSQLState())<\/p>\n<\/blockquote>\n<ol start=\"5\">\n<li>\u66f4\u65b0\u4e00\u4e2a\u5458\u5de5\u7684photo<\/li>\n<\/ol>\n<blockquote>\n<p>BLOB\u63d2\u5165, \u5404\u79cd\u6d41<\/p>\n<\/blockquote>\n<p><strong>\u7b2c\u4e8c\u5927\u9898, \u5171\u8ba125\u5206<\/strong><\/p>\n<p>\u5f00\u653e\u9898, \u7ed9\u4e86\u4e2a\u7279\u522b\u5e7d\u9ed8\u768412306 app\u622a\u56fe, \u5708\u4e86\u51e0\u4e2a\u529f\u80fd\u95ee\u600e\u4e48\u5b9e\u73b0<\/p>\n<ol>\n<li>\u4e0d\u767b\u5f55\u7684\u7528\u6237\u53ef\u4ee5\u770b\u5217\u8f66\u8868, \u4f46\u662f\u53ea\u6709\u767b\u5f55\u7528\u6237\u53ef\u4ee5\u8d2d\u7968, \u95ee\u600e\u4e48\u5b9e\u73b0<\/li>\n<\/ol>\n<blockquote>\n<p>\u6211\u5199\u7684\u53ea\u8bfb\u89c6\u56fe, \u4e0d\u77e5\u9053\u4ed6\u60f3\u95ee\u5565<\/p>\n<\/blockquote>\n<ol start=\"2\">\n<li>\u81ea\u884c\u5b9a\u4e49\u8868\u7684\u7ed3\u6784\uff0c\u5b8c\u6210\u4e0b\u9762\u95ee\u9898\n<ol>\n<li>\u5b9e\u73b0\u67e5\u8be2, \u5e76\u4e14\u53ef\u4ee5\u52fe\u9009\u590d\u9009\u6846\u7b5b\u9009, \u53ea\u770b\u9ad8\u94c1, \u53ea\u770b\u52a8\u8f66, \u53ea\u770b\u6709\u7968<\/li>\n<li>\u6839\u636e\u8017\u65f6, \u7968\u4ef7\u7b49\u591a\u4e2a\u6307\u6807\u6392\u5e8f<\/li>\n<li>\u70b9\u51fb\u7b5b\u9009, \u51fa\u6765\u4e00\u4e2a\u590d\u6742\u7b5b\u9009\u754c\u9762<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<blockquote>\n<p>\u592a\u5f00\u653e\u4e86, \u4ece\u4e24\u4e2a\u89d2\u5ea6\u8bf4, \u4e00\u4e2aSQL, \u53ef\u4ee5\u591a\u91cd\u5b50\u67e5\u8be2\u5b9e\u73b0, String\u62fc\u63a5SQL\u8bed\u53e5; \u4e00\u4e2aGUI, \u8bf4\u4e86\u4e00\u4e0b\u53ef\u4ee5\u7528java.swing, java.awt.Event, \u90fd\u662f\u8fd9\u8bfe\u5b66\u7684, \u4e0d\u80fd\u767d\u80cc\u4e86, \u786c\u5199, \u5199\u6ee1, \u624b\u5199\u9178\u4e86, \u4f11\u606f\u4e86\u5341\u51e0\u5206\u949f, \u53c8\u5199\u4e86\u51e0\u5206\u949f, \u624b\u53c8\u5f00\u59cb\u75bc, \u6ca1\u8010\u5fc3\u4e86, \u63d0\u524d\u4ea4\u5377<\/p>\n<\/blockquote>\n<p>\u8003\u9898\u603b\u7ed3\u81ea\u6d1b\u6a31, \u5f15\u7528\u51e0\u4e2a\u53c2\u8003\u8d44\u6599<\/p>\n<p>\u4eca\u5e74\u70ed\u4e4e\u7684<\/p>\n<ul>\n<li><a href=\"https:\/\/blog.csdn.net\/m0_73704463\/article\/details\/144806264\">https:\/\/blog.csdn.net\/m0_73704463\/article\/details\/144806264<\/a><\/li>\n<\/ul>\n<p>\u5f80\u5e74\u7684<\/p>\n<ul>\n<li><a href=\"https:\/\/blog.csdn.net\/kqwangxi\/article\/details\/122232917\">https:\/\/blog.csdn.net\/kqwangxi\/article\/details\/122232917<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/It_Ray\/article\/details\/135437808\">https:\/\/blog.csdn.net\/It_Ray\/article\/details\/135437808<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/Xing-hui\/db2\">https:\/\/github.com\/Xing-hui\/db2<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/syladmit985350\/\">https:\/\/github.com\/syladmit985350\/<\/a>&#8211;<\/li>\n<\/ul>\n<p>\u5f80\u5e74\u8003\u7684\u5f88\u516b\u80a1, \u90fd\u662f\u62ff\u540d\u8bcd\u76f4\u63a5\u95ee, \u4e0d\u77e5\u9053\u4eca\u5e74\u62bd\u4ec0\u4e48\u98ce, \u8003\u7684\u4e1c\u897f\u548cJava6, db2\u90fd\u6ca1\u5173\u7cfb, \u51fa\u5f97\u9898\u5f88\u5f00\u653e, \u9700\u8981\u7406\u89e3, \u81ea\u5df1\u5206\u6790\u8981\u7528\u4ec0\u4e48\u65b9\u6cd5<\/p>\n<p>\u5f80\u5e74\u8003\u70b9\u603b\u7ed3\u662f\u8fd9\u4e9b<\/p>\n<ul>\n<li>db2\u8fde\u63a5+\u52a0\u8f7d\u542f\u52a8, \u5fc5\u8003<\/li>\n<li>\u5173\u95ed\u8fde\u63a5\u6b21\u5e8f, \u5e38\u8003<\/li>\n<li>pstmt\u6279\u5904\u7406, \u5e38\u8003<\/li>\n<li>wasNull, setNull, \u5fc5\u8003<\/li>\n<li>\u6e38\u6807\u53c2\u6570, \u5012\u53d9, \u8003\u8fc7<\/li>\n<li>FOR UPDATE + WHERE CURRENT OF, \u5fc5\u8003<\/li>\n<li>CLOB\/BLOB, \u5e38\u8003<\/li>\n<li>GUI, \u8003\u8fc7, \u5c31\u95ee\u7528\u5230\u54ea\u4e9b\u5e93\u4e86, \u6ca1\u95ee\u5177\u4f53\u5b9e\u73b0, \u4f30\u8ba1\u8001\u5e08\u81ea\u5df1\u4e5f\u4e0d\u4f1a\u5b9e\u73b0<\/li>\n<li>\u9519\u8bef\u5904\u7406, \u5e38\u8003<\/li>\n<\/ul>\n<h2>\u7b80\u8981\u603b\u7ed3<\/h2>\n<p>\u5e7d\u9ed8\u8bfe, XP+db2+Java6, \u517b\u86ca, \u603b\u62ff\u4e2a\u522b\u94f6\u884c\u8fd8\u5728\u7528\u8fd9\u5957\u7cfb\u7edf\u8bf4\u4e8b, \u5176\u5b9e\u5c31\u662f\u9057\u7559\u95ee\u9898<\/p>\n<p>\u6210\u7ee950%\u62a5\u544a, 50%\u671f\u672b\u7b14\u8bd5, \u8bf4\u662f\u8fd9\u4e48\u8bf4, \u6211\u611f\u89c9\u62a5\u544a\u4f1a\u53c2\u8003\u7b14\u8bd5\u5206\u6570<\/p>\n<p>\u4e0a\u8bfe\u6709\u7b7e\u5230, \u5fc5\u987b\u53bb<\/p>\n<p>\u5185\u5bb9\u5176\u5b9e\u4e0d\u7b97\u591a, \u6211\u5b66\u4e861\u5929, \u505a\u4e86\u524d\u51e0\u4e2a\u5b9e\u9a8c, \u770bPPT\u603b\u7ed3\u4e00\u4e0b, \u7136\u540e\u603b\u7ed3\u5386\u5e74\u9898, \u75af\u72c2\u9ed8\u5199\u4e0a\u9762\u603b\u7ed3\u8003\u70b9\u7684\u4ee3\u7801, \u4e0d\u80fd\u8bf4\u6ca1\u7528, \u4f46\u8fd8\u662f\u88ab\u8003\u8bd5\u9898\u5413\u4e86\u4e00\u8df3<\/p>\n<p>\u5b9e\u9a8c\u505a\u7684\u4eba\u60f3\u6b7b, \u975e\u5f97\u6298\u817e\u90a3XP\u865a\u62df\u673a, macos\u6ca1\u6709\u539f\u751f\u652f\u6301\u7684Java6, \u6700\u540e\u67e5\u624b\u518c\u4e0b\u4e86\u4e2aJava8(Java8\u90fd\u5f88\u96be\u4e0b\u4e86, \u592a\u8001\u4e86). <\/p>\n<p>db2\u7528\u7684\u9a71\u52a8\u8001\u6389\u7259, GPT\u8868\u793a\u8fd9\u4e2a\u7248\u672c\u65e9\u5c31\u7ed3\u675f\u751f\u547d\u5468\u671f\u4e86, \u786c\u505a, \u524d\u51e0\u4e2a\u5b9e\u9a8c\u4e0d\u8ba9\u7528\u5916\u90e8(\u5bbf\u4e3b\u673a)\u7684IDE, \u5efa\u8bae\u5927\u4f19\u76f4\u63a5\u628a\u865a\u62df\u673a\u6865\u63a5\u51fa\u6765, \u5728\u5916\u9762\u8fde\u4e0a\u6570\u636e\u5e93, IDE\u6846\u6846\u5199, \u6d4b\u6ca1\u95ee\u9898\u4e86\u6587\u4ef6\u4f20\u865a\u62df\u673a\u91cc\u9762, \u518d\u88c5\u6a21\u4f5c\u6837\u7684javac, java<\/p>\n<h2>\u521d\u59cb\u5316\u6570\u636e\u5e93\u53c2\u6570\u4e0eURL<\/h2>\n<pre><code class=\"language-java\">\/\/ \u6570\u636e\u5e93\u8fde\u63a5\u53c2\u6570\nString host = &quot;10.67.4.163&quot;;\nString port = &quot;50000&quot;;\nString dbName = &quot;sample&quot;;\nString user = agrs[0];\nString pswd = args[1];\n\/\/ JDBC \u8fde\u63a5\u5b57\u7b26\u4e32\nString url = &quot;jdbc:db2:\/\/&quot; + host + &quot;:&quot; + port + &quot;\/&quot; + dbName;<\/code><\/pre>\n<h3>\u5178\u578b\u7684URL<\/h3>\n<p>\u8981\u8fde\u63a5\u8fdc\u7aef\u7684\u6570\u636e\u5e93<\/p>\n<pre><code>jdbc:db2:\/\/&lt;host&gt;:&lt;port&gt;\/&lt;dbName&gt;<\/code><\/pre>\n<p>\u8981\u8fde\u63a5\u672c\u5730\u7684\u6570\u636e\u5e93, \u53ef\u4ee5\u7b80\u5199\u4e3a<\/p>\n<pre><code>jdbc:db2:&lt;dbName&gt;<\/code><\/pre>\n<h2>\u52a0\u8f7d\u6570\u636e\u5e93\u8fde\u63a5<\/h2>\n<pre><code class=\"language-java\">\/\/ \u521d\u59cb\u5316\u6570\u636e\u5e93\u8fde\u63a5\nConnection connection = null;\ntry {\n    \/\/ \u52a0\u8f7d DB2 JDBC \u9a71\u52a8\u7a0b\u5e8f\n    Class.forName(&quot;com.ibm.db2.jcc.DB2Driver&quot;);\n    \/\/ \u521b\u5efa\u8fde\u63a5\n    System.out.println(&quot;Connecting to the database...&quot;);\n    connection = DriverManager.getConnection(url, user, pswd); \/\/ \u6b7b\u8bb0\u786c\u80cc\u8fd9\u4e00\u884c\n    System.out.println(&quot;Connection successful!&quot;);\n} catch (ClassNotFoundException e) {\n    System.err.println(&quot;DB2 Driver not found. Ensure the DB2 JDBC driver is in the classpath.&quot;);\n    e.printStackTrace();\n} catch (SQLException e) {\n    System.err.println(&quot;Failed to connect to the database. Check the connection parameters.&quot;);\n    e.printStackTrace();\n}<\/code><\/pre>\n<h2>\u7b80\u5355\u67e5\u8be2: Statement\u4e0eResultSet<\/h2>\n<pre><code class=\"language-java\">try {\n    Statement stmt = connection.createStatement();\n    \/\/ ResultSet \u5377\u5b50\u91cc\u90fd\u53eb\u7ed3\u679c\u96c6\n    ResultSet rs = stmt.executeQuery(&quot;SELECT * FROM TEMPL&quot;);\n    \/\/ \u4e00\u6b21\u67e5\u8be2\u540e, rs\u4f4d\u4e8e\u7ed3\u679c\u7684\u524d\u4e00\u4e2a\u884c, \u5fc5\u987bnext\u4e00\u6b21, \u624d\u6307\u5411\u6570\u636e\n    while (rs.next()) {\n        System.out.println(&quot;empno = &quot; + rs.getString(1) + &quot;lastname = &quot; + rs.getString(2));\n    }\n\n} catch (SQLException e) {\n    e.printStackTrace();\n}<\/code><\/pre>\n<p><strong>Statement<\/strong>: \u7528\u4e8e\u5411\u6570\u636e\u5e93\u53d1\u9001 SQL \u8bed\u53e5\u7684\u63a5\u53e3\uff0c\u7528\u4e8e\u6267\u884c\u9759\u6001 SQL \u67e5\u8be2\uff08\u5982 <code>SELECT<\/code>\u3001<code>INSERT<\/code>\u3001<code>UPDATE<\/code> \u7b49\uff09\u3002\u5b83\u662f\u6700\u57fa\u7840\u7684\u63a5\u53e3\uff0c\u9002\u5408\u6267\u884c\u7b80\u5355\u7684 SQL \u8bed\u53e5<\/p>\n<ul>\n<li><code>executeQuery(String sql)<\/code>, \u6267\u884c\u67e5\u8be2, \u8fd4\u56de\u7ed3\u679c\u96c6\u5bf9\u8c61<\/li>\n<li><code>executeUpdate(String sql)<\/code>, \u6267\u884c\u66f4\u65b0\u8bed\u53e5, \u8fd4\u56de\u53d7\u5f71\u54cd\u884c\u6570<\/li>\n<li><code>execute(String sql)<\/code>, \u4efb\u610fSQL\u8bed\u53e5, \u8fd4\u56deboolean, <code>true<\/code>\u4ee3\u8868\u67e5\u8be2,<code>false<\/code>\u4ee3\u8868\u66f4\u65b0<\/li>\n<\/ul>\n<p><strong>ResultSet: <\/strong>\u7528\u6765\u5b58\u50a8\u67e5\u8be2\u7ed3\u679c\u7684\u5bf9\u8c61\uff0c\u4e00\u4e2a\u6307\u5411\u884c\u7684\u6307\u9488, \u4f7f\u4f60\u53ef\u4ee5\u9010\u884c\u8bfb\u53d6\u67e5\u8be2\u8fd4\u56de\u7684\u6570\u636e\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u83b7\u53d6\u5217\u503c\uff0c\u5e76\u652f\u6301\u901a\u8fc7\u5217\u540d\u6216\u5217\u7d22\u5f15\u8bbf\u95ee\u6570\u636e<\/p>\n<ul>\n<li><code>next()<\/code>, \u79fb\u52a8\u5230\u4e0b\u4e00\u884c\uff0c\u8fd4\u56de <code>true<\/code> \u5982\u679c\u5b58\u5728\u4e0b\u4e00\u884c, \u5426\u5219\u8fd4\u56de<code>false<\/code>, \u8fd9\u662f\u7528\u6765\u5224\u65ad\u6570\u636e\u4f4d\u7f6e\u7684\u6838\u5fc3\u65b9\u6cd5<\/li>\n<li><code>getInt(String columnLabel)<\/code> , \u83b7\u53d6\u67d0\u4e00\u5217\u7684\u6574\u6570\u503c<\/li>\n<li><code>getString(String columnLabel)<\/code> , \u83b7\u53d6\u67d0\u4e00\u5217\u7684\u5b57\u7b26\u4e32\u503c<\/li>\n<li><code>getDate(String columnLabel)<\/code> , \u83b7\u53d6\u67d0\u4e00\u5217\u7684\u65e5\u671f\u503c<\/li>\n<li><code>close()<\/code>\u5173\u95ed <code>ResultSet<\/code>\uff0c\u91ca\u653e\u8d44\u6e90<\/li>\n<\/ul>\n<h2>\u5173\u95ed\u8fde\u63a5\u7684\u6b21\u5e8f<\/h2>\n<pre><code class=\"language-java\">rs.close();       \/\/ \u5173\u95ed ResultSet\nstmt.close();     \/\/ \u5173\u95ed Statement\nconnection.close(); \/\/ \u5173\u95ed Connection<\/code><\/pre>\n<p>ResultSet\u4f9d\u8d56\u4e8eStatement, \u800cConnection\u662f\u6838\u5fc3\u90e8\u4ef6, \u5e94\u8be5\u4ee5\u4e0a\u9762\u7684\u6b21\u5e8f\u5173\u95ed\u8fde\u63a5, \u5426\u5219\u53ef\u80fd\u4f1a\u51fa\u73b0\u957f\u65f6\u95f4\u5360\u7528\u8d44\u6e90<\/p>\n<blockquote>\n<p>\u6ce8\u610f: \u5173\u95ed\u8fde\u63a5\u5e76\u4e0d\u4ee3\u8868\u4fee\u6539\u5df2\u7ecf\u63d0\u4ea4\u4e86<\/p>\n<\/blockquote>\n<h2>PreparedSatement<\/h2>\n<p>JDBC \u4e2d\u7528\u4e8e\u6267\u884c <strong>\u9884\u7f16\u8bd1 SQL \u8bed\u53e5<\/strong> \u7684\u63a5\u53e3\uff0c\u7ee7\u627f\u81ea <code>Statement<\/code>\u3002\u5b83\u662f <code>Statement<\/code> \u7684\u589e\u5f3a\u7248\u672c\uff0c\u4e13\u95e8\u7528\u4e8e\u5904\u7406\u52a8\u6001\u53c2\u6570\u5316\u7684 SQL \u67e5\u8be2\u3002<\/p>\n<p>pstmt\u7279\u6027: \u9884\u7f16\u8bd1\u6548\u7387\u66f4\u9ad8, \u652f\u6301\u52a8\u6001\u53c2\u6570, \u9632\u6b62SQL\u6ce8\u5165<\/p>\n<h3>pstmt\u67e5\u8be2\u6570\u636e<\/h3>\n<pre><code class=\"language-java\">String sql = &quot;SELECT * FROM employees WHERE department_id = ? AND salary &gt; ?&quot;;\n\/\/ \u6ce8\u610f\u4e0b\u9762\u8fd9\u91cc, pstmt\u548cstmt\u4e0d\u592a\u4e00\u6837, \u521b\u5efa\u65f6\u5019\u5c31\u586b\u5165sql\u8bed\u53e5\u4e86(\u9884\u7f16\u8bd1\u7684)\nPreparedStatement pstmt = connection.prepareStatement(sql);\n\n\/\/ \u8bbe\u7f6e\u53c2\u6570\npstmt.setInt(1, 10);        \/\/ \u7b2c\u4e00\u4e2a\u53c2\u6570\uff1adepartment_id = 10\npstmt.setDouble(2, 5000.0); \/\/ \u7b2c\u4e8c\u4e2a\u53c2\u6570\uff1asalary &gt; 5000.0\n\n\/\/ \u6267\u884c\u67e5\u8be2\nResultSet rs = pstmt.executeQuery();\nwhile (rs.next()) {\n    System.out.println(&quot;ID: &quot; + rs.getInt(&quot;id&quot;));\n    System.out.println(&quot;Name: &quot; + rs.getString(&quot;name&quot;));\n}<\/code><\/pre>\n<h3>pstmt\u63d2\u5165\u6570\u636e<\/h3>\n<pre><code class=\"language-java\">\/\/ \u521b\u5efa PreparedStatement\nString sql = &quot;INSERT INTO employees (name, department_id, salary) VALUES (?, ?, ?)&quot;;\nPreparedStatement pstmt = connection.prepareStatement(sql);\n\n\/\/ \u8bbe\u7f6e\u53c2\u6570\npstmt.setString(1, &quot;Alice&quot;);    \/\/ \u7b2c\u4e00\u4e2a\u53c2\u6570\uff1aname = &#039;Alice&#039;\npstmt.setInt(2, 5);             \/\/ \u7b2c\u4e8c\u4e2a\u53c2\u6570\uff1adepartment_id = 5\npstmt.setDouble(3, 7000.0);     \/\/ \u7b2c\u4e09\u4e2a\u53c2\u6570\uff1asalary = 7000.0\n\n\/\/ \u6267\u884c\u66f4\u65b0\nint rowsInserted = pstmt.executeUpdate();\nSystem.out.println(&quot;Rows inserted: &quot; + rowsInserted);<\/code><\/pre>\n<h3>pstmt\u6279\u91cf\u64cd\u4f5c: Batch<\/h3>\n<pre><code class=\"language-java\">\/\/ pstmt\u975e\u5e38\u9002\u5408\u6279\u91cf\u64cd\u4f5c\nString sql = &quot;INSERT INTO employees (name, department_id, salary) VALUES (?, ?, ?)&quot;;\nPreparedStatement pstmt = connection.prepareStatement(sql);\n\n\/\/ \u6dfb\u52a0\u591a\u6761\u8bb0\u5f55\npstmt.setString(1, &quot;Alice&quot;);\npstmt.setInt(2, 1);\npstmt.setDouble(3, 5000.0);\npstmt.addBatch(); \/\/ \u52a0\u5165\u6279\u5904\u7406\n\npstmt.setString(1, &quot;Bob&quot;);\npstmt.setInt(2, 2);\npstmt.setDouble(3, 6000.0);\npstmt.addBatch(); \/\/ \u52a0\u5165\u6279\u5904\u7406\n\n\/\/ \u6267\u884c\u6279\u91cf\u64cd\u4f5c\nint[] results = pstmt.executeBatch();\n\nSystem.out.println(&quot;Rows affected: &quot; + Arrays.toString(results));<\/code><\/pre>\n<h3>\u901a\u8fc7\u5b50\u67e5\u8be2\u63d2\u5165<\/h3>\n<pre><code class=\"language-java\">\/\/ \u5b9a\u4e49\u63d2\u5165\u8bed\u53e5\u7684 SQL\nString sqlInsert = &quot;INSERT INTO TEMPL (EMPNO, FIRSTNAME, MIDNAME, LASTNAME) &quot;;\n\n\/\/ \u5b9a\u4e49\u5b50\u67e5\u8be2\u8bed\u53e5\u7684 SQL\nString selectQuery = &quot;SELECT EMPNO, FIRSTNME, MIDNAME, LASTNAME FROM JLU.EMPLOYEE WHERE ? = ?&quot;;\n\n\/\/ \u5408\u5e76\u63d2\u5165\u8bed\u53e5\u548c\u5b50\u67e5\u8be2\u8bed\u53e5\nString findInsert = &quot;INSERT INTO TEMPL (EMPNO, FIRSTNME, LASTNAME, EDLEVEL) &quot; +\n                    &quot;SELECT EMPNO, FIRSTNME, LASTNAME, EDLEVEL FROM EMPLOYEE &quot; +\n                    &quot;WHERE EMPNO = ? AND FIRSTNME = ? AND LASTNAME = ? AND EDLEVEL = ?&quot;;\n\n\/\/ \u51c6\u5907 SQL \u8bed\u53e5\nPreparedStatement pstmt = connection.prepareStatement(findInsert);\n\n\/\/ \u8bbe\u7f6e\u5360\u4f4d\u7b26\u7684\u503c\npstmt.setString(1, &quot;123&quot;);  \/\/ \u66ff\u6362 EMPNO \u7684\u5360\u4f4d\u7b26\npstmt.setString(2, &quot;John&quot;); \/\/ \u66ff\u6362 FIRSTNME \u7684\u5360\u4f4d\u7b26\npstmt.setString(3, &quot;Doe&quot;);  \/\/ \u66ff\u6362 LASTNAME \u7684\u5360\u4f4d\u7b26\npstmt.setInt(4, 5);         \/\/ \u66ff\u6362 EDLEVEL \u7684\u5360\u4f4d\u7b26\n\n\/\/ \u6267\u884c\u63d2\u5165\u64cd\u4f5c\nint rowsAffected = pstmt.executeUpdate();\nSystem.out.println(&quot;Rows inserted: &quot; + rowsAffected);<\/code><\/pre>\n<h2>\u4e3b\u53d8\u91cf\u4e0e\u5217\u53d8\u91cf<\/h2>\n<ul>\n<li>\u4e3b\u53d8\u91cf: C\/Java\u7b49\u7a0b\u5e8f\u4e2d\u5b9a\u4e49, \u7528\u4ee5\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u6570\u636e\u7684\u53d8\u91cf<\/li>\n<li>\u5217\u53d8\u91cf: \u6570\u636e\u5e93\u5b9e\u4f53\u7684\u5217, \u5b58\u653e\u5728\u78c1\u76d8\u4e2d<\/li>\n<\/ul>\n<p>\u4e0b\u9762\u7684SQL\u8bed\u53e5\u4e2d, <code>EMP_ID<\/code>, <code>EMP_NAME<\/code>, <code>EMP_SALARY<\/code> \u90fd\u662f\u5217\u53d8\u91cf<\/p>\n<pre><code class=\"language-sql\">CREATE TABLE EMPLOYEE (\n    EMP_ID INTEGER,\n    EMP_NAME VARCHAR(50),\n    EMP_SALARY DECIMAL(10, 2)\n);<\/code><\/pre>\n<p>\u4e0b\u9762\u662fDCLGE\u5de5\u5177\u4e3a\u8be5\u8868\u751f\u6210\u7684\u7c7b, \u7c7b\u5185\u5c01\u88c5\u4e86\u4e3b\u53d8\u91cf<\/p>\n<pre><code class=\"language-java\">\/\/ DCLGEN Output for Table: EMPLOYEE\npublic class Employee {\n    public int empId;               \/\/ \u5bf9\u5e94 EMP_ID\n    public String empName;          \/\/ \u5bf9\u5e94 EMP_NAME\n    public java.math.BigDecimal empSalary; \/\/ \u5bf9\u5e94 EMP_SALARY\n    \/\/ \u6784\u9020\u51fd\u6570\n    public Employee() {}\n    \/\/ \u53ef\u9009\uff1a\u63d0\u4f9b getter \u548c setter \u65b9\u6cd5\n    public int getEmpId() {\n        return empId;\n    }\n    public void setEmpId(int empId) {\n        this.empId = empId;\n    }\n        ...\n}<\/code><\/pre>\n<blockquote>\n<p>DCLGE\u5de5\u5177\u7528\u4ee5\u81ea\u52a8\u5316\u7684\u4e3a\u4e00\u4e2a\u6570\u636e\u5e93\u7684\u8868\u751f\u6210\u5bf9\u5e94\u4e3b\u673a\u53d8\u91cf\u7684\u58f0\u660e, \u4ee5\u4e0b\u662f\u4e3a\u4e0a\u9762Employee\u8868\u751f\u6210\u7684\u547d\u4ee4<\/p>\n<p><code>db2dclgn -D sample -T EMPLOYEE -L Java -F Employee.java<\/code><\/p>\n<\/blockquote>\n<h2>\u5173\u4e8eNULL\u7684\u8ba8\u8bba<\/h2>\n<h3>wasNull()<\/h3>\n<p>\u6bcf\u79cdJDBC\u53d8\u91cf\u5728\u6570\u636e\u5e93\u4e3aNULL\u65f6\u90fd\u6709\u4e00\u4e2a\u9ed8\u8ba4\u7684\u8fd4\u56de\u503c, \u56e0\u6b64\u76f4\u63a5\u5c06get\u5230\u7684\u4e3b\u673a\u53d8\u91cf\u4e0eNULL\u505a\u6bd4\u8f83\u662f\u4e0d\u53ef\u9760\u7684<\/p>\n<p>\u8003\u8651\u4e0b\u9762\u7684\u4ee3\u7801<\/p>\n<pre><code class=\"language-java\">String mgrno = rs.getString(1);\nif (mogrno == null) {\n    ... \/\/ \u4f5c\u51fa\u4e00\u4e9b\u5904\u7406\n}<\/code><\/pre>\n<p>\u8fd9\u79cd\u505a\u6cd5\u662f\u4e0d\u53ef\u9760\u7684, \u6bd4\u5982<code>getInt()<\/code>, <code>getShort()<\/code>\u4f1a\u5728NULL\u65f6\u8fd4\u56de0, \u6b64\u65f6\u65e0\u6cd5\u5f97\u77e5\u8be5\u6570\u636e\u662fNULL\u8fd8\u662f0<\/p>\n<p>\u6211\u4eec\u5e94\u8be5\u7528<code>wasNull()<\/code>\u6765\u68c0\u67e5\u4e0a\u4e00\u6b21getXXX\u7684\u7ed3\u679c\u662f\u4e0d\u662fNULL<\/p>\n<pre><code class=\"language-java\">\/\/ \u904d\u5386\u7ed3\u679c\u96c6\nwhile (resultSet.next()) {\n    \/\/ \u83b7\u53d6 name \u5217\u7684\u503c\n    String name = resultSet.getString(&quot;name&quot;);\n    \/\/ \u68c0\u6d4b name \u662f\u5426\u4e3a NULL\n    if (resultSet.wasNull()) {\n        System.out.println(&quot;name \u5217\u503c\u4e3a NULL&quot;);\n    } else {\n        System.out.println(&quot;name: &quot; + name);\n    }\n    \/\/ \u83b7\u53d6 age \u5217\u7684\u503c\n    int age = resultSet.getInt(&quot;age&quot;);\n    \/\/ \u68c0\u6d4b age \u662f\u5426\u4e3a NULL\n    if (resultSet.wasNull()) {\n        System.out.println(&quot;age \u5217\u503c\u4e3a NULL&quot;);\n    } else {\n        System.out.println(&quot;age: &quot; + age);\n    }\n    System.out.println(&quot;------------------------------&quot;);\n}<\/code><\/pre>\n<h3>setNull()<\/h3>\n<p>\u8003\u8651\u8fd9\u6837\u7684\u4ee3\u7801<\/p>\n<pre><code class=\"language-java\">PreparedStatement stmt = null;\n\/\/ \u8981\u5c06\u7f16\u53f7\u4e3a000110\u7684\u5458\u5de5\u7535\u8bdd\u53f7\u7801\u8bbe\u7f6e\u4e3a\u7a7a\nsql = &quot;UPDATE TEMPL SET PHONENO = ? &quot; + &quot;WHERE EMPNO = &#039;000110&#039; &quot;;\nstmt = con.prepareStatement(sql);\nif (...) {\n        stmt.setString(1, null); \/\/ \u8fd9\u6837\u505a\u4e0d\u597d!\n} else {\n    ...\n}\nupdateCount = stmt.executeUpdate();<\/code><\/pre>\n<p>JDBC \u65e0\u6cd5\u786e\u5b9a\u53c2\u6570\u7684 SQL \u6570\u636e\u7c7b\u578b<br \/>\n\u4e0a\u9762\u7684\u505a\u6cd5\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u5e93\u629b\u51fa\u9519\u8bef\uff0c\u6216\u8005\u65e0\u6cd5\u6b63\u786e\u5730\u5c06\u53c2\u6570\u6807\u8bb0\u4e3a SQL \u7684 <code>NULL<\/code> \u503c\u3002\u4f8b\u5982\uff0c\u6570\u636e\u5e93\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u5c06 <code>null<\/code> \u89e3\u6790\u4e3a <code>VARCHAR<\/code> \u7c7b\u578b\uff0c\u4f46\u5982\u679c\u6570\u636e\u5e93\u5b57\u6bb5\u662f\u5176\u4ed6\u7c7b\u578b\uff08\u5982 <code>INTEGER<\/code> \u6216 <code>DATE<\/code>\uff09\uff0c\u5c31\u4f1a\u5bfc\u81f4\u7c7b\u578b\u4e0d\u5339\u914d\u9519\u8bef<\/p>\n<p>\u5728\u9700\u8981\u8bbe\u7f6eNULL\u65f6\u5019, \u6211\u4eec\u5e94\u8be5\u7528JDBC\u63d0\u4f9b\u7684<code>setNull()<\/code>\u65b9\u6cd5<\/p>\n<pre><code class=\"language-java\">if (some condition) {\n    stmt.setNull(1, java.sql.Types.VARCHAR); \/\/ \u660e\u786e\u6307\u5b9a\u4e3a SQL NULL\uff0c\u5e76\u660e\u786e\u7c7b\u578b\u4e3a VARCHAR (\u8fd9\u4e00\u5217\u7684\u7c7b\u578b)\n} else {\n    stmt.setString(1, newphone); \/\/ \u6b63\u5e38\u8bbe\u7f6e\u5b57\u7b26\u4e32\u503c\n}\nupdateCount = stmt.executeUpdate();<\/code><\/pre>\n<h2>SQLCA<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/pic.cirno.fun\/sssn-blog-pics\/image-20241228174540443.png\" alt=\"image-20241228174540443\" style=\"zoom:33%;\" \/><\/p>\n<p><strong>SQLCA<\/strong>\uff08SQL Communication Area\uff09\u662f SQL \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e\u5b58\u50a8 SQL \u64cd\u4f5c\u6267\u884c\u540e\u7684\u72b6\u6001\u4fe1\u606f\u3002\u5b83\u662f\u6570\u636e\u5e93\u7cfb\u7edf\u4e3a\u4e86\u5e2e\u52a9\u7a0b\u5e8f\u5904\u7406 SQL \u8bed\u53e5\u6267\u884c\u7ed3\u679c\u800c\u8bbe\u8ba1\u7684\u4e00\u4e2a\u6807\u51c6\u5316\u7ed3\u6784<\/p>\n<p>SQLCA\u7684\u6838\u5fc3\u4fe1\u606f\u6709: SQLCODE, SQLSTATE, SQLERRM, SQLWARN <\/p>\n<pre><code class=\"language-java\">\/\/ SQLExpection\u901a\u5e38\u662f\u81f4\u547d\u7684\nSQLException.getSQLState();\nSQLException.getErrorCode();\nSQLException.getMessage(); \n\/\/ SQLWARN\u662f\u975e\u81f4\u547d\u7684\nDriverManager.getConnection.getWarn(); <\/code><\/pre>\n<p>SQLCA \u63d0\u4f9b\u7684\u4fe1\u606f\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>SQL \u64cd\u4f5c\u662f\u5426\u6210\u529f, \u5f71\u54cd\u7684\u884c\u6570, \u9519\u8bef\u4ee3\u7801\u548c\u9519\u8bef\u4fe1\u606f(\u5982\u679c\u6709), \u8b66\u544a\u72b6\u6001<\/li>\n<\/ul>\n<p>Java\u4e2d\u6ca1\u6709\u76f4\u63a5\u7684SQLCA\u6570\u636e\u7ed3\u6784, \u4f46\u662f\u53ef\u4ee5\u901a\u8fc7JDBC\u7684<code>sqlcode<\/code>\u548c<code>sqlerrm<\/code>\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c<\/p>\n<pre><code class=\"language-java\">try {\n    \/\/ 1. \u5efa\u7acb\u8fde\u63a5\n    conn = DriverManager.getConnection(jdbcUrl, username, password);\n    \/\/ 2. \u6267\u884c\u67e5\u8be2\n    String sql = &quot;SELECT * FROM employees&quot;;\n    pstmt = conn.prepareStatement(sql);\n    rs = pstmt.executeQuery();\n    \/\/ 3. \u5904\u7406\u67e5\u8be2\u7ed3\u679c\n    while (rs.next()) {\n        System.out.println(&quot;\u5458\u5de5ID: &quot; + rs.getInt(&quot;id&quot;) + &quot;, \u59d3\u540d: &quot; + rs.getString(&quot;name&quot;));\n    }\n    \/\/ 4. \u68c0\u67e5 SQL \u8b66\u544a\n    SQLWarning warning = conn.getWarnings();\n    while (warning != null) {\n        System.out.println(&quot;SQL \u8b66\u544a: &quot; + warning.getMessage());\n        warning = warning.getNextWarning();\n    }\n} catch (SQLException e) {\n    \/\/ \u5904\u7406 SQL \u9519\u8bef\uff0c\u7c7b\u4f3c SQLCA \u4e2d\u7684 sqlcode \u548c sqlerrm\n    System.err.println(&quot;SQL \u9519\u8bef\u4ee3\u7801: &quot; + e.getErrorCode());\n    System.err.println(&quot;SQL \u72b6\u6001: &quot; + e.getSQLState());\n    System.err.println(&quot;\u9519\u8bef\u6d88\u606f: &quot; + e.getMessage());\n    \/\/ \u4e0a\u9762\u8fd9\u4e9bget\u65b9\u6cd5, \u90fd\u8fd4\u56deString, \u5982\u679c\u60f3\u6bd4\u8f83, \u5219\n    \/\/ if (e.getSQLState().equals(&quot;42818&quot;)) {...}\n} finally {\n    \/\/ 5. \u5173\u95ed\u8d44\u6e90\n    try {\n        if (rs != null) rs.close();\n        if (pstmt != null) pstmt.close();\n        if (conn != null) conn.close();\n    } catch (SQLException e) {\n        System.err.println(&quot;\u5173\u95ed\u8d44\u6e90\u65f6\u51fa\u9519: &quot; + e.getMessage());\n    }\n}<\/code><\/pre>\n<p>\u5982\u679c\u4e0a\u9762\u7684\u4ee3\u7801\u51fa\u9519\u4e86, \u53ef\u80fd\u4f1a\u8f93\u51fa\u5f62\u4f3c\u8fd9\u6837\u7684\u4fe1\u606f<\/p>\n<pre><code>SQL \u9519\u8bef\u4ee3\u7801: 1045\nSQL \u72b6\u6001: 28000\n\u9519\u8bef\u6d88\u606f: Access denied for user &#039;root&#039;@&#039;localhost&#039; (using password: YES)<\/code><\/pre>\n<h3>SQLSTATE \u548c SQLCODE \u7684\u5dee\u5f02<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u7279\u6027<\/th>\n<th><code>SQLSTATE<\/code><\/th>\n<th><code>SQLCODE<\/code><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>\u957f\u5ea6\/\u683c\u5f0f<\/strong><\/td>\n<td>5 \u4e2a\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u4ee3\u7801\uff08\u5982 <code>23000<\/code>\uff09<\/td>\n<td>\u4e00\u4e2a\u6574\u6570\uff08\u5982 -104 \u6216 +100\uff09<\/td>\n<\/tr>\n<tr>\n<td><strong>\u6807\u51c6\u5316<\/strong><\/td>\n<td>\u8de8\u6570\u636e\u5e93\u7cfb\u7edf\u6807\u51c6\u5316\uff0c\u7b26\u5408 SQL \u6807\u51c6<\/td>\n<td>\u6570\u636e\u5e93\u7cfb\u7edf\u4e13\u6709\uff0c\u53ef\u80fd\u56e0\u7cfb\u7edf\u800c\u5f02<\/td>\n<\/tr>\n<tr>\n<td><strong>\u7528\u9014<\/strong><\/td>\n<td>\u66f4\u9002\u5408\u8de8\u5e73\u53f0\u7684\u9519\u8bef\u5904\u7406\u548c\u517c\u5bb9\u6027<\/td>\n<td>\u66f4\u9002\u5408\u7279\u5b9a\u6570\u636e\u5e93\u7cfb\u7edf\u5185\u90e8\u7684\u9519\u8bef\u8c03\u8bd5<\/td>\n<\/tr>\n<tr>\n<td><strong>\u9519\u8bef\u7c7b\u578b<\/strong><\/td>\n<td>\u63d0\u4f9b\u901a\u7528\u7684\u9519\u8bef\u5206\u7c7b\u548c\u5b50\u5206\u7c7b<\/td>\n<td>\u63d0\u4f9b\u66f4\u5177\u4f53\u7684\u9519\u8bef\u7f16\u53f7<\/td>\n<\/tr>\n<tr>\n<td><strong>\u5e38\u89c1\u8303\u56f4<\/strong><\/td>\n<td><code>00000<\/code>\uff08\u6210\u529f\uff09\uff0c<code>01000<\/code>\uff08\u8b66\u544a\uff09\uff0c <code>02000<\/code>\uff08\u672a\u627e\u5230\u6570\u636e\uff09\u7b49<\/td>\n<td><code>0<\/code>\uff08\u6210\u529f\uff09\uff0c<code>+100<\/code>\uff08\u672a\u627e\u5230\u6570\u636e\uff09\uff0c\u8d1f\u6570\u8868\u793a\u9519\u8bef<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>SQLSTATE \u548c SQLCODE\u7684\u5173\u7cfb<\/h3>\n<ul>\n<li><strong>\u6620\u5c04\u5173\u7cfb<\/strong>\uff1a\n<ul>\n<li>\u6bcf\u4e2a <code>SQLSTATE<\/code> \u901a\u5e38\u5bf9\u5e94\u4e00\u4e2a\u6216\u591a\u4e2a <code>SQLCODE<\/code> \u503c<\/li>\n<li>\u4f8b\u5982\uff1a\n<ul>\n<li><code>SQLSTATE<\/code> \u4e3a <code>02000<\/code>\uff08\u672a\u627e\u5230\u6570\u636e\uff09\uff0c\u5bf9\u5e94\u7684 <code>SQLCODE<\/code> \u4e3a <code>+100<\/code><\/li>\n<li><code>SQLSTATE<\/code> \u4e3a <code>23000<\/code>\uff08\u8fdd\u53cd\u5b8c\u6574\u6027\u7ea6\u675f\uff09\uff0c\u53ef\u80fd\u5bf9\u5e94\u591a\u4e2a <code>SQLCODE<\/code>\uff0c\u5982 <code>-803<\/code>\uff08\u4e3b\u952e\u51b2\u7a81\uff09\u6216 <code>-104<\/code>\uff08\u7ea6\u675f\u9519\u8bef\uff09<\/li>\n<\/ul>\n<\/li>\n<li>\u4f46 <code>SQLCODE<\/code> \u662f\u6570\u636e\u5e93\u7cfb\u7edf\u7279\u5b9a\u7684\uff0c\u67d0\u4e9b\u6570\u636e\u5e93\u53ef\u80fd\u5b9a\u4e49\u4e86\u989d\u5916\u7684 <code>SQLCODE<\/code> \u503c\uff0c\u800c\u8fd9\u4e9b\u503c\u6ca1\u6709\u5bf9\u5e94\u7684 <code>SQLSTATE<\/code><\/li>\n<\/ul>\n<\/li>\n<li><strong>\u4f18\u5148\u7ea7<\/strong>\uff1a\n<ul>\n<li>\u5982\u679c\u9700\u8981\u8de8\u6570\u636e\u5e93\u5e73\u53f0\u7684\u9519\u8bef\u5904\u7406\uff0c\u63a8\u8350\u4f7f\u7528 <code>SQLSTATE<\/code><\/li>\n<li>\u5982\u679c\u53ea\u9488\u5bf9\u5355\u4e00\u6570\u636e\u5e93\u7cfb\u7edf\uff0c<code>SQLCODE<\/code> \u901a\u5e38\u63d0\u4f9b\u66f4\u8be6\u7ec6\u7684\u4fe1\u606f<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<blockquote>\n<p>JDBC\u53ea\u652f\u6301\u52a8\u6001SQL\uff0c\u8981\u6c42\u6267\u884c\u7a0b\u5e8f\u7684User\u5fc5\u987b\u62e5\u6709\u6267\u884c\u8fd9\u6761\u8bed\u53e5\u7684\u6743\u9650<\/p>\n<\/blockquote>\n<h2>\u6e38\u6807\u7684\u4e24\u4e2a\u53c2\u6570<\/h2>\n<p>\u5728\u524d\u9762\u7684\u5b9e\u9a8c\u4e2d, \u90fd\u4f7f\u7528\u4e86\u9ed8\u8ba4\u7684\u6e38\u6807, \u8fd9\u79cd\u6e38\u6807\u53ea\u80fd\u5411\u524d\u6eda\u52a8, \u53eb\u505a<code>TYPE_FORWARD_ONLY<\/code>\u7ed3\u679c\u96c6<\/p>\n<p>\u9664\u6b64\u4e4b\u5916, \u8fd8\u6709\u8fd8\u6709 <code>TYPE_SCROLL_INSENSITIVE<\/code> \u548c <code>TYPE_SCROLL_SENSITIVE<\/code> \u7ed3\u679c\u96c6, \u8fd9\u4e24\u79cd\u7ed3\u679c\u96c6\u7684\u6e38\u6807\u90fd\u53ef\u4ee5\u524d\u540e\u6eda\u52a8, \u533a\u522b\u662f\u654f\u611f\u7ed3\u679c\u96c6\u4f1a\u7acb\u5373\u53cd\u5e94\u6570\u636e\u7684\u66f4\u6539\u60c5\u51b5, \u800c\u4e0d\u654f\u611f\u7ed3\u679c\u96c6\u9700\u8981\u624b\u52a8\u5237\u65b0<\/p>\n<p>\u6e38\u6807\u5c5e\u6027\u5e94\u8be5\u8fd9\u6837\u8bbe\u7f6e:<\/p>\n<pre><code class=\"language-java\">ResultSet rs = null;\nPreparedStatement pstmt = con.prepareStatement(\n    &quot;SELECT EMPNO, LASTNAME &quot; +\n    &quot; FROM TEMPL &quot; +\n    &quot; WHERE WORKDEPT = ? &quot;, \n    ResultSet.TYPE_FORWARD_ONLY, \/\/ \u53ea\u80fd\u5411\u524d\n    ResultSet.CONCUR_READ_ONLY   \/\/ \u53ea\u6709\u8bfb\u64cd\u4f5c\u53ef\u4ee5\u5e76\u53d1\n);\npstmt.setString(1, argv[0]);\nwhile (prs.next()) {\n    System.out.println(&quot;empno &quot; + prs.getString(1) +\n                       &quot; lastname &quot; + prs.getString(2));\n}<\/code><\/pre>\n<blockquote>\n<p>\u9664\u4e86 <code>CONCUR_READ_ONLY<\/code> \u5e76\u53d1\u7c7b\u578b(\u8868\u793a\u67e5\u8be2\u4ec5\u4e3a\u53ea\u8bfb\u6a21\u5f0f FETCH ONLY)\u4e4b\u5916\uff0cJDBC 2.0 \u8fd8\u652f\u6301 <code>CONCUR_UPDATABLE<\/code> \u5e76\u53d1\u7c7b\u578b\uff0c\u8be5\u7c7b\u578b\u5141\u8bb8\u5bf9\u67e5\u8be2\u8fdb\u884c FOR UPDATE \u64cd\u4f5c\u3002<\/p>\n<p>\u7136\u800c\uff0c<strong>DB2 \u7684 JDBC 2.0 \u9a71\u52a8\u7a0b\u5e8f\u4e0d\u652f\u6301 <code>CONCUR_UPDATABLE<\/code> \u53c2\u6570\u3002<\/strong><\/p>\n<p>\u5047\u5982\u4f60\u6b63\u5728\u5f00\u53d1\u4e00\u4e2a\u57fa\u4e8e DB2 \u6570\u636e\u5e93\u7684 Java \u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u9700\u8981\u4fee\u6539\u67e5\u8be2\u7ed3\u679c\u96c6\u4e2d\u7684\u6570\u636e\uff0c\u90a3\u4e48\u4f60\u9700\u8981\u627e\u5230\u66ff\u4ee3\u65b9\u6cd5\uff08\u4f8b\u5982\u76f4\u63a5\u6267\u884c <code>UPDATE<\/code> \u8bed\u53e5, \u6216\u8005\u4f7f\u7528\u5b9a\u4f4d\u66f4\u65b0\u6280\u672f\uff09\uff0c\u56e0\u4e3a <code>CONCUR_UPDATABLE<\/code> \u65e0\u6cd5\u4f7f\u7528\u3002<\/p>\n<\/blockquote>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u9ad8\u7ea7\u6e38\u6807\u7684\u4f8b\u5b50<\/p>\n<pre><code class=\"language-java\">\/\/ \u5b9a\u4e49\u4e00\u4e2a\u53ef\u6eda\u52a8\u7684\u7ed3\u679c\u96c6\u5bf9\u8c61\nResultSet scrollrs = null;\nsql = &quot;SELECT&quot; \n    + &quot;    EMP.FIRSTNAME,&quot;   \/\/ \u5458\u5de5\u7684\u540d\u5b57\n    + &quot;    EMP.LASTNAME&quot;     \/\/ \u5458\u5de5\u7684\u59d3\u6c0f\n    + &quot; FROM&quot; \n    + &quot;    EMP&quot;              \/\/ \u4ece EMP \u8868\u4e2d\u67e5\u8be2\n    + &quot; WHERE&quot; \n    + &quot;    (&quot; \n    + &quot;        EMP.WORKDEPT = ?&quot; \/\/ \u6761\u4ef6\uff1a\u5458\u5de5\u7684\u5de5\u4f5c\u90e8\u95e8\n    + &quot;    )&quot;;\n\/\/ \u51c6\u5907 SQL \u8bed\u53e5\nstmt = con.prepareStatement(sql, \n                            scrollrs.TYPE_SCROLL_INSENSITIVE, \n                            scrollrs.CONCUR_READ_ONLY);\n\/\/ \u8bbe\u7f6e\u67e5\u8be2\u6761\u4ef6\u7684\u53c2\u6570\uff0c\u4f7f\u7528\u5360\u4f4d\u7b26 (?) \u586b\u5145\u90e8\u95e8\u7f16\u53f7 (dno)\nstmt.setString(1, dno);\n\/\/ \u6267\u884c\u67e5\u8be2\u5e76\u5c06\u7ed3\u679c\u8d4b\u503c\u7ed9 scrollrs\uff08\u53ef\u6eda\u52a8\u7684\u7ed3\u679c\u96c6\uff09\nscrollrs = stmt.executeQuery();\n\/\/ \u5c06\u5149\u6807\u79fb\u52a8\u5230\u7ed3\u679c\u96c6\u7684\u6700\u540e\u4e00\u884c\u4e4b\u540e\uff0c\u51c6\u5907\u8fdb\u884c\u9006\u5e8f\u904d\u5386\nscrollrs.afterLast();\n\n\/\/ \u4f7f\u7528 while \u5faa\u73af\u4ece\u7ed3\u679c\u96c6\u7684\u6700\u540e\u4e00\u884c\u9010\u884c\u5411\u4e0a\u904d\u5386\nwhile (scrollrs.previous()) {\n    \/\/ \u83b7\u53d6\u5f53\u524d\u884c\u7684\u7b2c\u4e00\u5217\u503c\uff08\u5458\u5de5\u7684\u540d\u5b57\uff09\n    firstname = scrollrs.getString(1);\n    \/\/ \u83b7\u53d6\u5f53\u524d\u884c\u7684\u7b2c\u4e8c\u5217\u503c\uff08\u5458\u5de5\u7684\u59d3\u6c0f\uff09\n    lastname = scrollrs.getString(2);\n}<\/code><\/pre>\n<h2>\u7269\u5316: \u5c06\u7ed3\u679c\u96c6\u6682\u5b58\u5230\u78c1\u76d8\u4e0a<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/pic.cirno.fun\/sssn-blog-pics\/image-20241228184018526.png\" alt=\"image-20241228184018526\" style=\"zoom:33%;\" \/><\/p>\n<p>OPEN NOMAT: \u975e\u7269\u5316, \u4e0d\u6392\u5e8f, \u67e5\u8be2\u7ed3\u679c\u5b58\u50a8\u5728\u5185\u5b58\u4e2d, \u9002\u7528\u4e8e\u5c0f\u6570\u636e<\/p>\n<p>OPEN MAT: \u7269\u5316, \u4e0d\u6392\u5e8f, \u7ed3\u679c\u6682\u5b58\u5230\u6587\u4ef6\u4e2d, \u9002\u7528\u4e8e\u5927\u6570\u636e<\/p>\n<blockquote>\n<p>\u8fd9\u770b\u4e0a\u53bb\u6709\u70b9\u5947\u602a, \u6392\u5e8f\u548c\u5b58\u786c\u76d8\u8fd8\u80fd\u6302\u94a9, \u6211\u60f3\u4e71\u5e8f\u5b58\u786c\u76d8\u4e0d\u884c\u5417?<\/p>\n<\/blockquote>\n<p>\u5728\u4ee3\u7801\u4e0a, \u533a\u522b\u53ea\u5728<code>ORDER BY<\/code>\u4e0a<\/p>\n<p>\u65e0 <code>ORDER BY<\/code>\uff08\u975e\u7269\u5316\uff09\uff1a<\/p>\n<ul>\n<li>\u7ed3\u679c\u96c6\u76f4\u63a5\u4ece\u8868\u4e2d\u83b7\u53d6\uff0c\u4e0d\u9700\u8981\u989d\u5916\u5904\u7406\u3002\u6570\u636e\u5e93\u5f15\u64ce\u53ea\u9700\u9010\u884c\u63d0\u53d6\u6570\u636e\u5e76\u4f20\u9012\u7ed9 JDBC\u3002<\/li>\n<li>\u4e0d\u6d89\u53ca\u4e34\u65f6\u6587\u4ef6\uff0c\u67e5\u8be2\u6027\u80fd\u66f4\u9ad8\u3002<\/li>\n<\/ul>\n<p>\u6709 <code>ORDER BY<\/code>\uff08\u7269\u5316\uff09\uff1a<\/p>\n<ul>\n<li>\u6570\u636e\u5e93\u9700\u8981\u5148\u5c06\u6240\u6709\u67e5\u8be2\u7ed3\u679c\u63d0\u53d6\u51fa\u6765\uff0c\u5e76\u6839\u636e <code>ORDER BY<\/code> \u7684\u89c4\u5219\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<li>\u6392\u5e8f\u901a\u5e38\u9700\u8981\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u4e34\u65f6\u5de5\u4f5c\u6587\u4ef6\uff08<code>WORKFILE<\/code>\uff09\u4e2d\uff0c\u4e4b\u540e\u518d\u5c06\u6392\u5e8f\u540e\u7684\u7ed3\u679c\u8fd4\u56de\u3002<\/li>\n<li>\u8fd9\u79cd\u573a\u666f\u4e0b\uff0c\u6570\u636e\u5e93\u5f15\u64ce\u4f1a\u7269\u5316\u7ed3\u679c\u96c6\uff08\u5373\u5c06\u5176\u5b58\u50a8\u5230\u4e34\u65f6\u6587\u4ef6\u4e2d\uff09\u3002<\/li>\n<\/ul>\n<blockquote>\n<p>\u62d3\u5c55: \u9664\u4e86<code>ORDER BY<\/code>, \u6bd4\u5982\u590d\u6742\u7684<code>JOIN<\/code>, <code>SUM<\/code>, <code>AVG<\/code>, <code>GROUP BY<\/code>, \u5b50\u67e5\u8be2\u4e5f\u4f1a\u89e6\u53d1\u7269\u5316\u64cd\u4f5c<\/p>\n<\/blockquote>\n<h2>\u5b9a\u4f4d\u66f4\u65b0: \u6e38\u6807\u64cd\u4f5c\u7ed3\u679c\u96c6<\/h2>\n<p>\u5b9a\u4f4d\u66f4\u65b0\u662f\u6307\u901a\u8fc7\u6e38\u6807\u64cd\u4f5c\u7ed3\u679c\u96c6\u4e2d\u7684\u5f53\u524d\u884c\u6570\u636e\uff0c\u5e76\u6839\u636e\u4e1a\u52a1\u903b\u8f91\u76f4\u63a5\u66f4\u65b0\u6570\u636e\u5e93\u4e2d\u7684\u5bf9\u5e94\u8bb0\u5f55<\/p>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a<code>WHERE CURRENT OF<\/code> \u540e\u5fc5\u987b\u6307\u5b9a\u6e38\u6807\u540d\u79f0\uff0c\u6e38\u6807\u540d\u79f0\u901a\u8fc7 <code>ResultSet.getCursorName()<\/code> \u83b7\u53d6<\/p>\n<pre><code class=\"language-java\">\/\/ FOR UPDATE: \u8868\u660e\u67e5\u8be2\u7ed3\u679c\u96c6\u4e2d\u7684\u8bb0\u5f55\u662f\u53ef\u4ee5\u88ab\u4fee\u6539\u7684\nString mySelect = &quot;SELECT LASTNAME, FIRSTNME FROM EMP FOR UPDATE&quot;;\n\/\/ WHERE CURRENT OF: \u8868\u793a\u5bf9\u6e38\u6807\u5f53\u524d\u6307\u5411\u7684\u8bb0\u5f55\u8fdb\u884c\u66f4\u65b0\nString myUpdate = &quot;UPDATE EMP SET FIRSTNME = ? WHERE CURRENT OF &quot;;\n\/\/ \u4e0a\u9762\u7684\u5199\u6cd5\u4e0d\u662fSQL\u6807\u51c6, \u4f9d\u8d56\u4e8eJDBC\u5b9e\u73b0\nString cursorName = null;\n\nStatement stmt = con.createStatement();\nResultSet rs = stmt.executeQuery(mySelect);\ncursorName = rs.getCursorName();\nPreparedStatement ps = con.prepareStatement(myUpdate + cursorName); \/\/ \u8fd9\u91cc\u5f88\u91cd\u8981\n\n\/\/ \u904d\u5386mySelect\u7684\u7ed3\u679c\u96c6\nwhile (rs.next()) {\n    String lastname = rs.getString(1);\n    String firstnme = rs.getString(2);\n    if (lastname.equals(&quot;SMITH&quot;)) {\n            String newFirstname = &quot;George&quot;;\n            ps.setString(1, newFirstname); \/\/ \u4e1a\u52a1\u903b\u8f91: \u7528myUpdate+cursorName\u66f4\u65b0\u540d\u5b57\n            ps.executeUpdate();\n    }\n}<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u8fd9\u79cd\u66f4\u65b0 <strong>\u4f1a\u53cd\u6620\u5230\u6570\u636e\u5e93<\/strong>\uff0c\u4e0d\u4ec5\u4ec5\u662f\u5bf9\u7ed3\u679c\u96c6\u8d1f\u8d23<\/p>\n<p>\u4e8b\u5b9e\u4e0a, \u5f53\u6267\u884c\u5230<\/p>\n<pre><code class=\"language-java\">\/\/ String myUpdate = &quot;UPDATE EMP SET FIRSTNME = ? WHERE CURRENT OF &quot;;\n\/\/ PreparedStatement ps = con.prepareStatement(myUpdate + cursorName);\nps.executeUpdate();<\/code><\/pre>\n<p>\u76f8\u5f53\u4e8e\u6267\u884cSQL<\/p>\n<pre><code class=\"language-java\">UPDATE EMP SET FIRSTNME = &#039;George&#039; WHERE CURRENT OF cursor_name;<\/code><\/pre>\n<h2>FOR UPDATE\u7684\u597d\u5904: \u907f\u514d\u6b7b\u9501<\/h2>\n<p>\u590d\u4e60\u4e00\u4e0b\u4e4b\u524d\u5b66\u8fc7\u7684\u9501<\/p>\n<ul>\n<li>\n<p><strong>IS \u9501<\/strong> \u548c <strong>IX \u9501<\/strong> \u662f\u8868\u7ea7\u522b\u7684\u610f\u5411\u9501\uff0c\u7528\u4e8e\u4f18\u5316\u52a0\u9501\u6027\u80fd\uff0c\u51cf\u5c11\u9501\u51b2\u7a81<\/p>\n<\/li>\n<li>\n<p><strong>S \u9501<\/strong> \u7528\u4e8e\u8bfb\u53d6\u6570\u636e\uff0c\u9632\u6b62\u6570\u636e\u88ab\u4fee\u6539<\/p>\n<\/li>\n<li>\n<p><strong>U \u9501<\/strong> \u662f\u66f4\u65b0\u64cd\u4f5c\u7684\u8fc7\u6e21\u9501\uff0c\u907f\u514d\u4e86\u6b7b\u9501<\/p>\n<\/li>\n<li>\n<p><strong>X \u9501<\/strong> \u7528\u4e8e\u4fee\u6539\u6570\u636e\uff0c\u72ec\u5360\u5bf9\u6570\u636e\u7684\u8bbf\u95ee\u6743\u9650<\/p>\n<\/li>\n<\/ul>\n<p>\u9501\u7684\u517c\u5bb9\u6027\u77e9\u9635<\/p>\n<table>\n<thead>\n<tr>\n<th>\u9501\u7c7b\u578b<\/th>\n<th>IS<\/th>\n<th>IX<\/th>\n<th>S<\/th>\n<th>U<\/th>\n<th>X<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>IS<\/strong><\/td>\n<td>\u2714\ufe0f<\/td>\n<td>\u2714\ufe0f<\/td>\n<td>\u2714\ufe0f<\/td>\n<td>\u274c<\/td>\n<td>\u274c<\/td>\n<\/tr>\n<tr>\n<td><strong>IX<\/strong><\/td>\n<td>\u2714\ufe0f<\/td>\n<td>\u2714\ufe0f<\/td>\n<td>\u274c<\/td>\n<td>\u274c<\/td>\n<td>\u274c<\/td>\n<\/tr>\n<tr>\n<td><strong>S<\/strong><\/td>\n<td>\u2714\ufe0f<\/td>\n<td>\u274c<\/td>\n<td>\u2714\ufe0f<\/td>\n<td>\u274c<\/td>\n<td>\u274c<\/td>\n<\/tr>\n<tr>\n<td><strong>U<\/strong><\/td>\n<td>\u274c<\/td>\n<td>\u274c<\/td>\n<td>\u274c<\/td>\n<td>\u2714\ufe0f<\/td>\n<td>\u274c<\/td>\n<\/tr>\n<tr>\n<td><strong>X<\/strong><\/td>\n<td>\u274c<\/td>\n<td>\u274c<\/td>\n<td>\u274c<\/td>\n<td>\u274c<\/td>\n<td>\u274c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img decoding=\"async\" src=\"https:\/\/pic.cirno.fun\/sssn-blog-pics\/image-20241228193737738.png\" alt=\"image-20241228193737738\" style=\"zoom:33%;\" \/><\/p>\n<p>\u5176\u4e2d, \u4e0d\u4f7f\u7528FOR UPDATE\u7684\u505a\u6cd5\u6307UPDATE+\u5b50\u67e5\u8be2<\/p>\n<p><strong>\u9650\u5236\u8bf4\u660e<\/strong><\/p>\n<p>\u5728SQL\u4e2d, <code>SELECT<\/code> \u8bed\u53e5\u4e0d\u80fd\u5305\u542b <code>FOR UPDATE OF<\/code>\uff0c\u4e5f\u4e0d\u80fd\u7528\u4e8e <code>DELETE WHERE CURRENT OF<\/code>\u6216 <code>UPDATE WHERE CURRENT OF<\/code> \u7684\u64cd\u4f5c\uff0c\u5982\u679c <code>SELECT<\/code> \u8bed\u53e5\u4e2d\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<ul>\n<li><strong>ORDER BY<\/strong>\uff1a\u5373\u5bf9\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c\u6392\u5e8f<\/li>\n<li><strong>GROUP BY<\/strong>\uff1a\u5373\u5bf9\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c\u5206\u7ec4<\/li>\n<li><strong>DISTINCT<\/strong>\uff1a\u5373\u53bb\u9664\u67e5\u8be2\u7ed3\u679c\u4e2d\u7684\u91cd\u590d\u6570\u636e<\/li>\n<li><strong>\u96c6\u5408\u64cd\u4f5c\u7b26\uff08Set Operators\uff09<\/strong>\uff1a\u5305\u62ec <strong><code>UNION<\/code><\/strong>\u3001<strong><code>EXCEPT<\/code><\/strong> \u548c <strong><code>INTERSECT<\/code><\/strong><\/li>\n<li><strong>\u51fd\u6570\uff08Function\uff09<\/strong>\uff1a\u6307\u5728\u67e5\u8be2\u8bed\u53e5\u4e2d\u8c03\u7528\u7684\u51fd\u6570<\/li>\n<li><strong>Join<\/strong>\uff1a\u5373\u5173\u8054\u591a\u4e2a\u8868\u7684\u67e5\u8be2\u64cd\u4f5c<\/li>\n<li><strong><code>FOR FETCH ONLY<\/code>\/<code>FOR READ ONLY<\/code><\/strong>\uff1a\u53ea\u7528\u4e8e\u53ea\u8bfb\u7684\u67e5\u8be2\u64cd\u4f5c<\/li>\n<\/ul>\n<p>\u4f7f\u7528<code>rs.close()<\/code>\u540e, \u6e38\u6807\u5173\u95ed, \u5185\u5b58\u91ca\u653e, \u4f46\u662f\u9501\u4e0d\u4f1a\u6539\u53d8\u72b6\u6001, \u9664\u975ecommit<\/p>\n<h2>\u7ed3\u679c\u96c6\u91cd\u5b9a\u4f4d<\/h2>\n<p>\u5982\u679c\u8981\u4fee\u6539\u7684\u6570\u636e\u91cf\u592a\u5927, \u5219\u5e94\u8be5\u6bcf500\u884c\u5de6\u53f3\u63d0\u4ea4\u4e00\u6b21, \u5982\u679c\u9047\u5230\u4e86ABEND(\u5f02\u5e38\u7ec8\u6b62), \u5219\u5b9a\u4f4d\u6e38\u6807\u5230\u68c0\u67e5\u70b9<\/p>\n<pre><code class=\"language-sql\">-- \u521b\u5efa\u4e00\u4e2a\u540d\u4e3a RESTART \u7684\u8868\uff0c\u7528\u4e8e\u4fdd\u5b58\u6e38\u6807\u91cd\u65b0\u5b9a\u4f4d\u7684\u503c\nCREATE TABLE RESTART (VALUE INTEGER NOT NULL);<\/code><\/pre>\n<pre><code class=\"language-java\">int ctr = 0;                 \/\/ \u8ba1\u6570\u5668\uff0c\u7528\u4e8e\u8bb0\u5f55\u5f53\u524d\u5904\u7406\u7684\u884c\u6570\nint MinValue = 0;            \/\/ \u6700\u5c0f\u503c\uff0c\u7528\u4e8e\u521d\u59cb\u5316\u6216\u91cd\u65b0\u5b9a\u4f4d\nint abValue = 0;             \/\/ \u4fdd\u5b58\u7684\u91cd\u65b0\u5b9a\u4f4d\u503c\uff08\u4ece RESTART \u8868\u4e2d\u8bfb\u53d6\uff09\n\nResultSet rs2 = null;        \/\/ \u7b2c\u4e8c\u4e2a\u7ed3\u679c\u96c6\uff0c\u7528\u4e8e\u5904\u7406\u4e3b\u6570\u636e\nStatement stmt = con.createStatement();  \/\/ \u521b\u5efa SQL \u8bed\u53e5\u5bf9\u8c61\n\n\/\/ \u67e5\u8be2 RESTART \u8868\u7684\u503c\uff0c\u7528\u4e8e\u83b7\u53d6\u4e0a\u6b21\u5904\u7406\u7684\u4e2d\u65ad\u4f4d\u7f6e\nsql = &quot;SELECT VALUE FROM RESTART&quot;;\nResultSet rs = stmt.executeQuery(sql);\n\n\/\/ \u68c0\u67e5\u662f\u5426\u6709\u503c\uff0c\u5e76\u5c06\u503c\u8d4b\u7ed9 abValue\nwhile (rs.next()) {\n    abValue = rs.getShort(1);  \/\/ \u83b7\u53d6\u7b2c1\u5217\u7684\u503c\n}\n\n\/\/ \u6784\u9020\u4e00\u4e2a\u67e5\u8be2\uff0c\u7528\u4e8e\u4ece\u4e3b\u8868 T1 \u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u4f7f\u7528 abValue \u6765\u5b9a\u4f4d\u8d77\u59cb\u4f4d\u7f6e\nsql2 = &quot;SELECT X, Y, Z FROM T1 &quot;\n     + &quot;WHERE X &gt; ? &quot;        \/\/ \u4f7f\u7528\u5360\u4f4d\u7b26 ? \u8868\u793a\u6761\u4ef6\n     + &quot;ORDER BY X&quot;;         \/\/ \u786e\u4fdd\u7ed3\u679c\u96c6\u6709\u5e8f\n\nPreparedStatement pstmt = con.prepareStatement(sql2,\n    rs2.TYPE_SCROLL_INSENSITIVE,  \/\/ \u652f\u6301\u6e38\u6807\u79fb\u52a8\uff0c\u7ed3\u679c\u96c6\u4e0d\u4f1a\u968f\u6570\u636e\u5e93\u53d8\u5316\u800c\u6539\u53d8\n    rs2.CONCUR_READ_ONLY);        \/\/ \u7ed3\u679c\u96c6\u4e3a\u53ea\u8bfb\uff0c\u9632\u6b62\u610f\u5916\u4fee\u6539\n\npstmt.setShort(1, abValue);       \/\/ \u7ed1\u5b9a ? \u53c2\u6570\u4e3a abValue \u7684\u503c\nrs2 = pstmt.executeQuery();       \/\/ \u6267\u884c\u67e5\u8be2\u5e76\u8fd4\u56de\u7ed3\u679c\u96c6\n\n\/\/ \u904d\u5386\u7ed3\u679c\u96c6 rs2 \u4e2d\u7684\u6bcf\u4e00\u884c\nwhile (rs2.next()) {\n    short storx = rs2.getShort(1);  \/\/ \u83b7\u53d6\u7b2c1\u5217\u7684\u503c\uff08\u4e3b\u952e X\uff09\n    String story = rs2.getString(2);  \/\/ \u83b7\u53d6\u7b2c2\u5217\u7684\u503c\uff08\u5217 Y\uff09\n    String storz = rs2.getString(3);  \/\/ \u83b7\u53d6\u7b2c3\u5217\u7684\u503c\uff08\u5217 Z\uff09\n\n    \/\/ \u68c0\u67e5\u67d0\u4e9b\u6761\u4ef6\uff08\u9700\u6839\u636e\u5b9e\u9645\u4e1a\u52a1\u903b\u8f91\u5b9a\u4e49\uff09\n    if (some condition) {\n        \/\/ \u6784\u9020\u66f4\u65b0\u8bed\u53e5\uff0c\u66f4\u65b0\u8868 T1 \u4e2d\u7684\u5f53\u524d\u884c\n        sql3 = &quot;UPDATE T1 SET Y = ?, Z = ? &quot;\n             + &quot;WHERE X = ?&quot;;  \/\/ \u901a\u8fc7\u4e3b\u952e X \u5b9a\u4f4d\n\n        \/\/ \u4f7f\u7528 PreparedStatement \u66f4\u65b0\u8868 T1\n        PreparedStatement pstmt2 = con.prepareStatement(sql3);\n        pstmt2.setString(1, newStory);  \/\/ \u8bbe\u7f6e\u65b0\u503c\u5230\u5217 Y\n        pstmt2.setString(2, newStorz);  \/\/ \u8bbe\u7f6e\u65b0\u503c\u5230\u5217 Z\n        pstmt2.setShort(3, storx);      \/\/ \u8bbe\u7f6e\u6761\u4ef6\u5217 X \u7684\u503c\n        updateCount = pstmt2.executeUpdate();  \/\/ \u6267\u884c\u66f4\u65b0\u8bed\u53e5\n        ctr = ctr + 1;  \/\/ \u8ba1\u6570\u5668\u9012\u589e\n    }\n    \/\/ \u68c0\u67e5\u8ba1\u6570\u5668\u662f\u5426\u8fbe\u5230 500 \u884c\uff0c\u82e5\u662f\u5219\u6267\u884c\u63d0\u4ea4\u64cd\u4f5c\n        if (ctr == 500) {\n        \/\/ \u66f4\u65b0 RESTART \u8868\u4e2d\u7684\u503c\uff0c\u4fdd\u5b58\u5f53\u524d\u5904\u7406\u7684\u884c\u4f4d\u7f6e\n        sql4 = &quot;UPDATE RESTART SET VALUE = ?&quot;;\n        PreparedStatement pstmt3 = con.prepareStatement(sql4);\n        pstmt3.setShort(1, storx);  \/\/ \u5c06\u5f53\u524d\u884c\u7684\u4e3b\u952e\u503c storx \u4fdd\u5b58\u5230 RESTART \u8868\n        updateCount = pstmt3.executeUpdate();  \/\/ \u6267\u884c\u66f4\u65b0\n\n        \/\/ \u63d0\u4ea4\u4e8b\u52a1\uff0c\u91ca\u653e\u9501\uff0c\u786e\u4fdd\u6570\u636e\u4e00\u81f4\u6027\n        sql5 = &quot;COMMIT&quot;;\n        Statement stmt2 = con.createStatement();\n        stmt2.executeUpdate(sql5);\n\n        ctr = 0;  \/\/ \u91cd\u7f6e\u8ba1\u6570\u5668\n\n        \/\/ \u91cd\u65b0\u5b9a\u4f4d\u6e38\u6807\u5230\u63d0\u4ea4\u524d\u7684\u6700\u540e\u4e00\u884c\n        rs2.absolute(storx);  \/\/ \u6839\u636e\u5b58\u50a8\u7684\u884c\u4f4d\u7f6e\u91cd\u65b0\u5b9a\u4f4d\n    }\n}\n\n\/\/ \u5f53\u6240\u6709\u5904\u7406\u5b8c\u6210\u65f6\uff0c\u4fdd\u5b58\u6700\u540e\u7684\u4f4d\u7f6e\u5e76\u63d0\u4ea4\u4e8b\u52a1\nsql6 = &quot;UPDATE RESTART SET VALUE = ?&quot;;\nPreparedStatement upd = con.prepareStatement(sql6);\nupd.setShort(1, minValue);  \/\/ \u8bbe\u7f6e\u6700\u5c0f\u503c\u6216\u6700\u540e\u5904\u7406\u7684\u503c\nUpdateCount = upd.executeUpdate();  \/\/ \u6267\u884c\u66f4\u65b0\u64cd\u4f5c\n\n\/\/ \u63d0\u4ea4\u4e8b\u52a1\uff0c\u5b8c\u6210\u6574\u4e2a\u6d41\u7a0b\nsql7 = &quot;COMMIT&quot;;\nStatement cstmt = con.createStatement();\ncstmt.executeUpdate(sql7);<\/code><\/pre>\n<h2>\u83b7\u53d6\u5143\u6570\u636e<\/h2>\n<p>ResultSetMetaData \u662f Java \u4e2d\u7528\u4e8e\u63cf\u8ff0\u67e5\u8be2\u7ed3\u679c\u7684\u7ed3\u6784\uff08\u5373\u5143\u6570\u636e\uff09\u7684\u63a5\u53e3<\/p>\n<p>\u901a\u8fc7\u4ece <code>ResultSet<\/code> \u83b7\u53d6 <code>ResultSetMetaData<\/code> \u5bf9\u8c61\uff0c\u53ef\u4ee5\u8bbf\u95ee\u67e5\u8be2\u7ed3\u679c\u7684\u7ed3\u6784\u4fe1\u606f<\/p>\n<pre><code class=\"language-java\">try {\n    \/\/ 1. \u901a\u8fc7 DriverManager \u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\n    Connection sample = DriverManager.getConnection(&quot;jdbc:db2:sample&quot;);\n    \/\/ 2. \u83b7\u53d6 DatabaseMetaData \u5bf9\u8c61\uff0c\u7528\u4e8e\u83b7\u53d6\u6570\u636e\u5e93\u7684\u5143\u6570\u636e\n    DatabaseMetaData dbmd = sample.getMetaData();\n    \/\/ 3. \u8c03\u7528 getSchemas() \u65b9\u6cd5\uff0c\u83b7\u53d6\u6240\u6709\u6a21\u5f0f\u7684 ResultSet\n    ResultSet rs = dbmd.getSchemas();\n    \/\/ 4. \u904d\u5386\u7ed3\u679c\u96c6\uff0c\u9010\u884c\u5904\u7406\u6bcf\u4e2a\u6a21\u5f0f\u4fe1\u606f\n    while (rs.next()) {\n        \/\/ 5. \u83b7\u53d6\u5f53\u524d\u884c\u7684\u7b2c 1 \u5217\uff08\u6a21\u5f0f\u540d\u79f0\uff09\n        String s = rs.getString(1);\n        \/\/ \u6253\u5370\u6a21\u5f0f\u540d\u79f0\n        System.out.println(&quot;\\nSchema Name: &quot; + s);\n    }\n} catch (SQLException e) {\n    \/\/ \u6355\u83b7\u5e76\u6253\u5370 SQL \u5f02\u5e38\n    e.printStackTrace();\n}<\/code><\/pre>\n<h2>\u5927\u5bf9\u8c61<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/pic.cirno.fun\/sssn-blog-pics\/image-20241228212259785.png\" alt=\"image-20241228212259785\" style=\"zoom:33%;\" \/><\/p>\n<h3>CLOB SQL\u5b9e\u4f8b<\/h3>\n<pre><code class=\"language-sql\">-- \u5b9a\u4e49\u4e00\u4e2a\u5305\u542bCLOB\u7684\u8868\nCREATE TABLE example_table (\n    id INT NOT NULL,\n    description CLOB(1M), -- \u6700\u5927\u5b58\u50a8 1MB \u7684\u6587\u672c\n    PRIMARY KEY (id)\n);\n\n-- \u6267\u884c\u63d2\u5165\nINSERT INTO example_table (id, description)\nVALUES (1, &#039;This is a large text stored as CLOB.&#039;);\n\n-- \u67e5\u8be2, \u63d0\u53d6\u524d100\u5b57\u7b26\nSELECT SUBSTR(description, 1, 100) AS snippet FROM example_table WHERE id = 1;<\/code><\/pre>\n<h3>JDBC\u5b9e\u4f8b<\/h3>\n<pre><code class=\"language-java\">String resume = null; \/\/ \u7528\u4e8e\u5b58\u50a8\u7b80\u5386\u5185\u5bb9\nString empnum = &quot;000130&quot;; \/\/ \u5458\u5de5\u7f16\u53f7\nint startper, startper1, startdpt = 0; \/\/ \u7528\u4e8e\u5b58\u50a8\u8d77\u59cb\u4f4d\u7f6e\u7b49\u4fe1\u606f\u7684\u53d8\u91cf\nPreparedStatement stmt1, stmt2, stmt3 = null; \nString sql1, sql2, sql3 = null; \/\/ SQL \u67e5\u8be2\u8bed\u53e5\nString empno, resumefmt = null; \/\/ \u5458\u5de5\u7f16\u53f7\u548c\u7b80\u5386\u683c\u5f0f\nClob resumelob = null; \/\/ \u7528\u4e8e\u5b58\u50a8 CLOB \u5bf9\u8c61\nResultSet rs1, rs2, rs3 = null; \/\/ \u7ed3\u679c\u96c6\u5bf9\u8c61\n\n\/\/ \u5b9a\u4e49 SQL \u67e5\u8be2\u8bed\u53e5\uff0c\u7528\u4e8e\u67e5\u627e &quot;Personal&quot; \u5728\u7b80\u5386\u4e2d\u7684\u8d77\u59cb\u5b57\u8282\u4f4d\u7f6e\nsql1 = &quot;SELECT POSSTR(RESUME,&#039;Personal&#039;) &quot; \/\/ \u67e5\u627e &quot;Personal&quot; \u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\n     + &quot;FROM EMP_RESUME &quot;                 \/\/ \u4ece EMP_RESUME \u8868\u4e2d\u67e5\u8be2\n     + &quot;WHERE EMPNO = ? AND RESUME_FORMAT = &#039;ascii&#039; &quot;; \/\/ \u6761\u4ef6\uff1a\u6307\u5b9a\u5458\u5de5\u7f16\u53f7\u548c\u7b80\u5386\u683c\u5f0f\n\nstmt1 = con.prepareStatement(sql1); \nstmt1.setString(1, empnum); \/\/ \u5c06\u7b2c\u4e00\u4e2a\u53c2\u6570\u8bbe\u7f6e\u4e3a\u5458\u5de5\u7f16\u53f7 empnum\nrs1 = stmt1.executeQuery(); \/\/ \u6267\u884c\u67e5\u8be2\u5e76\u8fd4\u56de\u7ed3\u679c\u96c6\nwhile (rs1.next()) { \n    \/\/ \u83b7\u53d6\u67e5\u8be2\u7ed3\u679c\u7684\u7b2c\u4e00\u5217\u503c\uff0c\u5373 &quot;Personal&quot; \u7684\u8d77\u59cb\u5b57\u8282\u4f4d\u7f6e\n    startper = rs1.getInt(1); \/\/ \u5c06\u8d77\u59cb\u4f4d\u7f6e\u503c\u8d4b\u7ed9 startper\n} <\/code><\/pre>\n<p>\u8fd9\u91cc\u9762\u7684SQL\u5355\u62ff\u51fa\u6765\u8bb2\u4e00\u4e0b<\/p>\n<pre><code class=\"language-sql\">SELECT POSSTR(RESUME, &#039;Personal&#039;) \nFROM EMP_RESUME \nWHERE EMPNO = ? AND RESUME_FORMAT = &#039;ascii&#039;;<\/code><\/pre>\n<p><strong><code>POSSTR<\/code> \u51fd\u6570<\/strong>\uff1a<\/p>\n<p>\u7528\u4e8e\u67e5\u627e\u4e00\u4e2a\u5b57\u7b26\u4e32\u5728\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e<\/p>\n<p>\u8fd4\u56de\u503c\uff1a\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\uff08\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\uff0c\u8d77\u59cb\u4f4d\u7f6e\u4ece 1`\u5f00\u59cb\uff09. \u5982\u679c\u5b57\u7b26\u4e32\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de 0<\/p>\n<ol>\n<li>\u7b2c\u4e00\u4e2a\u53c2\u6570\uff1a<code>RESUME<\/code> \u5217\uff08CLOB \u7c7b\u578b\uff09\uff0c\u5373\u5b58\u50a8\u7b80\u5386\u6570\u636e\u7684\u5217<\/li>\n<li>\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff1a\u76ee\u6807\u5b57\u7b26\u4e32 <code>&#039;Personal&#039;<\/code>\uff0c\u9700\u8981\u5728 <code>RESUME<\/code> \u5217\u4e2d\u67e5\u627e\u7684\u5b50\u4e32<\/li>\n<\/ol>\n<h3>CLOB\u7b80\u5386\u5927\u9898<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/pic.cirno.fun\/sssn-blog-pics\/image-20241229112551031.png\" alt=\"image-20241229112551031\" style=\"zoom:50%;\" \/><\/p>\n<p>\u8fd9\u9898\u51fa\u592a\u590d\u6742\u4e86, \u4e0b\u9762\u4ee3\u7801\u4e0d\u662f\u6309\u7167\u9898\u5e72\u505a\u7684<\/p>\n<pre><code class=\"language-java\">\/\/ 1. \u63a5\u6536\u7528\u6237\u8f93\u5165\u7684\u5458\u5de5\u7f16\u53f7\nSystem.out.print(&quot;\u8bf7\u8f93\u5165\u5458\u5de5\u7f16\u53f7\uff1a&quot;);\nString empno = scanner.nextLine();\n\/\/ 2. \u67e5\u8be2\u7b80\u5386\u4e2d &quot;Personal&quot; \u5173\u952e\u8bcd\u7684\u4f4d\u7f6e\nString sql1 = &quot;SELECT POSSTR(resume, &#039;Personal&#039;) FROM emp_resume WHERE empno = ?&quot;;\nPreparedStatement pstmt1 = con.prepareStatement(sql1);\npstmt1.setString(1, empno);\nResultSet rs1 = pstmt1.executeQuery();\nint startPersonal = -1;\nif (rs1.next()) {\n    startPersonal = rs1.getInt(1); \/\/ \u83b7\u53d6 &quot;Personal&quot; \u5173\u952e\u8bcd\u7684\u4f4d\u7f6e\n}\n\/\/ 3. \u67e5\u8be2\u7b80\u5386\u4e2d &quot;Department&quot; \u5173\u952e\u8bcd\u7684\u4f4d\u7f6e\nString sql2 = &quot;SELECT POSSTR(resume, &#039;Department&#039;) FROM emp_resume WHERE empno = ?&quot;;\nPreparedStatement pstmt2 = con.prepareStatement(sql2);\npstmt2.setString(1, empno);\nResultSet rs2 = pstmt2.executeQuery();\nint startDepartment = -1;\nif (rs2.next()) {\n    startDepartment = rs2.getInt(1); \/\/ \u83b7\u53d6 &quot;Department&quot; \u5173\u952e\u8bcd\u7684\u4f4d\u7f6e\n}\n\/\/ 4. \u8ba1\u7b97 &quot;Personal&quot; \u524d\u7684\u5185\u5bb9\u8303\u56f4\nint startPersonalAdjusted = startPersonal - 1;\n\/\/ 5. \u62fc\u63a5\u7b80\u5386\u4e2d &quot;Personal&quot; \u524d\u7684\u5185\u5bb9\u548c &quot;Department&quot; \u540e\u7684\u5185\u5bb9\nString sql3 = &quot;SELECT SUBSTR(resume, 1, ?) || SUBSTR(resume, ?) FROM emp_resume WHERE empno = ?&quot;;\nPreparedStatement pstmt3 = con.prepareStatement(sql3);\npstmt3.setInt(1, startPersonalAdjusted); \/\/ \u622a\u53d6\u4ece\u5f00\u59cb\u5230 &quot;Personal&quot; \u524d\u7684\u5185\u5bb9\npstmt3.setInt(2, startDepartment);      \/\/ \u4ece &quot;Department&quot; \u5f00\u59cb\u622a\u53d6\npstmt3.setString(3, empno);             \/\/ \u8bbe\u7f6e\u5458\u5de5\u7f16\u53f7\nResultSet rs3 = pstmt3.executeQuery();\n\/\/ 6. \u8f93\u51fa\u5904\u7406\u540e\u7684\u7b80\u5386\nif (rs3.next()) {\n    Clob clob = rs3.getClob(1); \/\/ \u83b7\u53d6 CLOB \u5bf9\u8c61\n    String resume = clob.getSubString(1, (int) clob.length()); \/\/ \u8f6c\u4e3a\u5b57\u7b26\u4e32\n    System.out.println(&quot;\u5904\u7406\u540e\u7684\u7b80\u5386\u5185\u5bb9\uff1a&quot;);\n    System.out.println(resume);\n}\n<\/code><\/pre>\n<p>CLOB\u5927\u9898\u603b\u4f53\u601d\u8def\u5c31\u8fd9\u6837<\/p>\n<ul>\n<li>\u5148\u7528<code>POSSTR<\/code>\u67e5\u5173\u952e\u8bcd\u4e0b\u6807, \u4e00\u6b21\u53ea\u80fd\u67e5\u4e00\u4e2a<\/li>\n<li>\u518d\u7528<code>SUBSTR<\/code>\u67e5\u5b50\u4e32, \u8fd9\u65f6\u5019\u7528<code>||<\/code>\u968f\u4fbf\u62fc<\/li>\n<\/ul>\n<h3>\u63d2\u5165\u56fe\u7247<\/h3>\n<p>\u5b8c\u6574\u7684\u8fc7\u7a0b, \u4f7f\u7528\u4e86try\u7684\u81ea\u52a8\u8d44\u6e90\u7ba1\u7406<\/p>\n<pre><code class=\"language-java\">\/\/ \u56fe\u7247\u6587\u4ef6\u8def\u5f84\nString imagePath = &quot;e:\\\\photo\\\\a.jpg&quot;;\n\/\/ SQL \u63d2\u5165\u8bed\u53e5: \u8868student\u7684photo\u5217, \u800c\u4e14\u5fc5\u987b\u662f\u4e00\u7ec4(?)\nString sql = &quot;INSERT INTO student (photo) VALUES (?)&quot;;\ntry (\n    \/\/ \u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\n    Connection connection = DriverManager.getConnection(url, user, password);\n    \/\/ \u521b\u5efa PreparedStatement\n    PreparedStatement statement = connection.prepareStatement(sql);\n    \/\/ \u521b\u5efa\u6587\u4ef6\u8f93\u5165\u6d41\u8bfb\u53d6\u56fe\u7247\n    FileInputStream inputStream = new FileInputStream(new File(imagePath))\n) {\n    \/\/ \u8bbe\u7f6e BLOB \u53c2\u6570\n    statement.setBlob(1, inputStream);\n\n    \/\/ \u6267\u884c\u63d2\u5165\u64cd\u4f5c\n    int rowsInserted = statement.executeUpdate();\n\n    if (rowsInserted &gt; 0) {\n        System.out.println(&quot;\u56fe\u7247\u5df2\u6210\u529f\u63d2\u5165\u5230\u6570\u636e\u5e93\uff01&quot;);\n    } else {\n        System.out.println(&quot;\u63d2\u5165\u56fe\u7247\u5931\u8d25\uff01&quot;);\n    }\n} catch (Exception e) {\n    e.printStackTrace();\n}<\/code><\/pre>\n<p>\u7cbe\u7b80\u90e8\u5206<\/p>\n<pre><code class=\"language-java\">\/\/ \u521b\u5efa PreparedStatement \u5bf9\u8c61\uff0c\u7528\u4e8e\u6267\u884c SQL \u8bed\u53e5\n\/\/ \u8fd9\u91cc\u5b9e\u9645\u4e0a\u662f\u63d2\u5165\u4e86\u4e00\u884c\nPreparedStatement preparedStatement = conn.prepareStatement(\n    &quot;INSERT INTO emp_photo VALUES (&#039;000130&#039;, &#039;jpeg&#039;, ?)&quot;\n);\n\n\/\/ \u521b\u5efa\u6587\u4ef6\u5bf9\u8c61\uff0c\u53c2\u6570\u662f\u672c\u5730\u56fe\u7247\u7684\u8def\u5f84\u540d\nFile file = new File(&quot;e:\\\\folder\\\\a123.jpg&quot;);\n\n\/\/ \u521b\u5efa BufferedInputStream \u5bf9\u8c61\uff0c\u7528\u4e8e\u8bfb\u53d6\u6587\u4ef6\u6570\u636e\nBufferedInputStream imageInput = new BufferedInputStream(\n    new FileInputStream(file)\n);\n\n\/\/ \u8bbe\u7f6e\u53c2\u6570\uff1a\n\/\/ \u7b2c1\u4e2a\u53c2\u6570\u662f\u5360\u4f4d\u7b26\u7d22\u5f15\uff08?\u7684\u4f4d\u7f6e\uff09\uff0c\n\/\/ \u7b2c2\u4e2a\u53c2\u6570\u662f InputStream \u5bf9\u8c61\uff0c\n\/\/ \u7b2c3\u4e2a\u53c2\u6570\u662f\u6587\u4ef6\u7684\u5b57\u8282\u957f\u5ea6\uff08\u9700\u8981\u5f3a\u5236\u8f6c\u6362\u4e3aint\uff09\npreparedStatement.setBinaryStream(1, imageInput, (int) file.length());\n\n\/\/ \u6267\u884c SQL \u8bed\u53e5\uff0c\u5c06\u6570\u636e\u63d2\u5165\u6570\u636e\u5e93\npreparedStatement.executeUpdate();<\/code><\/pre>\n<h2>GUI<\/h2>\n<p>\u8bf4\u662f\u4e0d\u8003, \u4f46\u662f\u5f80\u5e74\u4e5f\u6709\u6d89\u53ca\u5230\u7684, \u5c31\u4e00\u70b9\u70b9, \u95ee\u4e2a\u7b80\u5355\u7528\u6cd5\u4e4b\u7c7b\u7684, \u91cd\u70b9\u8fd8\u662f\u4e0a\u9762<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/pic.cirno.fun\/sssn-blog-pics\/image-20241229004117663.png\" alt=\"image-20241229004117663\" style=\"zoom:33%;\" \/><\/p>\n<h3><strong>\u5e38\u89c1 Swing \u7ec4\u4ef6<\/strong><\/h3>\n<p>\u4ee5\u4e0b\u662f Swing \u4e2d\u4e00\u4e9b\u5e38\u7528\u7ec4\u4ef6\u53ca\u5176\u529f\u80fd\uff1a<\/p>\n<ul>\n<li>\n<p><strong>JFrame<\/strong>\uff1a\u8868\u793a\u4e00\u4e2a\u7a97\u53e3\uff0c\u901a\u5e38\u4f5c\u4e3a\u5e94\u7528\u7a0b\u5e8f\u7684\u4e3b\u7a97\u53e3<\/p>\n<\/li>\n<li>\n<p><strong>JPanel<\/strong>\uff1a\u7528\u4e8e\u7ec4\u7ec7\u7ec4\u4ef6\u7684\u5bb9\u5668<\/p>\n<\/li>\n<li>\n<p><strong>JLabel<\/strong>\uff1a\u663e\u793a\u6587\u672c\u6216\u56fe\u50cf\u7684\u6807\u7b7e<\/p>\n<\/li>\n<li>\n<p><strong>JButton<\/strong>\uff1a\u6309\u94ae\u7ec4\u4ef6\uff0c\u7528\u4e8e\u89e6\u53d1\u64cd\u4f5c<\/p>\n<\/li>\n<li>\n<p><strong>JTextField<\/strong>\uff1a\u5355\u884c\u6587\u672c\u8f93\u5165\u6846<\/p>\n<\/li>\n<li>\n<p><strong>JTable<\/strong>\uff1a\u8868\u683c\u7ec4\u4ef6<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5409\u6797\u5927\u5b66\u8f6f\u4ef6\u5b66\u9662 \u4f01\u4e1a\u7ea7\u6570\u636e\u5e93\u5e94\u7528\u5f00\u53d1 \u8003\u9898\u56de\u5fc6 \u5409\u6797\u5927\u5b66\u8f6f\u4ef6\u5b66\u9662\u4f01\u4e1a\u7ea7\u6570\u636e\u5e93\u5e94 [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[],"class_list":["post-812","post","type-post","status-publish","format-standard","hentry","category-30"],"_links":{"self":[{"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/posts\/812","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=812"}],"version-history":[{"count":4,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/posts\/812\/revisions"}],"predecessor-version":[{"id":846,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/posts\/812\/revisions\/846"}],"wp:attachment":[{"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}