{"id":188156,"date":"2024-11-11T20:16:25","date_gmt":"2024-11-11T12:16:25","guid":{"rendered":"https:\/\/server.hk\/cnblog\/188156\/"},"modified":"2024-11-11T20:16:26","modified_gmt":"2024-11-11T12:16:26","slug":"%e5%9b%9b%e7%a8%ae%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e6%9f%a5%e8%a9%a2%ef%bc%9a%e4%bd%a0%e9%9c%80%e8%a6%81%e7%9f%a5%e9%81%93%e7%9a%84%e5%b8%b8%e7%94%a8%e6%96%b9%e5%bc%8f-%e5%9b%9b%e7%a8%ae","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/188156\/","title":{"rendered":"\u56db\u7a2e\u6578\u64da\u5eab\u9023\u63a5\u67e5\u8a62\uff1a\u4f60\u9700\u8981\u77e5\u9053\u7684\u5e38\u7528\u65b9\u5f0f (\u56db\u7a2e\u6578\u64da\u5eab\u9023\u63a5\u67e5\u8a62)"},"content":{"rendered":"<h1 id=\"%e5%9b%9b%e7%a8%ae%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e6%9f%a5%e8%a9%a2%ef%bc%9a%e4%bd%a0%e9%9c%80%e8%a6%81%e7%9f%a5%e9%81%93%e7%9a%84%e5%b8%b8%e7%94%a8%e6%96%b9%e5%bc%8f-btzShNnjgp\">\u56db\u7a2e\u6578\u64da\u5eab\u9023\u63a5\u67e5\u8a62\uff1a\u4f60\u9700\u8981\u77e5\u9053\u7684\u5e38\u7528\u65b9\u5f0f<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0c\u6578\u64da\u5eab\u7684\u4f7f\u7528\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u666e\u904d\u3002\u7121\u8ad6\u662f\u958b\u767c\u7db2\u7ad9\u3001\u61c9\u7528\u7a0b\u5e8f\u9084\u662f\u9032\u884c\u6578\u64da\u5206\u6790\uff0c\u4e86\u89e3\u5982\u4f55\u6709\u6548\u5730\u9023\u63a5\u548c\u67e5\u8a62\u6578\u64da\u5eab\u90fd\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u56db\u7a2e\u5e38\u7528\u7684\u6578\u64da\u5eab\u9023\u63a5\u67e5\u8a62\u65b9\u5f0f\uff0c\u5e6b\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u6578\u64da\u5eab\u3002<\/p>\n<h2 id=\"1-%e4%bd%bf%e7%94%a8-jdbc-%e9%80%a3%e6%8e%a5-mysql-%e6%95%b8%e6%93%9a%e5%ba%ab-btzShNnjgp\">1. \u4f7f\u7528 JDBC \u9023\u63a5 MySQL \u6578\u64da\u5eab<\/h2>\n<p>Java Database Connectivity (JDBC) \u662f Java \u63d0\u4f9b\u7684\u4e00\u7a2e API\uff0c\u7528\u65bc\u9023\u63a5\u548c\u64cd\u4f5c\u6578\u64da\u5eab\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 JDBC \u9023\u63a5 MySQL \u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<pre><code>import java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.ResultSet;\nimport java.sql.Statement;\n\npublic class MySQLConnection {\n    public static void main(String[] args) {\n        Connection connection = null;\n        try {\n            \/\/ \u52a0\u8f09 MySQL \u9a45\u52d5\n            Class.forName(\"com.mysql.cj.jdbc.Driver\");\n            \/\/ \u9023\u63a5\u6578\u64da\u5eab\n            connection = DriverManager.getConnection(\"jdbc:mysql:\/\/localhost:3306\/yourDatabase\", \"username\", \"password\");\n            Statement statement = connection.createStatement();\n            ResultSet resultSet = statement.executeQuery(\"SELECT * FROM yourTable\");\n            while (resultSet.next()) {\n                System.out.println(resultSet.getString(\"columnName\"));\n            }\n        } catch (Exception e) {\n            e.printStackTrace();\n        } finally {\n            try {\n                if (connection != null) connection.close();\n            } catch (Exception e) {\n                e.printStackTrace();\n            }\n        }\n    }\n}<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u9996\u5148\u52a0\u8f09 MySQL \u9a45\u52d5\uff0c\u7136\u5f8c\u4f7f\u7528 DriverManager \u9023\u63a5\u5230\u6578\u64da\u5eab\uff0c\u6700\u5f8c\u57f7\u884c\u67e5\u8a62\u4e26\u8655\u7406\u7d50\u679c\u3002<\/p>\n<h2 id=\"2-%e4%bd%bf%e7%94%a8-php-%e9%80%a3%e6%8e%a5-postgresql-%e6%95%b8%e6%93%9a%e5%ba%ab-btzShNnjgp\">2. \u4f7f\u7528 PHP \u9023\u63a5 PostgreSQL \u6578\u64da\u5eab<\/h2>\n<p>PHP \u662f\u4e00\u7a2e\u5ee3\u6cdb\u4f7f\u7528\u7684\u958b\u6e90\u8173\u672c\u8a9e\u8a00\uff0c\u7279\u5225\u9069\u5408\u65bc Web \u958b\u767c\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 PHP \u9023\u63a5 PostgreSQL \u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\n$conn = pg_connect(\"host=localhost dbname=yourDatabase user=username password=password\");\nif (!$conn) {\n    echo \"\u7121\u6cd5\u9023\u63a5\u5230\u6578\u64da\u5eab\";\n    exit;\n}\n$result = pg_query($conn, \"SELECT * FROM yourTable\");\nwhile ($row = pg_fetch_assoc($result)) {\n    echo $row['columnName'] . \"&lt;br&gt;\";\n}\npg_close($conn);\n?&gt;<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 pg_connect \u51fd\u6578\u9023\u63a5\u5230 PostgreSQL \u6578\u64da\u5eab\uff0c\u7136\u5f8c\u57f7\u884c\u67e5\u8a62\u4e26\u8f38\u51fa\u7d50\u679c\u3002<\/p>\n<h2 id=\"3-%e4%bd%bf%e7%94%a8-python-%e9%80%a3%e6%8e%a5-sqlite-%e6%95%b8%e6%93%9a%e5%ba%ab-btzShNnjgp\">3. \u4f7f\u7528 Python \u9023\u63a5 SQLite \u6578\u64da\u5eab<\/h2>\n<p>SQLite \u662f\u4e00\u7a2e\u8f15\u91cf\u7d1a\u7684\u6578\u64da\u5eab\uff0c\u9069\u5408\u7528\u65bc\u5c0f\u578b\u61c9\u7528\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 Python \u9023\u63a5 SQLite \u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import sqlite3\n\n# \u9023\u63a5\u5230 SQLite \u6578\u64da\u5eab\nconnection = sqlite3.connect('yourDatabase.db')\ncursor = connection.cursor()\n\n# \u57f7\u884c\u67e5\u8a62\ncursor.execute(\"SELECT * FROM yourTable\")\nrows = cursor.fetchall()\nfor row in rows:\n    print(row)\n\n# \u95dc\u9589\u9023\u63a5\nconnection.close()<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 sqlite3 \u6a21\u584a\u9023\u63a5\u5230 SQLite \u6578\u64da\u5eab\uff0c\u57f7\u884c\u67e5\u8a62\u4e26\u8f38\u51fa\u6240\u6709\u7d50\u679c\u3002<\/p>\n<h2 id=\"4-%e4%bd%bf%e7%94%a8-node-js-%e9%80%a3%e6%8e%a5-mongodb-%e6%95%b8%e6%93%9a%e5%ba%ab-btzShNnjgp\">4. \u4f7f\u7528 Node.js \u9023\u63a5 MongoDB \u6578\u64da\u5eab<\/h2>\n<p>MongoDB \u662f\u4e00\u7a2e NoSQL \u6578\u64da\u5eab\uff0c\u9069\u5408\u8655\u7406\u5927\u91cf\u975e\u7d50\u69cb\u5316\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 Node.js \u9023\u63a5 MongoDB \u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>const { MongoClient } = require('mongodb');\n\nasync function main() {\n    const uri = \"mongodb:\/\/localhost:27017\";\n    const client = new MongoClient(uri);\n\n    try {\n        await client.connect();\n        const database = client.db('yourDatabase');\n        const collection = database.collection('yourCollection');\n        const query = {};\n        const results = await collection.find(query).toArray();\n        console.log(results);\n    } finally {\n        await client.close();\n    }\n}\n\nmain().catch(console.error);<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 MongoClient \u9023\u63a5\u5230 MongoDB \u6578\u64da\u5eab\uff0c\u57f7\u884c\u67e5\u8a62\u4e26\u5c07\u7d50\u679c\u8f49\u63db\u70ba\u6578\u7d44\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-btzShNnjgp\">\u7e3d\u7d50<\/h2>\n<p>\u4e86\u89e3\u4e0d\u540c\u7684\u6578\u64da\u5eab\u9023\u63a5\u67e5\u8a62\u65b9\u5f0f\u5c0d\u65bc\u958b\u767c\u8005\u4f86\u8aaa\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u7121\u8ad6\u662f\u4f7f\u7528 JDBC \u9023\u63a5 MySQL\u3001PHP \u9023\u63a5 PostgreSQL\u3001Python \u9023\u63a5 SQLite\uff0c\u9084\u662f Node.js \u9023\u63a5 MongoDB\uff0c\u638c\u63e1\u9019\u4e9b\u6280\u8853\u90fd\u80fd\u5e6b\u52a9\u4f60\u66f4\u6709\u6548\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u6578\u64da\u3002\u82e5\u4f60\u9700\u8981\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u4f60\u7684\u6578\u64da\u5eab\u61c9\u7528\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u56db\u7a2e\u5e38\u7528\u7684\u6578\u64da\u5eab\u9023\u63a5\u67e5\u8a62\u65b9\u5f0f\uff0c\u5e6b\u52a9\u4f60\u9ad8\u6548\u7ba1\u7406\u548c\u64cd\u4f5c\u6578\u64da\u5eab\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\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-188156","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/188156","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=188156"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/188156\/revisions"}],"predecessor-version":[{"id":188157,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/188156\/revisions\/188157"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=188156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=188156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=188156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}