{"id":1646,"date":"2020-11-26T22:15:43","date_gmt":"2020-11-26T14:15:43","guid":{"rendered":"https:\/\/www.yusian.com\/blog\/?p=1646"},"modified":"2020-11-28T15:32:09","modified_gmt":"2020-11-28T07:32:09","slug":"java%e7%ae%80%e5%8d%95%e5%ae%8c%e6%95%b4%e6%95%b0%e6%8d%ae%e5%ba%93%e6%93%8d%e4%bd%9c%e5%85%a8%e8%bf%87%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/java\/2020\/11\/26\/2215431646.html","title":{"rendered":"Java\u7b80\u5355\u5b8c\u6574\u6570\u636e\u5e93\u64cd\u4f5c\u5168\u8fc7\u7a0b(JDBC)"},"content":{"rendered":"<h2>1\u3001\u57fa\u672c\u6982\u5ff5\u4e0e\u6b65\u9aa4<\/h2>\n<h3>1.1 \u521d\u59cb\u5316JDBC<\/h3>\n<p>jdbc\u5b9a\u4e49\u7684\u662f\u4e00\u5957\u63a5\u53e3\uff0c\u6bcf\u79cd\u6570\u636e\u5e93\u7c7b\u578b\u90fd\u6709\u5404\u81ea\u7684\u5b9e\u73b0\uff0c\u6211\u4eec\u8c03\u7528\u7684\u90fd\u662f\u63a5\u53e3\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u5e76\u4e0d\u6e05\u695a\u8be5\u91c7\u7528\u54ea\u79cd\u5b9e\u4f8b\uff0c\u6240\u4ee5\u9700\u8981\u5148\u521d\u59cb\u5316\u3002<\/p>\n<pre><code class=\"language-java line-numbers\">Class.forName(\"com.mysql.jdbc.Driver\");\n<\/code><\/pre>\n<h3>1.2 \u6570\u636e\u5e93\u8fde\u63a5\u5bf9\u8c61(Connection\u7c7b)<\/h3>\n<pre><code class=\"language-java line-numbers\">String connectUrl = \"jdbc:mysql:\/\/example.domain.com:3306\/db3?useSSL=false\";\nConnection conn = DriverManager.getConnection(connectUrl, \"user\", \"password\");\n<\/code><\/pre>\n<h3>1.3 \u6267\u884cSQL(Statement\u7c7b)<\/h3>\n<p>\u4e24\u4e2a\u91cd\u8981\u7684\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><code>executeUpdate<\/code> \u6267\u884cDML\u8bed\u53e5\u548cDDL\u8bed\u53e5\uff0c\u5373\u6570\u636e\u6216\u5e93\u8868\u7684\u589e\u5220\u6539\u64cd\u4f5c\uff1b<\/li>\n<li><code>executeQuery<\/code> \u6267\u884cDQL\u8bed\u53e5\uff0c\u5373\u6570\u636e\u5e93\u7684\u67e5\u8be2\u64cd\u4f5c\uff1b<\/li>\n<\/ul>\n<pre><code class=\"language-java line-numbers\">String sql = \"select * from xxx\";\nStatement stmt = conn.createStatement();\nResultSet resultSet = stmt.executeQuery(sql);\nwhile (resultSet.next()) {\n    String uname = resultSet.getString(\"column1\");\n    int balance = resultSet.getInt(\"column2\");\n    System.out.println(uname + \"--\" + balance);\n}\n<\/code><\/pre>\n<h3>1.4 \u91ca\u653e\u8d44\u6e90<\/h3>\n<pre><code class=\"language-java line-numbers\">stmt.close();\nconn.close();\n<\/code><\/pre>\n<p><!--more--><\/p>\n<h2>2 \u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u7a0b\u5e8f<\/h2>\n<pre><code class=\"language-java line-numbers\">package com.yusian.update;\n\nimport java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.SQLException;\nimport java.sql.Statement;\n\npublic class UpdateDemo {\n    public static void main(String[] args) {\n        Statement stmt = null;\n        Connection conn = null;\n        try {\n            \/\/ 1\u3001\u6ce8\u518c\u9a71\u52a8\n            Class.forName(\"com.mysql.jdbc.Driver\");\n            String url = \"jdbc:mysql:\/\/example.domain.com:3306\/db?useSSL=false\";\n            \/\/ 2\u3001\u8fde\u63a5\u6570\u636e\u5e93\n            conn = DriverManager.getConnection(url, \"username\", \"password\");\n            \/\/ 3\u3001\u83b7\u53d6\u6267\u884cSQL\u5bf9\u8c61\n            stmt = conn.createStatement();\n            \/\/ 4\u3001\u6267\u884cSQL\u8bed\u53e5\u5e76\u83b7\u53d6\u6267\u884c\u7ed3\u679c\n            final int count = stmt.executeUpdate(\"update account set balance = 1500 where id = 3\");\n            if (count == 0) {\n                System.out.println(\"\u6267\u884c\u5931\u8d25\uff01\");\n            } else {\n                System.out.println(\"\u6267\u884c\u6210\u529f\uff01\" + count);\n            }\n        } catch (ClassNotFoundException | SQLException e) {\n            e.printStackTrace();\n        } finally {\n            \/\/ 5\u3001\u91ca\u653e\u8d44\u6e90\n            if (stmt != null) {\n                try {\n                    stmt.close();\n                } catch (SQLException e) {\n                    e.printStackTrace();\n                }\n            }\n            if (conn != null) {\n                try {\n                    conn.close();\n                } catch (SQLException e) {\n                    e.printStackTrace();\n                }\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<h2>3 \u5c01\u88c5\u4f18\u5316<\/h2>\n<h3>3.1 \u5de5\u5177\u7c7b<\/h3>\n<pre><code class=\"language-java line-numbers\">package com.yusian.utils;\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\n    \/**\n     * \u9759\u6001\u4ee3\u7801\u5757\n     *\/\n    static {\n        \/\/ \u901a\u8fc7\u7c7b\u52a0\u8f7d\u5668\u83b7\u53d6\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\n        ClassLoader loader = Utils.class.getClassLoader();\n        final URL resource = loader.getResource(\"jdbc.properties\");\n        final String path = resource.getPath();\n        try {\n            \/\/ \u4ece\u914d\u7f6e\u6587\u4ef6\u4e2d\u52a0\u8f7d\u6570\u636e\u5e93\u76f8\u5173\u914d\u7f6e\uff0c\u5e76\u8d4b\u503c\u7ed9\u5f53\u524d\u7c7b\u7684\u9759\u6001\u53d8\u91cf\u65b9\u4fbf\u8c03\u7528\n            Properties pros = new Properties();\n            pros.load(new FileReader(path));\n            url = pros.getProperty(\"url\");\n            user = pros.getProperty(\"user\");\n            pass = pros.getProperty(\"pass\");\n            driver = pros.getProperty(\"driver\");\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n    }\n\n    \/**\n     * \u4ece\u914d\u7f6e\u6587\u4ef6\u52a0\u8f7d\u7684\u76f8\u5173\u914d\u7f6e\u6570\u636e\n     *\/\n    private static String url;\n    private static String user;\n    private static String pass;\n    private static String driver;\n\n    \/**\n     * \u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\n     * @return \u6570\u636e\u5e93\u8fde\u63a5\u5bf9\u8c61\n     * @throws SQLException\n     *\/\n    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     * \u91ca\u653e\u67e5\u8be2\u7c7b\u64cd\u4f5c\u7684\u76f8\u5173\u8d44\u6e90\n     * @param conn \u8fde\u63a5\u5bf9\u8c61\n     * @param stmt SQL\u6267\u884c\u5bf9\u8c61\n     * @param retSet \u67e5\u8be2\u7ed3\u679c\u5bf9\u8c61\n     *\/\n    public static void releaseQureyObject(Connection conn, Statement stmt, ResultSet retSet) {\n        releaseCloseableObject(retSet);\n        releaseUpdateObject(conn, stmt);\n    }\n\n    \/**\n     * \u91ca\u653e\u64cd\u4f5c\u7c7b\u7684\u76f8\u5173\u8d44\u6e90\n     * @param conn \u8fde\u63a5\u5bf9\u8c61\n     * @param stmt SQL\u6267\u884c\u5bf9\u8c61\n     *\/\n    public static void releaseUpdateObject(Connection conn, Statement stmt) {\n        releaseCloseableObject(stmt);\n        releaseCloseableObject(conn);\n    }\n\n    \/**\n     * \u91ca\u653e\u5355\u4e2a\u8d44\u6e90\u7684\u5c01\u88c5\uff0c\u56e0\u4e3a\u90fd\u6709close\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63a5\u6536\u7c7b\u578b\u53ef\u501f\u7528AutoCloseable\u63a5\u53e3\u7c7b\n     * @param obj \u91ca\u653e\u5bf9\u8c61\n     *\/\n    private static void releaseCloseableObject(AutoCloseable obj) {\n        if (obj == null) return;\n        try {\n            obj.close();\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}\n<\/code><\/pre>\n<h2>3.2 \u5b9e\u73b0<\/h2>\n<pre><code class=\"language-java line-numbers\">package com.yusian.utils;\n\nimport java.sql.Connection;\nimport java.sql.ResultSet;\nimport java.sql.SQLException;\nimport java.sql.Statement;\n\npublic class UtilsDemo {\n    public static void main(String[] args) {\n        Connection conn = null;\n        Statement stmt = null;\n        ResultSet retSet = null;\n        try {\n            conn = Utils.getConnection();   \/\/ \u6570\u636e\u5e93\u8fde\u63a5\n            stmt = conn.createStatement();  \/\/ Sql\u6267\u884c\u5bf9\u8c61\n            retSet = stmt.executeQuery(\"select * from account\"); \/\/ \u6267\u884c\u67e5\u8be2\u8bed\u53e5\n            while (retSet.next()) {\n                int id = retSet.getInt(\"id\");\n                String uname = retSet.getString(\"uname\");\n                double balance = retSet.getDouble(\"balance\");\n                System.out.println(id + \"---\" + uname + \"---\" + balance);\n            }\n        } catch (SQLException e) {\n            e.printStackTrace();\n        } finally {\n            \/\/ \u91ca\u653e\u8d44\u6e90\n            Utils.releaseQureyObject(conn, stmt, retSet);\n        }\n    }\n}\n<\/code><\/pre>\n<h3>3.3 \u914d\u7f6e\u6587\u4ef6<\/h3>\n<pre><code class=\"language-txt line-numbers\">url=jdbc:mysql:\/\/example.domain.com:3306\/db?useSSL=false\nuser=username\npass=password\ndriver=com.mysql.jdbc.Driver\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u57fa\u672c\u6982\u5ff5\u4e0e\u6b65\u9aa4 1.1 \u521d\u59cb\u5316JDBC jdbc\u5b9a\u4e49\u7684\u662f\u4e00\u5957\u63a5\u53e3\uff0c\u6bcf\u79cd\u6570\u636e\u5e93\u7c7b\u578b\u90fd\u6709\u5404\u81ea\u7684\u5b9e\u73b0\uff0c\u6211\u4eec\u8c03\u7528\u7684\u90fd\u662f\u63a5\u53e3\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u5e76\u4e0d\u6e05\u695a\u8be5\u91c7\u7528\u54ea\u79cd\u5b9e\u4f8b\uff0c\u6240\u4ee5\u9700\u8981\u5148\u521d\u59cb\u5316\u3002 Class.forName(&#8220;com.mysql.jdbc.Driver&#8221;); 1.2 \u6570\u636e\u5e93\u8fde\u63a5\u5bf9\u8c61(Connection\u7c7b) String connectUrl = &#8220;jdbc:mysql:\/\/example.domain.com:3306\/db3?useSSL=false&#8221;; Connection conn = DriverManager.getConnection(connectUrl, &#8220;user&#8221;, &#8220;password&#8221;); 1.3 \u6267\u884cSQL(Statement\u7c7b) \u4e24\u4e2a\u91cd\u8981\u7684\u65b9\u6cd5\uff1a executeUpdate \u6267\u884cDML\u8bed\u53e5\u548cDDL\u8bed\u53e5\uff0c\u5373\u6570\u636e\u6216\u5e93\u8868\u7684\u589e\u5220\u6539\u64cd\u4f5c\uff1b executeQuery \u6267\u884cDQL\u8bed\u53e5\uff0c\u5373\u6570\u636e\u5e93\u7684\u67e5\u8be2\u64cd\u4f5c\uff1b String sql = &#8220;select * from xxx&#8221;; Statement stmt = conn.createStatement(); ResultSet resultSet = stmt.executeQuery(sql); while (resultSet.next()) { String uname = resultSet.getString(&#8220;column1&#8221;); int balance = resultSet.getInt(&#8220;column2&#8221;); System.out.println(uname + &#8220;&#8211;&#8221; + balance); [&hellip;]<\/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,69],"class_list":["post-1646","post","type-post","status-publish","format-standard","hentry","category-java","tag-jdbc","tag-69"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1646","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=1646"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1646\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=1646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=1646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=1646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}