{"id":1628,"date":"2020-11-28T15:21:56","date_gmt":"2020-11-28T07:21:56","guid":{"rendered":"https:\/\/www.yusian.com\/blog\/?p=1628"},"modified":"2020-11-28T15:21:56","modified_gmt":"2020-11-28T07:21:56","slug":"java%e6%a8%a1%e6%8b%9f%e7%94%a8%e6%88%b7%e5%90%8d%e5%af%86%e7%a0%81%e7%99%bb%e5%bd%95%e9%aa%8c%e8%af%81jdbc","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/java\/2020\/11\/28\/1521561628.html","title":{"rendered":"Java\u6a21\u62df\u7528\u6237\u540d\u5bc6\u7801\u767b\u5f55\u9a8c\u8bc1(JDBC)"},"content":{"rendered":"<h3>0\u3001\u793a\u4f8b\u7a0b\u5e8f\u8bf4\u660e<\/h3>\n<ul>\n<li>\u6f14\u793a\u767b\u5f55\u9a8c\u8bc1\uff0c\u7528\u6237\u901a\u8fc7\u7ec8\u7aef\u8f93\u5165\u7528\u6237\u540d\u4e0e\u5bc6\u7801\uff0c\u7a0b\u5e8f\u8fde\u63a5\u6570\u636e\u5e93\u67e5\u8be2\u5408\u6cd5\u6027\uff1b<\/li>\n<li>\u57fa\u672c\u6b65\u9aa4\uff1a\n<ul>\n<li>\u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\u5bf9\u8c61<\/li>\n<li>\u83b7\u53d6SQL\u6267\u884c\u5bf9\u8c61<\/li>\n<li>\u6267\u884cSQL\u83b7\u53d6\u7ed3\u679c\u5bf9\u8c61<\/li>\n<li>\u5904\u7406\u7ed3\u679c<\/li>\n<\/ul>\n<\/li>\n<li>\u6267\u884cSQL\u8bed\u53e5\u6709\u4e24\u4e2a\u7c7b\uff0cStatement\u4e0ePrepareStatement\uff0c\u540e\u8005\u662f\u524d\u8005\u7684\u5b50\u7c7b\uff0c\u540e\u8005\u6709\u9632SQL\u6d41\u5165\u7684\u529f\u80fd<\/li>\n<\/ul>\n<h3>1\u3001\u5c01\u88c5\u5de5\u5177\u7c7b\u64cd\u4f5c\u6570\u636e\u5e93<\/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    private static String url;  \/\/ \u6570\u636e\u5e93\u8fde\u63a5url\n    private static String user; \/\/ \u6570\u636e\u5e93\u7528\u6237\u540d\n    private static String pass; \/\/ \u6570\u636e\u5e93\u5bc6\u7801\n    private static String driver;\n\n    \/**\n     * \u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u521d\u59cb\u5316\u53c2\u6570\u503c\n     *\/\n    static {\n        \/\/ \u901a\u8fc7ClassLoader\u83b7\u53d6\u914d\u7f6e\u6587\u4ef6\u7684\u8def\u5f84\n        ClassLoader loader = Utils.class.getClassLoader();\n        URL jdbc = loader.getResource(\"jdbc.properties\");\n        try {\n            \/\/ \u901a\u8fc7Properties\u7c7b\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u6570\u636e\uff0c\u8d4b\u503c\u7ed9\u9759\u6001\u53d8\u91cf\n            Properties prop = new Properties();\n            prop.load(new FileReader(jdbc.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     * \u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\n     * @return \u6570\u636e\u5e93\u8fde\u63a5\u5bf9\u8c61Connection\n     * @throws SQLException\n     *\/\n    public static Connection getConnection() throws SQLException {\n        \/\/ \u6ce8\u518cMySQL\u9a71\u52a8\u7c7b\n        try {\n            Class.forName(driver);\n        } catch (ClassNotFoundException e) {\n            e.printStackTrace();\n        }\n        \/\/ \u83b7\u53d6MySQL\u8fde\u63a5\u5bf9\u8c61\n        return DriverManager.getConnection(url, user, pass);\n    }\n\n    \/**\n     * \u91ca\u653e\u66f4\u65b0\u7c7b\u6570\u636e\u5e93\u64cd\u4f5c\u5bf9\u8c61\n     * @param conn \u6570\u636e\u5e93\u8fde\u63a5\u5bf9\u8c61\n     * @param stmt SQL\u8bed\u53e5\u6267\u884c\u5bf9\u8c61\n     *\/\n    public static void releaseExcuteUpdate(Connection conn, Statement stmt) {\n        releaseCloseableObject(stmt);\n        releaseCloseableObject(conn);\n    }\n\n    \/**\n     * \u91ca\u653e\u67e5\u8be2\u7c7b\u6570\u636e\u5e93\u64cd\u4f5c\u5bf9\u8c61\n     * @param conn \u6570\u636e\u5e93\u8fde\u63a5\u5bf9\u8c61\n     * @param stmt SQL\u6267\u884c\u5bf9\u8c61\n     * @param retSet \u67e5\u8be2\u7ed3\u679c\n     *\/\n    public static void releaseExecuteQuery(Connection conn, Statement stmt, ResultSet retSet) {\n        releaseCloseableObject(retSet);\n        releaseExcuteUpdate(conn, stmt);\n    }\n    \/**\n     * \u91ca\u653e\u8d44\u6e90\n     * @param object \u9700\u8981\u91ca\u653e\u7684\u8d44\u6e90\u5bf9\u8c61\n     *\/\n    private 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\n<\/code><\/pre>\n<p><!--more--><\/p>\n<h3>2\u3001\u5b57\u7b26\u4e32\u62fc\u63a5SQL\u8bed\u53e5\u7684\u65b9\u5f0f\u5b9e\u73b0\u67e5\u8be2<\/h3>\n<pre><code class=\"language-java line-numbers\">\/**\n * \u6570\u636e\u5e93\u9a8c\u8bc1\u7528\u6237\u540d\u53ca\u5bc6\u7801\u7684\u6b63\u786e\u6027\n *\n * @param username \u7528\u6237\u540d\n * @param password \u5bc6\u7801\n * @return \u9a8c\u8bc1\u7ed3\u679c\n *\/\nprivate static boolean login(String username, String password) {\n    Connection conn = null;\n    Statement stmt = null;\n    ResultSet retSet = null;\n    \/\/ \u62fc\u63a5\u67e5\u8be2\u8bed\u53e5SQL\n    String sql = \"select * from user where username = '\" + username + \"' and password = '\" + password + \"'\";\n    try {\n        conn = Utils.getConnection();\n        stmt = conn.createStatement();\n        \/\/ \u83b7\u53d6\u67e5\u8be2\u7ed3\u679c\uff0c\u6709\u7ed3\u679c\u8868\u660e\u9a8c\u8bc1\u6210\u529f\uff0c\u65e0\u5219\u5931\u8d25\n        retSet = stmt.executeQuery(sql);\n        return retSet.next();\n    } catch (SQLException e) {\n        e.printStackTrace();\n    } finally {\n        \/\/ \u91ca\u653e\u8d44\u6e90\n        Utils.releaseExecuteQuery(conn, stmt, retSet);\n    }\n    return false;\n}\n<\/code><\/pre>\n<h3>3\u3001\u9632SQL\u6ce8\u5165\u7684\u5b9e\u73b0\u65b9\u5f0f<\/h3>\n<pre><code class=\"language-java line-numbers\">\/**\n * \u6570\u636e\u5e93\u9a8c\u8bc1\u7528\u6237\u540d\u5bc6\u7801\u7684\u6b63\u786e\u6027\uff0c\u9632SQL\u6d41\u5165\u65b9\u6cd5\n * @param username \u7528\u6237\u540d\n * @param password \u5bc6\u7801\n * @return \u9a8c\u8bc1\u7ed3\u679c\n *\/\nprivate static boolean login2(String username, String password) {\n    Connection conn = null;\n    PreparedStatement pstmt = null;\n    ResultSet retSet = null;\n    try {\n        conn = Utils.getConnection();\n        \/\/ \u8fd9\u91cc\u662f\u4e0eStatement\u6700\u672c\u8d28\u7684\u533a\u522b\u70b9\uff0cSQL\u4e2d\u7684\u53d8\u91cf\u7528?\u5360\u4f4d\uff0c\u901a\u8fc7setXxxx\u65b9\u6cd5\u8d4b\u503c\u6765\u89c4\u907fSQL\u6ce8\u5165\n        pstmt = conn.prepareStatement(\"select * from user where username = ? and password = ?\");\n        \/\/ \u7ed9SQL\u4e2d\u5360\u4f4d\u7b26\u8d4b\u503c\uff0c\u5e8f\u53f7\u4ece1\u5f00\u59cb\n        pstmt.setString(1, username);\n        pstmt.setString(2, password);\n        retSet = pstmt.executeQuery();\n        return retSet.next();\n    } catch (SQLException e) {\n        e.printStackTrace();\n    } finally {\n        Utils.releaseExecuteQuery(conn, pstmt, retSet);\n    }\n    return false;\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>0\u3001\u793a\u4f8b\u7a0b\u5e8f\u8bf4\u660e \u6f14\u793a\u767b\u5f55\u9a8c\u8bc1\uff0c\u7528\u6237\u901a\u8fc7\u7ec8\u7aef\u8f93\u5165\u7528\u6237\u540d\u4e0e\u5bc6\u7801\uff0c\u7a0b\u5e8f\u8fde\u63a5\u6570\u636e\u5e93\u67e5\u8be2\u5408\u6cd5\u6027\uff1b \u57fa\u672c\u6b65\u9aa4\uff1a \u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\u5bf9\u8c61 \u83b7\u53d6SQL\u6267\u884c\u5bf9\u8c61 \u6267\u884cSQL\u83b7\u53d6\u7ed3\u679c\u5bf9\u8c61 \u5904\u7406\u7ed3\u679c \u6267\u884cSQL\u8bed\u53e5\u6709\u4e24\u4e2a\u7c7b\uff0cStatement\u4e0ePrepareStatement\uff0c\u540e\u8005\u662f\u524d\u8005\u7684\u5b50\u7c7b\uff0c\u540e\u8005\u6709\u9632SQL\u6d41\u5165\u7684\u529f\u80fd 1\u3001\u5c01\u88c5\u5de5\u5177\u7c7b\u64cd\u4f5c\u6570\u636e\u5e93 package com.yusian.utils; import java.io.FileReader; import java.io.IOException; import java.net.URL; import java.sql.*; import java.util.Properties; public class Utils { private static String url; \/\/ \u6570\u636e\u5e93\u8fde\u63a5url private static String user; \/\/ \u6570\u636e\u5e93\u7528\u6237\u540d private static String pass; \/\/ \u6570\u636e\u5e93\u5bc6\u7801 private static String driver; \/** * \u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u521d\u59cb\u5316\u53c2\u6570\u503c *\/ static { \/\/ \u901a\u8fc7ClassLoader\u83b7\u53d6\u914d\u7f6e\u6587\u4ef6\u7684\u8def\u5f84 [&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,22,293,294],"class_list":["post-1628","post","type-post","status-publish","format-standard","hentry","category-java","tag-jdbc","tag-mysql","tag-sql","tag-294"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1628","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=1628"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1628\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=1628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=1628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=1628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}