{"id":61967,"date":"2024-10-13T15:09:15","date_gmt":"2024-10-13T07:09:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/61967\/"},"modified":"2024-10-13T15:09:15","modified_gmt":"2024-10-13T07:09:15","slug":"java%e8%bc%95%e9%ac%86%e8%ae%80%e5%8f%96oracle%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9a%e5%bf%ab%e9%80%9f%e9%ab%98%e6%95%88%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-java%e8%ae%80%e5%8f%96orac","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/61967\/","title":{"rendered":"Java\u8f15\u9b06\u8b80\u53d6Oracle\u6578\u64da\u5eab\uff1a\u5feb\u901f\u9ad8\u6548\u7684\u6578\u64da\u5eab\u64cd\u4f5c (java\u8b80\u53d6oracle\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"java%e8%bc%95%e9%ac%86%e8%ae%80%e5%8f%96oracle%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9a%e5%bf%ab%e9%80%9f%e9%ab%98%e6%95%88%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-LaHyUioyeb\">Java\u8f15\u9b06\u8b80\u53d6Oracle\u6578\u64da\u5eab\uff1a\u5feb\u901f\u9ad8\u6548\u7684\u6578\u64da\u5eab\u64cd\u4f5c<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0cJava\u4f5c\u70ba\u4e00\u7a2e\u5ee3\u6cdb\u4f7f\u7528\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u5e38\u88ab\u7528\u65bc\u8207\u5404\u7a2e\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002Oracle\u6578\u64da\u5eab\u4ee5\u5176\u7a69\u5b9a\u6027\u548c\u9ad8\u6548\u6027\u800c\u805e\u540d\uff0c\u8a31\u591a\u4f01\u696d\u9078\u64c7\u4f7f\u7528\u5b83\u4f86\u5b58\u5132\u548c\u7ba1\u7406\u6578\u64da\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Java\u8f15\u9b06\u8b80\u53d6Oracle\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u4ee3\u78bc\u793a\u4f8b\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n<h2 id=\"%e7%92%b0%e5%a2%83%e6%ba%96%e5%82%99-LaHyUioyeb\">\u74b0\u5883\u6e96\u5099<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u5df2\u7d93\u5b89\u88dd\u4e86\u4ee5\u4e0b\u74b0\u5883\uff1a<\/p>\n<ul>\n<li>Java Development Kit (JDK)<\/li>\n<li>Oracle\u6578\u64da\u5eab<\/li>\n<li>Oracle JDBC\u9a45\u52d5\u7a0b\u5e8f<\/li>\n<\/ul>\n<p>\u60a8\u53ef\u4ee5\u5f9eOracle\u7684\u5b98\u65b9\u7db2\u7ad9\u4e0b\u8f09JDBC\u9a45\u52d5\u7a0b\u5e8f\uff0c\u4e26\u5c07\u5176\u6dfb\u52a0\u5230\u60a8\u7684Java\u9805\u76ee\u4e2d\u3002<\/p>\n<h2 id=\"%e9%80%a3%e6%8e%a5oracle%e6%95%b8%e6%93%9a%e5%ba%ab-LaHyUioyeb\">\u9023\u63a5Oracle\u6578\u64da\u5eab<\/h2>\n<p>\u8981\u9023\u63a5Oracle\u6578\u64da\u5eab\uff0c\u60a8\u9700\u8981\u4f7f\u7528JDBC\uff08Java Database Connectivity\uff09\u3002\u4ee5\u4e0b\u662f\u9023\u63a5Oracle\u6578\u64da\u5eab\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 OracleDBConnection {\n    public static void main(String[] args) {\n        String jdbcUrl = \"jdbc:oracle:thin:@localhost:1521:xe\"; \/\/ \u66ff\u63db\u70ba\u60a8\u7684\u6578\u64da\u5eabURL\n        String username = \"your_username\"; \/\/ \u66ff\u63db\u70ba\u60a8\u7684\u7528\u6236\u540d\n        String password = \"your_password\"; \/\/ \u66ff\u63db\u70ba\u60a8\u7684\u5bc6\u78bc\n\n        try {\n            Connection connection = DriverManager.getConnection(jdbcUrl, username, password);\n            System.out.println(\"\u6210\u529f\u9023\u63a5\u5230Oracle\u6578\u64da\u5eab\uff01\");\n            connection.close();\n        } catch (SQLException e) {\n            e.printStackTrace();\n        }\n    }\n}<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u78bc\u4e2d\uff0c\u60a8\u9700\u8981\u6839\u64da\u60a8\u7684\u6578\u64da\u5eab\u914d\u7f6e\u66ff\u63dbjdbcUrl\u3001username\u548cpassword\u3002\u6210\u529f\u9023\u63a5\u5f8c\uff0c\u60a8\u5c07\u770b\u5230\u201c\u6210\u529f\u9023\u63a5\u5230Oracle\u6578\u64da\u5eab\uff01\u201d\u7684\u6d88\u606f\u3002<\/p>\n<h2 id=\"%e8%ae%80%e5%8f%96%e6%95%b8%e6%93%9a-LaHyUioyeb\">\u8b80\u53d6\u6578\u64da<\/h2>\n<p>\u4e00\u65e6\u6210\u529f\u9023\u63a5\u5230\u6578\u64da\u5eab\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528SQL\u67e5\u8a62\u4f86\u8b80\u53d6\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5f9eOracle\u6578\u64da\u5eab\u4e2d\u8b80\u53d6\u6578\u64da\uff1a<\/p>\n<pre><code>import java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.ResultSet;\nimport java.sql.SQLException;\nimport java.sql.Statement;\n\npublic class ReadData {\n    public static void main(String[] args) {\n        String jdbcUrl = \"jdbc:oracle:thin:@localhost:1521:xe\"; \/\/ \u66ff\u63db\u70ba\u60a8\u7684\u6578\u64da\u5eabURL\n        String username = \"your_username\"; \/\/ \u66ff\u63db\u70ba\u60a8\u7684\u7528\u6236\u540d\n        String password = \"your_password\"; \/\/ \u66ff\u63db\u70ba\u60a8\u7684\u5bc6\u78bc\n\n        try {\n            Connection connection = DriverManager.getConnection(jdbcUrl, username, password);\n            Statement statement = connection.createStatement();\n            String sql = \"SELECT * FROM your_table\"; \/\/ \u66ff\u63db\u70ba\u60a8\u7684\u8868\u540d\n            ResultSet resultSet = statement.executeQuery(sql);\n\n            while (resultSet.next()) {\n                \/\/ \u5047\u8a2d\u8868\u4e2d\u6709\u4e00\u500b\u540d\u70ba'name'\u7684\u5217\n                String name = resultSet.getString(\"name\");\n                System.out.println(\"\u59d3\u540d: \" + name);\n            }\n\n            resultSet.close();\n            statement.close();\n            connection.close();\n        } catch (SQLException e) {\n            e.printStackTrace();\n        }\n    }\n}<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86SQL\u67e5\u8a62\u4f86\u9078\u64c7\u8868\u4e2d\u7684\u6240\u6709\u6578\u64da\uff0c\u4e26\u901a\u904eResultSet\u5c0d\u8c61\u4f86\u904d\u6b77\u7d50\u679c\u3002\u60a8\u53ef\u4ee5\u6839\u64da\u9700\u8981\u4fee\u6539SQL\u67e5\u8a62\u4ee5\u7372\u53d6\u7279\u5b9a\u7684\u6578\u64da\u3002<\/p>\n<h2 id=\"%e8%99%95%e7%90%86%e7%95%b0%e5%b8%b8-LaHyUioyeb\">\u8655\u7406\u7570\u5e38<\/h2>\n<p>\u5728\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u8655\u7406\u7570\u5e38\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4f7f\u7528try-catch\u584a\u53ef\u4ee5\u5e6b\u52a9\u60a8\u6355\u7372\u548c\u8655\u7406SQLException\uff0c\u5f9e\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7a69\u5b9a\u6027\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-LaHyUioyeb\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u5730\u4f7f\u7528Java\u8b80\u53d6Oracle\u6578\u64da\u5eab\u4e2d\u7684\u6578\u64da\u3002\u9019\u4e0d\u50c5\u63d0\u9ad8\u4e86\u6578\u64da\u64cd\u4f5c\u7684\u6548\u7387\uff0c\u9084\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u66f4\u9748\u6d3b\u5730\u7ba1\u7406\u6578\u64da\u3002\u7121\u8ad6\u662f\u7528\u65bc\u4f01\u696d\u61c9\u7528\u9084\u662f\u500b\u4eba\u9805\u76ee\uff0c\u638c\u63e1\u9019\u4e9b\u57fa\u672c\u6280\u80fd\u90fd\u662f\u975e\u5e38\u6709\u76ca\u7684\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\u90e8\u7f72\u60a8\u7684Java\u61c9\u7528\uff0cServer.HK\u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u5e6b\u52a9\u60a8\u8f15\u9b06\u7ba1\u7406\u548c\u64f4\u5c55\u60a8\u7684\u670d\u52d9\u5668\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Java\u8f15\u9b06\u9ad8\u6548\u5730\u8b80\u53d6Oracle\u6578\u64da\u5eab\uff0c\u638c\u63e1\u5feb\u901f\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u6280\u5de7\u8207\u6700\u4f73\u5be6\u8e10\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-61967","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61967","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=61967"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61967\/revisions"}],"predecessor-version":[{"id":61968,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61967\/revisions\/61968"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=61967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=61967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=61967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}