{"id":62083,"date":"2024-10-13T16:02:17","date_gmt":"2024-10-13T08:02:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/62083\/"},"modified":"2024-10-13T16:02:17","modified_gmt":"2024-10-13T08:02:17","slug":"java%e6%95%b8%e6%93%9a%e5%ba%ab%e5%a6%82%e4%bd%95%e9%80%b2%e8%a1%8c%e9%97%9c%e8%81%af%ef%bc%9f-java%e6%95%b8%e6%93%9a%e5%ba%ab%e6%80%8e%e9%ba%bc%e9%97%9c%e8%81%af","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/62083\/","title":{"rendered":"Java\u6578\u64da\u5eab\u5982\u4f55\u9032\u884c\u95dc\u806f\uff1f (java\u6578\u64da\u5eab\u600e\u9ebc\u95dc\u806f)"},"content":{"rendered":"<h1 id=\"java%e6%95%b8%e6%93%9a%e5%ba%ab%e5%a6%82%e4%bd%95%e9%80%b2%e8%a1%8c%e9%97%9c%e8%81%af%ef%bc%9f-VhatkYGgmG\">Java\u6578\u64da\u5eab\u5982\u4f55\u9032\u884c\u95dc\u806f\uff1f<\/h1>\n<p>\u5728\u73fe\u4ee3\u61c9\u7528\u7a0b\u5e8f\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u95dc\u806f\u6027\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002Java\u4f5c\u70ba\u4e00\u7a2e\u5ee3\u6cdb\u4f7f\u7528\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u65b9\u6cd5\u4f86\u8655\u7406\u6578\u64da\u5eab\u4e4b\u9593\u7684\u95dc\u806f\u3002\u672c\u6587\u5c07\u63a2\u8a0eJava\u5982\u4f55\u9032\u884c\u6578\u64da\u5eab\u95dc\u806f\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e9%97%9c%e8%81%af%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-VhatkYGgmG\">\u6578\u64da\u5eab\u95dc\u806f\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u6578\u64da\u5eab\u95dc\u806f\u662f\u6307\u5728\u4e0d\u540c\u7684\u6578\u64da\u8868\u4e4b\u9593\u5efa\u7acb\u908f\u8f2f\u9023\u63a5\u3002\u9019\u4e9b\u95dc\u806f\u901a\u5e38\u662f\u57fa\u65bc\u4e3b\u9375\u548c\u5916\u9375\u7684\u6982\u5ff5\u3002\u4e3b\u9375\u662f\u552f\u4e00\u6a19\u8b58\u4e00\u884c\u6578\u64da\u7684\u5b57\u6bb5\uff0c\u800c\u5916\u9375\u5247\u662f\u6307\u5411\u53e6\u4e00\u500b\u8868\u7684\u4e3b\u9375\u3002\u901a\u904e\u9019\u7a2e\u65b9\u5f0f\uff0c\u6578\u64da\u5eab\u80fd\u5920\u6709\u6548\u5730\u7d44\u7e54\u548c\u6aa2\u7d22\u6578\u64da\u3002<\/p>\n<h2 id=\"java%e4%b8%ad%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e9%97%9c%e8%81%af-VhatkYGgmG\">Java\u4e2d\u7684\u6578\u64da\u5eab\u95dc\u806f<\/h2>\n<p>\u5728Java\u4e2d\uff0c\u901a\u5e38\u4f7f\u7528Java Database Connectivity (JDBC)\u4f86\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002JDBC\u63d0\u4f9b\u4e86\u4e00\u7d44API\uff0c\u5141\u8a31\u958b\u767c\u8005\u57f7\u884cSQL\u67e5\u8a62\u3001\u66f4\u65b0\u6578\u64da\u4ee5\u53ca\u7ba1\u7406\u6578\u64da\u5eab\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528JDBC\u9032\u884c\u6578\u64da\u5eab\u95dc\u806f\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e8%a8%ad%e7%bd%ae%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-VhatkYGgmG\">1. \u8a2d\u7f6e\u6578\u64da\u5eab\u9023\u63a5<\/h3>\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            String url = \"jdbc:mysql:\/\/localhost:3306\/your_database\";\n            String user = \"your_username\";\n            String password = \"your_password\";\n            connection = DriverManager.getConnection(url, user, password);\n        } catch (SQLException e) {\n            e.printStackTrace();\n        }\n        return connection;\n    }\n}\n<\/code><\/pre>\n<h3 id=\"2-%e5%89%b5%e5%bb%ba%e6%95%b8%e6%93%9a%e8%a1%a8-VhatkYGgmG\">2. \u5275\u5efa\u6578\u64da\u8868<\/h3>\n<p>\u5728\u9032\u884c\u95dc\u806f\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5275\u5efa\u6578\u64da\u8868\u3002\u4ee5\u4e0b\u662f\u5275\u5efa\u5169\u500b\u8868\u7684SQL\u8a9e\u53e5\u793a\u4f8b\uff1a<\/p>\n<pre><code>CREATE TABLE authors (\n    author_id INT PRIMARY KEY,\n    name VARCHAR(100)\n);\n\nCREATE TABLE books (\n    book_id INT PRIMARY KEY,\n    title VARCHAR(100),\n    author_id INT,\n    FOREIGN KEY (author_id) REFERENCES authors(author_id)\n);\n<\/code><\/pre>\n<h3 id=\"3-%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-VhatkYGgmG\">3. \u63d2\u5165\u6578\u64da<\/h3>\n<p>\u5728\u5275\u5efa\u8868\u4e4b\u5f8c\uff0c\u53ef\u4ee5\u63d2\u5165\u6578\u64da\u4ee5\u5efa\u7acb\u95dc\u806f\uff1a<\/p>\n<pre><code>import java.sql.Connection;\nimport java.sql.PreparedStatement;\nimport java.sql.SQLException;\n\npublic class InsertData {\n    public static void insertData() {\n        Connection connection = DatabaseConnection.getConnection();\n        String insertAuthor = \"INSERT INTO authors (author_id, name) VALUES (?, ?)\";\n        String insertBook = \"INSERT INTO books (book_id, title, author_id) VALUES (?, ?, ?)\";\n\n        try {\n            PreparedStatement authorStmt = connection.prepareStatement(insertAuthor);\n            authorStmt.setInt(1, 1);\n            authorStmt.setString(2, \"John Doe\");\n            authorStmt.executeUpdate();\n\n            PreparedStatement bookStmt = connection.prepareStatement(insertBook);\n            bookStmt.setInt(1, 1);\n            bookStmt.setString(2, \"Java Programming\");\n            bookStmt.setInt(3, 1);\n            bookStmt.executeUpdate();\n        } catch (SQLException e) {\n            e.printStackTrace();\n        } finally {\n            try {\n                connection.close();\n            } catch (SQLException e) {\n                e.printStackTrace();\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<h3 id=\"4-%e6%9f%a5%e8%a9%a2%e9%97%9c%e8%81%af%e6%95%b8%e6%93%9a-VhatkYGgmG\">4. \u67e5\u8a62\u95dc\u806f\u6578\u64da<\/h3>\n<p>\u6700\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528JOIN\u8a9e\u53e5\u4f86\u67e5\u8a62\u95dc\u806f\u6578\u64da\uff1a<\/p>\n<pre><code>import java.sql.Connection;\nimport java.sql.ResultSet;\nimport java.sql.Statement;\n\npublic class QueryData {\n    public static void queryData() {\n        Connection connection = DatabaseConnection.getConnection();\n        String query = \"SELECT books.title, authors.name FROM books JOIN authors ON books.author_id = authors.author_id\";\n\n        try {\n            Statement statement = connection.createStatement();\n            ResultSet resultSet = statement.executeQuery(query);\n            while (resultSet.next()) {\n                System.out.println(\"Book: \" + resultSet.getString(\"title\") + \", Author: \" + resultSet.getString(\"name\"));\n            }\n        } catch (SQLException e) {\n            e.printStackTrace();\n        } finally {\n            try {\n                connection.close();\n            } catch (SQLException e) {\n                e.printStackTrace();\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-VhatkYGgmG\">\u7e3d\u7d50<\/h2>\n<p>\u5728Java\u4e2d\uff0c\u901a\u904eJDBC\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u5eab\u4e4b\u9593\u7684\u95dc\u806f\u3002\u5f9e\u8a2d\u7f6e\u6578\u64da\u5eab\u9023\u63a5\u5230\u5275\u5efa\u8868\u3001\u63d2\u5165\u6578\u64da\u4ee5\u53ca\u67e5\u8a62\u95dc\u806f\u6578\u64da\uff0c\u9019\u4e9b\u6b65\u9a5f\u90fd\u662f\u5efa\u7acb\u548c\u7dad\u8b77\u6578\u64da\u5eab\u95dc\u806f\u7684\u57fa\u790e\u3002\u4e86\u89e3\u9019\u4e9b\u57fa\u672c\u6982\u5ff5\u548c\u64cd\u4f5c\uff0c\u5c07\u6709\u52a9\u65bc\u958b\u767c\u8005\u5728\u5be6\u969b\u61c9\u7528\u4e2d\u66f4\u597d\u5730\u8655\u7406\u6578\u64da\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684Java\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK\u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u5728Java\u4e2d\u4f7f\u7528JDBC\u548cORM\u6846\u67b6\u9032\u884c\u6578\u64da\u5eab\u95dc\u806f\uff0c\u5be6\u73fe\u9ad8\u6548\u7684\u6578\u64da\u7ba1\u7406\u548c\u67e5\u8a62\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-62083","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/62083","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=62083"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/62083\/revisions"}],"predecessor-version":[{"id":62084,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/62083\/revisions\/62084"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=62083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=62083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=62083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}