{"id":1658,"date":"2020-11-28T21:33:31","date_gmt":"2020-11-28T13:33:31","guid":{"rendered":"https:\/\/www.yusian.com\/blog\/?p=1658"},"modified":"2020-11-28T21:36:54","modified_gmt":"2020-11-28T13:36:54","slug":"java%e4%b8%ad%e6%95%b0%e6%8d%ae%e5%ba%93%e6%93%8d%e4%bd%9c%e4%b8%ad%e7%9a%84%e4%ba%8b%e5%8a%a1%e5%ae%9e%e7%8e%b0jdbc","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/java\/2020\/11\/28\/2133311658.html","title":{"rendered":"Java\u4e2d\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\u7684\u4e8b\u52a1\u5b9e\u73b0(JDBC)"},"content":{"rendered":"<p>\u57fa\u672c\u5b9e\u73b0\u5148\u53c2\u7167\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.yusian.com\/blog\/java\/2020\/11\/26\/2215431646.html\" title=\"Java\u7b80\u5355\u5b8c\u6574\u6570\u636e\u5e93\u64cd\u4f5c\u5168\u8fc7\u7a0b(JDBC)\">Java\u7b80\u5355\u5b8c\u6574\u6570\u636e\u5e93\u64cd\u4f5c\u5168\u8fc7\u7a0b(JDBC)<\/a><\/p>\n<p>\u4e8b\u52a1\u7684\u652f\u6301\u5176\u5b9e\u53ea\u9700\u8981\u5728Statement\u5bf9\u8c61\u6267\u884cSQL\u4e4b\u524d\u5f00\u542f\u4e8b\u52a1\uff0c\u6267\u884c\u4e4b\u540e\u63d0\u4ea4\u4e8b\u52a1\uff0c\u51fa\u73b0\u5f02\u5e38\u60c5\u51b5\u56de\u6eda\u6570\u636e\u5373\u53ef\uff0c\u5206\u522b\u5bf9\u5e94\u4e09\u4e2a\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><code>setAutoCommit(boolean)<\/code>\uff1a\u53c2\u6570\u4e3afalse\u5219\u5f00\u542f\u4e8b\u52a1<\/li>\n<li><code>commit()<\/code>\uff1a\u63d0\u4ea4\u4e8b\u52a1<\/li>\n<li><code>rollback()<\/code>\uff1a\u56de\u6eda\u4e8b\u52a1<\/li>\n<\/ul>\n<p>\u8fd9\u4e09\u4e2a\u65b9\u6cd5\u662f\u6570\u636e\u5e93\u8fde\u63a5\u7c7b<code>Connection<\/code>\u7684\u5bf9\u8c61\u65b9\u6cd5\u3002<\/p>\n<hr \/>\n<p><strong>\u5b9e\u73b0\u53c2\u8003\uff1a<\/strong><\/p>\n<pre><code class=\"language-java line-numbers\">package com.yusian.transaction;\n\nimport java.sql.Connection;\nimport java.sql.PreparedStatement;\nimport java.sql.SQLException;\n\npublic class TransactionDemo {\n    public static void main(String[] args) {\n        Connection conn = null;\n        PreparedStatement pstmt1 = null;\n        PreparedStatement pstmt2 = null;\n        try {\n            conn = Utils.getConnection();\n            pstmt1 = conn.prepareStatement(\"update account set balance = balance - ? where id = ?\");\n            pstmt2 = conn.prepareStatement(\"update account set balance = balance + ? where id = ?\");\n            pstmt1.setDouble(1, 500);\n            pstmt1.setInt(2, 1);\n            pstmt2.setDouble(1, 500);\n            pstmt2.setInt(2, 2);\n            \/\/ \u91cd\u70b9\u6765\u4e86\uff0c\u5f00\u542f\u4e8b\u52a1\u548c\u4e0d\u5f00\u542f\u4e8b\u52a1\u5c31\u53ea\u6709\u8fd9\u4e00\u70b9\u5dee\u522b\n            conn.setAutoCommit(false);  \/\/ \u5f00\u542f\u4e8b\u52a1\n            pstmt1.executeUpdate();\n            pstmt2.executeUpdate();     \/\/ \u63d0\u4ea4\u4e8b\u52a1\n            conn.commit();\n        } catch (Exception e) {\n            e.printStackTrace();\n            try {\n                \/\/ \u4e8b\u52a1\u56de\u6eda\uff0c\u6ce8\u610f\uff1a\u56de\u6eda\u5e94\u8be5\u5728\u6240\u6709\u7684\u5f02\u5e38\u6355\u83b7\u4e2d\u6267\u884c\uff0c\u56e0\u4e3a\u65e0\u8bba\u53d1\u751f\u4f55\u79cd\u5f02\u5e38\uff0c\u90fd\u5e94\u8be5\u56de\u6eda\u3002\n                conn.rollback();\n            } catch (SQLException sqlException) {\n                sqlException.printStackTrace();\n            }\n        }finally {\n            \/\/ \u8d44\u6e90\u91ca\u653e\n            Utils.releaseCloseableObject(pstmt1);\n            Utils.releaseCloseableObject(pstmt2);\n            Utils.releaseCloseableObject(conn);\n        }\n    }\n}\n<\/code><\/pre>\n<p><!--more--><\/p>\n<hr \/>\n<p><strong>Utils\u5de5\u5177\u7c7b\uff1a<\/strong><\/p>\n<pre><code class=\"language-java line-numbers\">package com.yusian.transaction;\n\nimport java.io.FileReader;\nimport java.io.IOException;\nimport java.net.URL;\nimport java.sql.*;\nimport java.util.Properties;\n\npublic class Utils {\n    private static String url;\n    private static String user;\n    private static String pass;\n    private static String driver;\n\n    static {\n        ClassLoader loader = Utils.class.getClassLoader();\n        URL res = loader.getResource(\"jdbc.properties\");\n        try {\n            Properties prop = new Properties();\n            prop.load(new FileReader(res.getPath()));\n            url = prop.getProperty(\"url\");\n            user = prop.getProperty(\"user\");\n            pass = prop.getProperty(\"pass\");\n            driver = prop.getProperty(\"driver\");\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n    }\n\n\n    public static Connection getConnection() throws SQLException {\n        try {\n            Class.forName(driver);\n        } catch (ClassNotFoundException e) {\n            e.printStackTrace();\n        }\n        return DriverManager.getConnection(url, user, pass);\n    }\n\n\n    \/**\n     * \u91ca\u653e\u67e5\u8be2\u7c7bSQL\u8bed\u53e5\u7684\u76f8\u5173\u8d44\u6e90\n     * @param conn SQL\u8fde\u63a5\u5bf9\u8c61\n     * @param stmt SQL\u6267\u884c\u5bf9\u8c61\n     * @param retSet \u67e5\u8be2\u7ed3\u679c\n     *\/\n    public static void close(Connection conn, Statement stmt, ResultSet retSet) {\n        releaseCloseableObject(retSet);\n        close(conn, stmt);\n    }\n\n    public static void close(Connection conn, Statement stmt) {\n        releaseCloseableObject(stmt);\n        releaseCloseableObject(conn);\n    }\n\n    public static void releaseCloseableObject(AutoCloseable object) {\n        if (object == null) return;\n        try {\n            object.close();\n        } catch (Exception e) {\n            e.printStackTrace();;\n        }\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Java\u4e2d\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\u7684\u4e8b\u52a1\u5b9e\u73b0(JDBC)<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[292,22,284],"class_list":["post-1658","post","type-post","status-publish","format-standard","hentry","category-java","tag-jdbc","tag-mysql","tag-284"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1658","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=1658"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1658\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=1658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=1658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=1658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}