{"id":183750,"date":"2024-11-10T07:25:16","date_gmt":"2024-11-09T23:25:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/183750\/"},"modified":"2024-11-10T07:25:17","modified_gmt":"2024-11-09T23:25:17","slug":"%e4%bd%bf%e7%94%a8java%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9f%a5%e8%a9%a2%ef%bc%8c%e7%b5%b1%e8%a8%88%e8%a1%8c%e6%95%b8-java-%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9f%a5%e8%a9%a2%e7%b5%b1%e8%a8%88","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/183750\/","title":{"rendered":"\u4f7f\u7528Java\u9032\u884c\u6578\u64da\u5eab\u67e5\u8a62\uff0c\u7d71\u8a08\u884c\u6578 (java \u6578\u64da\u5eab\u67e5\u8a62\u7d71\u8a08\u884c\u6578)"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8java%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9f%a5%e8%a9%a2%ef%bc%8c%e7%b5%b1%e8%a8%88%e8%a1%8c%e6%95%b8-QTfPsgpuZH\">\u4f7f\u7528Java\u9032\u884c\u6578\u64da\u5eab\u67e5\u8a62\uff0c\u7d71\u8a08\u884c\u6578<\/h1>\n<p>\u5728\u73fe\u4ee3\u61c9\u7528\u7a0b\u5e8f\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u4f7f\u7528\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u666e\u904d\u3002Java\u4f5c\u70ba\u4e00\u7a2e\u5ee3\u6cdb\u4f7f\u7528\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u65b9\u5f0f\u4f86\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Java\u9032\u884c\u6578\u64da\u5eab\u67e5\u8a62\u4e26\u7d71\u8a08\u884c\u6578\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u7ba1\u7406\u548c\u5206\u6790\u6578\u64da\u3002<\/p>\n<h2 id=\"java%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e9%80%a3%e6%8e%a5-QTfPsgpuZH\">Java\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5<\/h2>\n<p>\u5728\u9032\u884c\u6578\u64da\u5eab\u67e5\u8a62\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5efa\u7acb\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002Java\u63d0\u4f9b\u4e86JDBC\uff08Java Database Connectivity\uff09API\uff0c\u9019\u662f\u4e00\u500b\u6a19\u6e96\u7684API\uff0c\u7528\u65bc\u9023\u63a5\u548c\u64cd\u4f5c\u5404\u7a2e\u6578\u64da\u5eab\u3002\u4ee5\u4e0b\u662f\u5efa\u7acb\u6578\u64da\u5eab\u9023\u63a5\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<pre><code>import java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.SQLException;\n\npublic class DatabaseConnection {\n    public static Connection getConnection() {\n        Connection connection = null;\n        try {\n            \/\/ \u52a0\u8f09JDBC\u9a45\u52d5\n            Class.forName(\"com.mysql.cj.jdbc.Driver\");\n            \/\/ \u5efa\u7acb\u9023\u63a5\n            connection = DriverManager.getConnection(\"jdbc:mysql:\/\/localhost:3306\/your_database\", \"username\", \"password\");\n        } catch (ClassNotFoundException | SQLException e) {\n            e.printStackTrace();\n        }\n        return connection;\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e5%9f%b7%e8%a1%8c%e6%9f%a5%e8%a9%a2%e4%b8%a6%e7%b5%b1%e8%a8%88%e8%a1%8c%e6%95%b8-QTfPsgpuZH\">\u57f7\u884c\u67e5\u8a62\u4e26\u7d71\u8a08\u884c\u6578<\/h2>\n<p>\u4e00\u65e6\u5efa\u7acb\u4e86\u6578\u64da\u5eab\u9023\u63a5\uff0c\u5c31\u53ef\u4ee5\u57f7\u884c\u67e5\u8a62\u4e26\u7d71\u8a08\u7d50\u679c\u7684\u884c\u6578\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Java\u57f7\u884cSQL\u67e5\u8a62\u4e26\u7372\u53d6\u7d50\u679c\u96c6\u7684\u884c\u6578\uff1a<\/p>\n<pre><code>import java.sql.Connection;\nimport java.sql.ResultSet;\nimport java.sql.SQLException;\nimport java.sql.Statement;\n\npublic class CountRows {\n    public static void main(String[] args) {\n        Connection connection = DatabaseConnection.getConnection();\n        Statement statement = null;\n        ResultSet resultSet = null;\n\n        try {\n            statement = connection.createStatement();\n            String sql = \"SELECT * FROM your_table\";\n            resultSet = statement.executeQuery(sql);\n\n            \/\/ \u7d71\u8a08\u884c\u6578\n            int rowCount = 0;\n            while (resultSet.next()) {\n                rowCount++;\n            }\n            System.out.println(\"\u884c\u6578: \" + rowCount);\n        } catch (SQLException e) {\n            e.printStackTrace();\n        } finally {\n            \/\/ \u95dc\u9589\u8cc7\u6e90\n            try {\n                if (resultSet != null) resultSet.close();\n                if (statement != null) statement.close();\n                if (connection != null) connection.close();\n            } catch (SQLException e) {\n                e.printStackTrace();\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e4%bd%bf%e7%94%a8sql%e8%aa%9e%e5%8f%a5%e7%9b%b4%e6%8e%a5%e7%8d%b2%e5%8f%96%e8%a1%8c%e6%95%b8-QTfPsgpuZH\">\u4f7f\u7528SQL\u8a9e\u53e5\u76f4\u63a5\u7372\u53d6\u884c\u6578<\/h2>\n<p>\u9664\u4e86\u5728Java\u4e2d\u624b\u52d5\u8a08\u7b97\u884c\u6578\u5916\uff0c\u9084\u53ef\u4ee5\u4f7f\u7528SQL\u8a9e\u53e5\u76f4\u63a5\u7372\u53d6\u884c\u6578\u3002\u9019\u6a23\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u5927\u578b\u6578\u64da\u96c6\u6642\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528SQL\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>String sql = \"SELECT COUNT(*) FROM your_table\";\nresultSet = statement.executeQuery(sql);\nif (resultSet.next()) {\n    int rowCount = resultSet.getInt(1);\n    System.out.println(\"\u884c\u6578: \" + rowCount);\n}\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-QTfPsgpuZH\">\u7d50\u8ad6<\/h2>\n<p>\u4f7f\u7528Java\u9032\u884c\u6578\u64da\u5eab\u67e5\u8a62\u548c\u7d71\u8a08\u884c\u6578\u662f\u4e00\u500b\u57fa\u672c\u4f46\u91cd\u8981\u7684\u6280\u80fd\u3002\u901a\u904eJDBC API\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u9023\u63a5\u5230\u6578\u64da\u5eab\uff0c\u57f7\u884c\u67e5\u8a62\u4e26\u7372\u53d6\u6240\u9700\u7684\u6578\u64da\u3002\u7121\u8ad6\u662f\u624b\u52d5\u8a08\u7b97\u884c\u6578\u9084\u662f\u4f7f\u7528SQL\u8a9e\u53e5\u76f4\u63a5\u7372\u53d6\uff0c\u9019\u4e9b\u65b9\u6cd5\u90fd\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u6258\u7ba1\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u548c\u6578\u64da\u5eab\u3002Server.HK\u63d0\u4f9b\u7a69\u5b9a\u7684\u670d\u52d9\u5668\u74b0\u5883\uff0c\u5e6b\u52a9\u60a8\u8f15\u9b06\u7ba1\u7406\u6578\u64da\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Java\u9032\u884c\u6578\u64da\u5eab\u67e5\u8a62\uff0c\u8f15\u9b06\u7d71\u8a08\u884c\u6578\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6548\u7387\uff0c\u52a9\u529b\u958b\u767c\u8005\u5feb\u901f\u7372\u53d6\u6240\u9700\u4fe1\u606f\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-183750","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183750","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=183750"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183750\/revisions"}],"predecessor-version":[{"id":183751,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183750\/revisions\/183751"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=183750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=183750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=183750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}