{"id":1426,"date":"2022-03-27T11:36:14","date_gmt":"2022-03-27T03:36:14","guid":{"rendered":"http:\/\/www.zyhcoding.club\/?p=1426"},"modified":"2022-03-27T20:48:03","modified_gmt":"2022-03-27T12:48:03","slug":"jdbc","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2022\/03\/27\/jdbc\/","title":{"rendered":"\u4f7f\u7528JDBC\u8fde\u63a5\u6570\u636e\u5e93"},"content":{"rendered":"<p>\u4f7f\u7528\u4e4b\u524d\u9700\u8981\u5728IDEA\u4e0a\u8fde\u63a5mysql\uff0c\u7136\u540e\u5bfc\u5165jar\u5305<code>mysql-connector-java-8.0.20.jar<\/code>\uff0c\u6267\u884c\u7684\u57fa\u672c\u6b65\u9aa4\u4e3a\uff1a<\/p>\n<ol>\n<li>\u6ce8\u518c\u9a71\u52a8<\/li>\n<li>\u83b7\u53d6\u8fde\u63a5\u5bf9\u8c61<\/li>\n<li>\u521b\u5efaSQL\u8bed\u53e5<\/li>\n<li>\u521b\u5efa\u6267\u884cSQL\u8bed\u53e5\u7684Statement\u5bf9\u8c61<\/li>\n<li>\u6267\u884cSQL\u8bed\u53e5<\/li>\n<li>\u91ca\u653e\u8d44\u6e90<\/li>\n<\/ol>\n<p>6.0\u7248\u672c\u4ee5\u4e0a\u4e0d\u9700\u8981\u624b\u52a8\u52a0\u8f7d\u9a71\u52a8\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<\/p>\n<pre><code class=\"language-java\">\/\/1. \u901a\u8fc7DriverManager\u6765\u83b7\u5f97\u6570\u636e\u5e93\u8fde\u63a5\ntry (Connection connection = DriverManager.getConnection(&quot;jdbc:mysql:\/\/localhost:3306\/study&quot;, &quot;root&quot;, &quot;123456&quot;);\n     \/\/2. \u521b\u5efa\u4e00\u4e2a\u7528\u4e8e\u6267\u884cSQL\u7684Statement\u5bf9\u8c61\n     Statement statement = connection.createStatement()){   \/\/\u6ce8\u610f\u524d\u4e24\u6b65\u90fd\u653e\u5728try()\u4e2d\uff0c\u56e0\u4e3a\u5728\u6700\u540e\u9700\u8981\u91ca\u653e\u8d44\u6e90\uff01\n    \/\/3. \u6267\u884cSQL\u8bed\u53e5\uff0c\u5e76\u5f97\u5230\u7ed3\u679c\u96c6\n    ResultSet set = statement.executeQuery(&quot;select * from \u8868\u540d&quot;);\n    \/\/4. \u67e5\u770b\u7ed3\u679c\n    while (set.next()){\n        ...\n    }\n}catch (SQLException e){\n    e.printStackTrace();\n}\n\/\/5. \u91ca\u653e\u8d44\u6e90\uff0ctry-with-resource\u8bed\u6cd5\u4f1a\u81ea\u52a8\u5e2e\u52a9\u6211\u4eecclose<\/code><\/pre>\n<p>URL\u8868\u793a\u6240\u8fde\u63a5\u6570\u636e\u5e93\u7684\u5730\u5740\u3002\u8fd9\u91cc\u7684\u503c\u662f<code>jdbc:mysql:\/\/localhost:3306\/study<\/code>\uff0c\u8868\u793a\u9009\u62e9\u672c\u5730\u6570\u636e\u5e93\u7cfb\u7edf\u7684study\u6570\u636e\u5e93\uff0c\u9009\u62e9\u5176\u4ed6\u6570\u636e\u5e93\u65f6\u5c31\u5c06<code>study<\/code>\u6539\u4e3a\u5176\u5b83\u7684\u6570\u636e\u5e93\u3002<\/p>\n<h5>DriverManager<\/h5>\n<p>\u6b64\u7c7b\u662f\u9a71\u52a8\u7ba1\u7406\u8005\u7684\u610f\u601d\uff0c\u901a\u8fc7\u8be5\u7c7b\u6765\u52a0\u8f7d\u9a71\u52a8Driver\uff0c\u901a\u8fc7\u8bed\u53e5<code>Class.forName(&quot;com.mysql.cj.jdbc.Driver&quot;)<\/code>\u6765\u5c06Driver\u7c7b\u52a0\u8f7d\u8fdb\u5185\u5b58\u3002<\/p>\n<p>\u4e0a\u8ff0\u8bed\u53e5\u6267\u884c\u65f6\uff0cJVM\u4f1a\u628a\u8be5\u7c7b\u52a0\u8f7d\uff0c\u7136\u540e\u6267\u884c\u4e00\u6b21\u7c7b\u5185\u90e8\u7684\u9759\u6001\u4ee3\u7801\u5757\uff1a<\/p>\n<pre><code class=\"language-java\">\/\/ static code block in Class Driver\nstatic {\n    try {\n        DriverManager.registerDriver(new Driver());\n    } catch (SQLException var1) {\n        throw new RuntimeException(&quot;Can&#039;t register driver!&quot;);\n    }\n}<\/code><\/pre>\n<p>\u6267\u884c\u8be5\u4ee3\u7801\u5757\u4f1a\u901a\u8fc7<code>DriverManager.registerDriver(new Driver());<\/code>\u6765\u6ce8\u518c\u9a71\u52a8\u3002<code>DriverManager<\/code>\u6ce8\u518c\u65f6\u4f1a\u5c06\u8be5<code>Driver<\/code>\u5c01\u88c5\u6210<code>DriverInfo<\/code>\u5e76\u52a0\u5165\u5230<code>DriverManager<\/code>\u7c7b\u7684<code>registeredDrivers<\/code>\u4e2d\uff0c\u8fd9\u662f\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u5217\u8868\uff0c\u8868\u793a\u5df2\u7ecf\u52a0\u8f7d\u7684\u9a71\u52a8\u3002<\/p>\n<pre><code class=\"language-JAVA\">    private final static CopyOnWriteArrayList&lt;DriverInfo&gt; registeredDrivers = new CopyOnWriteArrayList&lt;&gt;();\n    public static void registerDriver(java.sql.Driver driver,\n            DriverAction da)\n        throws SQLException {\n\n        \/* Register the driver if it has not already been added to our list *\/\n        if (driver != null) {\n            registeredDrivers.addIfAbsent(new DriverInfo(driver, da));\n        } else {\n            \/\/ This is for compatibility with the original DriverManager\n            throw new NullPointerException();\n        }\n\n        println(&quot;registerDriver: &quot; + driver);\n\n    }<\/code><\/pre>\n<h5>Connection\u5bf9\u8c61<\/h5>\n<p>\u901a\u8fc7Mannager\u5bf9\u8c61\u6765\u83b7\u53d6\u6570\u636e\u5e93\u7684\u8fde\u63a5\u5bf9\u8c61Connection\uff0c\u9700\u8981\u63d0\u4f9b\u4e09\u4e2a\u53c2\u6570url\uff0cuser\uff0cpassword\u3002<\/p>\n<pre><code class=\"language-java\">    public static Connection getConnection(String url,\n        String user, String password) throws SQLException {\n        java.util.Properties info = new java.util.Properties();\n\n        if (user != null) {\n            info.put(&quot;user&quot;, user);\n        }\n        if (password != null) {\n            info.put(&quot;password&quot;, password);\n        }\n\n        return (getConnection(url, info, Reflection.getCallerClass()));\n    }<\/code><\/pre>\n<h5>Statement\u5bf9\u8c61\u548cResultSet\u5bf9\u8c61<\/h5>\n<p>Statement\u662f\u6570\u636e\u5e93\u8bed\u53e5\u5bf9\u8c61\uff0c\u901a\u8fc7\u6570\u636e\u5e93\u8fde\u63a5\u5bf9\u8c61\u6765\u521b\u5efa\u4e00\u4e2aStatement\u5bf9\u8c61\uff0cStatement\u5bf9\u8c61\u7528\u6765\u6267\u884c\u6570\u636e\u5e93\u8bed\u53e5\uff0c\u5e76\u5c06\u6267\u884c\u7ed3\u679c\u8fd4\u56de\u5230\u4e00\u4e2aResultSet\u4e2d\uff0c\u672c\u8d28\u662f\u4e00\u4e2a\u96c6\u5408\u3002<\/p>\n<pre><code class=\"language-java\">Statement statement = connection.createStatement();   \/\/\n\nResultSet set = statement.executeQuery(String sql);       \/\/ \u8be5\u65b9\u6cd5\u6267\u884csql\u8bed\u53e5\n\n\/\/ Resultset\u7684\u4f7f\u7528\u65b9\u6cd5   \u8fd4\u56de\u7684\u67e5\u8be2\u7ed3\u679c\u662f\u4e00\u5f20\u8868\uff0cset\u91cc\u7684\u6307\u9488\u4e00\u5f00\u59cb\u6307\u5411\u8868\u5934\nwhile (set.next()) {      \/\/ \u6bcf\u6267\u884c\u4e00\u6b21next\u51fd\u6570\uff0c\u6307\u9488\u5c31\u4f1a\u5f80\u4e0b\u79fb\u52a8\u4e00\u884c\u3002\n    \/\/ get\u51fd\u6570\u6709\u4e24\u79cd\u53c2\u6570\u7c7b\u578b\uff1aint\u548cstring\uff0c\u5206\u522b\u8868\u793a\u6240\u9009\u5217\u7684\u4e0b\u6807\u548c\u5217\u540d\n    System.out.println(set.getInt(1));          \/\/ \u9009\u7b2c\u4e00\u5217\uff0c\u56e0\u7b2c\u4e00\u5217\u4e3a\u6574\u6570\uff0c\u56e0\u6b64\u4f7f\u7528getInt\n    System.out.println(set.getString(&quot;name&quot;));    \/\/ \u9009\u62e9\u5217\u540d\u4e3a name \u7684\u90a3\u4e00\u5217\n    System.out.println(set.getString(3));        \/\/ \u9009\u62e9\u7b2c\u4e09\u5217\uff0c\u4e3a\u5b57\u7b26\u4e32\uff0c\u56e0\u6b64\u7528getString\n}\n\n\/\/ \u6279\u5904\u7406\uff0c\u4e00\u6b21\u60f3\u8981\u6267\u884c\u591a\u6761\u6570\u636e\u5e93\u8bed\u53e5\u65f6\uff0c\u5c06\u591a\u6761\u8bed\u53e5\u5408\u6210\u4e3a\u4e00\u6761\u53bb\u6267\u884c\u4f1a\u66f4\u5feb\uff0c\u6570\u636e\u5e93\u53ea\u7528\u6267\u884c\u4e00\u6b21\u3002\nstatement.addBatch(&quot;insert into user values (&#039;f&#039;, 1234)&quot;);\nstatement.addBatch(&quot;insert into user values (&#039;e&#039;, 1234)&quot;);   \/\/\u6dfb\u52a0\u6bcf\u4e00\u6761\u6279\u5904\u7406\u8bed\u53e5\nstatement.executeBatch();   \/\/\u4e00\u8d77\u6267\u884c<\/code><\/pre>\n<p>\u4f7f\u7528Statement\u5bf9\u8c61\u6267\u884c\u65f6\u6709\u4e2a\u975e\u5e38\u4e25\u91cd\u7684\u95ee\u9898\u90a3\u5c31\u662f\u8fd9\u91cc\u7684SQL\u8bed\u53e5\u90fd\u662f\u9759\u6001\u7684\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4SQL\u6ce8\u5165\uff0c\u51fa\u73b0\u5b89\u5168\u95ee\u9898\u3002\u4f7f\u7528PreparedStatement\u5bf9\u8c61\u53ef\u4ee5\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\uff0c\u8be5\u5bf9\u8c61\u662f\u9884\u7f16\u8bd1SQL\u8bed\u53e5\u7684\u5bf9\u8c61\uff0c\u7ee7\u627f\u81eaStatement\u3002<\/p>\n<h5>SQL\u6ce8\u5165<\/h5>\n<p>\u6f14\u793a\uff1a<\/p>\n<pre><code class=\"language-java\">\n-- \u5148\u5728\u6570\u636e\u5e93\u4e2d\u65b0\u5efauser\u8868\nCREATE TABLE user(\n    username varchar(255) NOT NULL,\n    password varchar(255) NOT NULL,\n    PRIMARY KEY(username, password)\n    );\n\nINSERT INTO user values(\n    &#039;Test&#039;, &#039;123&#039;);\n\n\/\/ \u7136\u540e\u4f7f\u7528JDBC\u5c1d\u8bd5\u8fde\u63a5\u6570\u636e\u5e93\u5e76\u767b\u5f55\u4e3auser\ntry( Connection connection = DriverManager.getConnection(&quot;jdbc:mysql:\/\/localhost:3306\/study&quot;, &quot;root&quot;, &quot;123456&quot;);\n    Statement statement = connection.createStatement();\n    Scanner scanner = new Scanner(System.in);\n    ) {\n\n    ResultSet set = statement.executeQuery(&quot;SELECT * FROM user WHERE username=&#039;&quot; + scanner.next() + &quot;&#039; and &quot; + &quot;password=&#039;&quot; + scanner.next()+&quot;&#039;;&quot;);\n    while (set.next()) {\n        System.out.println(set.getString(&quot;username&quot;));\n        System.out.println(&quot;\u767b\u5f55\u6210\u529f&quot;);\n    }\n}catch (Exception e) {\n    e.printStackTrace();\n}<\/code><\/pre>\n<p>\u6b63\u786e\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\u65f6\u4f1a\u6b63\u786e\u767b\u5f55\uff0c\u4f46\u662f\u5f53\u628a\u5bc6\u7801\u6539\u4e3a<code>111&#039; or 1=1; -- <\/code>\u65f6\uff0c\u53d1\u73b0\u4e5f\u53ef\u4ee5\u6b63\u786e\u767b\u5f55\uff0c\u56e0\u4e3a\u8fd9\u91cc\u7684\u7279\u6b8a\u8f93\u5165\u201c\u9a97\u8fc7\u201d\u4e86\u6570\u636e\u5e93\u7cfb\u7edf\u3002\u8fd9\u5c31\u662fSQL\u6ce8\u5165\u3002<\/p>\n<p>\u89e3\u51b3SQL\u6ce8\u5165\u7684\u65b9\u6cd5\u53ef\u4ee5\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n<ul>\n<li>\u6c38\u8fdc\u4e0d\u8981\u4fe1\u4efb\u7528\u6237\u7684\u8f93\u5165\u3002\u5bf9\u7528\u6237\u7684\u8f93\u5165\u8fdb\u884c\u6821\u9a8c\uff0c\u53ef\u4ee5\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6216\u9650\u5236\u957f\u5ea6\uff1b\u5bf9\u5355\u5f15\u53f7\u548c \u53cc&quot;-&quot;\u8fdb\u884c\u8f6c\u6362\u7b49\u3002<\/li>\n<li>\u6c38\u8fdc\u4e0d\u8981\u4f7f\u7528\u52a8\u6001\u62fc\u88c5sql\uff0c\u53ef\u4ee5\u4f7f\u7528\u53c2\u6570\u5316\u7684sql\u6216\u8005\u76f4\u63a5\u4f7f\u7528\u5b58\u50a8\u8fc7\u7a0b\u8fdb\u884c\u6570\u636e\u67e5\u8be2\u5b58\u53d6\u3002<\/li>\n<li>\u6c38\u8fdc\u4e0d\u8981\u4f7f\u7528\u7ba1\u7406\u5458\u6743\u9650\u7684\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u4e3a\u6bcf\u4e2a\u5e94\u7528\u4f7f\u7528\u5355\u72ec\u7684\u6743\u9650\u6709\u9650\u7684\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/li>\n<li>\u4e0d\u8981\u628a\u673a\u5bc6\u4fe1\u606f\u76f4\u63a5\u5b58\u653e\uff0c\u52a0\u5bc6\u6216\u8005hash\u6389\u5bc6\u7801\u548c\u654f\u611f\u7684\u4fe1\u606f\u3002<\/li>\n<li>\u5e94\u7528\u7684\u5f02\u5e38\u4fe1\u606f\u5e94\u8be5\u7ed9\u51fa\u5c3d\u53ef\u80fd\u5c11\u7684\u63d0\u793a\uff0c\u6700\u597d\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u9519\u8bef\u4fe1\u606f\u5bf9\u539f\u59cb\u9519\u8bef\u4fe1\u606f\u8fdb\u884c\u5305\u88c5<\/li>\n<li>sql\u6ce8\u5165\u7684\u68c0\u6d4b\u65b9\u6cd5\u4e00\u822c\u91c7\u53d6\u8f85\u52a9\u8f6f\u4ef6\u6216\u7f51\u7ad9\u5e73\u53f0\u6765\u68c0\u6d4b\uff0c\u8f6f\u4ef6\u4e00\u822c\u91c7\u7528sql\u6ce8\u5165\u68c0\u6d4b\u5de5\u5177jsky\uff0c\u7f51\u7ad9\u5e73\u53f0\u5c31\u6709\u4ebf\u601d\u7f51\u7ad9\u5b89\u5168\u5e73\u53f0\u68c0\u6d4b\u5de5\u5177\u3002MDCSOFT SCAN\u7b49\u3002\u91c7\u7528MDCSOFT-IPS\u53ef\u4ee5\u6709\u6548\u7684\u9632\u5fa1SQL\u6ce8\u5165\uff0cXSS\u653b\u51fb\u7b49\u3002<\/li>\n<\/ul>\n<h5>\u4f7f\u7528PreparedStatement<\/h5>\n<p>\u4e00\u4e2a<code>PreparedStatement<\/code>\u5bf9\u8c61\u662f\u9884\u7f16\u8bd1SQL\u8bed\u53e5\u7684\u5bf9\u8c61\uff0c\u5373\u5728\u521b\u5efa\u65f6\u5c31\u8981\u628asql\u8bed\u53e5\u5199\u597d\u3002\u7f16\u5199SQL\u8bed\u53e5\u65f6\uff0c\u4e0d\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5\uff0c\u800c\u662f\u4f7f\u7528<code>?<\/code>\u5360\u4f4d\u7b26\u6765\u4ee3\u66ff\u53d8\u91cf\u3002\u7136\u540e\u5728\u771f\u6b63\u6267\u884c\u4e4b\u524d\u518d\u7ed9\u5360\u4f4d\u7b26\u8d4b\u503c\u3002<\/p>\n<p>PrepareStatement\u5bf9\u8c61\u5728\u521b\u5efa\u65f6\u5c31\u8981\u6307\u660e\u8981\u6267\u884c\u7684SQL\u8bed\u53e5\u7c7b\u578b\u3002\u7136\u540e\u7528<code>?<\/code>\u4ee3\u66ff\u53d8\u91cf\uff0c\u5e76\u7528<code>setString(int Index, String)<\/code>\u65b9\u6cd5\u6765\u7ed9\u5360\u4f4d\u7b26\u8d4b\u503c\uff0c\u6700\u540e\u6267\u884c\u3002<\/p>\n<pre><code class=\"language-java\">\ntry( Connection connection = DriverManager.getConnection(&quot;jdbc:mysql:\/\/localhost:3306\/study&quot;, &quot;root&quot;, &quot;123456&quot;);\n             PreparedStatement statement = connection.prepareStatement(&quot;SELECT * FROM user WHERE username=? AND password=?&quot;);\n             Scanner scanner = new Scanner(System.in);\n                ) {\n\n            statement.setString(1, scanner.nextLine());\n            statement.setString(2, scanner.nextLine());\n            ResultSet set = statement.executeQuery();\n            while (set.next()) {\n                System.out.println(set.getString(&quot;username&quot;));\n                System.out.println(&quot;\u767b\u5f55\u6210\u529f&quot;);\n            }\n        }catch (Exception e) {\n            e.printStackTrace();\n        }<\/code><\/pre>\n<p>\u518d\u6b21\u6d4b\u8bd5\u53d1\u73b0\u539f\u6765\u7684\u8f93\u5165\u5df2\u7ecf\u4e0d\u80fd\u201c\u9a97\u8fc7\u201d\u6570\u636e\u5e93\u4e86\u3002<\/p>\n<h5>JDBC\u7ba1\u7406\u4e8b\u52a1<\/h5>\n<p>\u548c\u6570\u636e\u5e93\u7ba1\u7406\u4e8b\u52a1\u4e00\u6837\u7684\u3002<\/p>\n<pre><code class=\"language-java\">try ( Connection connection = DriverManager.getConnection(&quot;jdbc:mysql:\/\/localhost:3306\/study&quot;, &quot;root&quot;, &quot;123456&quot;);\n              Statement statement = connection.createStatement();\n                ) {\n            connection.setAutoCommit(false);       \/\/ \u628a\u8fde\u63a5\u7684\u81ea\u52a8\u63d0\u4ea4\u5173\u6389\n            Savepoint sp = connection.setSavepoint();  \/\/ \u8bbe\u7f6e\u56de\u6eda\u70b9\n            statement.executeUpdate(&quot;INSERT INTO user VALUES(&#039;a&#039;, 123)&quot;);\n            statement.executeUpdate(&quot;INSERT INTO user VALUES(&#039;b&#039;, 123)&quot;);\n            statement.executeUpdate(&quot;INSERT INTO user VALUES(&#039;c&#039;, 123)&quot;);\n\n            connection.rollback(sp);       \/\/ \u56de\u6eda\u5230\u56de\u6eda\u70b9\n            connection.commit();         \/\/ \u624b\u52a8\u63d0\u4ea4\n        }catch (Exception e) {\n            e.printStackTrace();\n        }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528\u4e4b\u524d\u9700\u8981\u5728IDEA\u4e0a\u8fde\u63a5mysql\uff0c\u7136\u540e\u5bfc\u5165jar\u5305mysql-connector-java-8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[77],"tags":[],"_links":{"self":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1426"}],"collection":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/comments?post=1426"}],"version-history":[{"count":15,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1426\/revisions"}],"predecessor-version":[{"id":1432,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1426\/revisions\/1432"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=1426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=1426"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=1426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}