{"id":154575,"date":"2024-11-02T16:57:19","date_gmt":"2024-11-02T08:57:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/154575\/"},"modified":"2024-11-02T16:57:20","modified_gmt":"2024-11-02T08:57:20","slug":"%e5%b0%8d-oracle-%e7%b6%81%e5%ae%9a%e8%ae%8a%e9%87%8f%e7%9a%84%e5%af%a6%e9%9a%9b%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/154575\/","title":{"rendered":"\u5c0d Oracle \u7d81\u5b9a\u8b8a\u91cf\u7684\u5be6\u969b\u64cd\u4f5c"},"content":{"rendered":"<h1 id=\"%e5%b0%8d-oracle-%e7%b6%81%e5%ae%9a%e8%ae%8a%e9%87%8f%e7%9a%84%e5%af%a6%e9%9a%9b%e6%93%8d%e4%bd%9c-PnmgETWZBV\">\u5c0d Oracle \u7d81\u5b9a\u8b8a\u91cf\u7684\u5be6\u969b\u64cd\u4f5c<\/h1>\n<p>\u5728 Oracle \u6578\u64da\u5eab\u4e2d\uff0c\u7d81\u5b9a\u8b8a\u91cf\u662f\u4e00\u7a2e\u91cd\u8981\u7684\u6280\u8853\uff0c\u80fd\u5920\u63d0\u9ad8 SQL \u67e5\u8a62\u7684\u6027\u80fd\u548c\u5b89\u5168\u6027\u3002\u7d81\u5b9a\u8b8a\u91cf\u5141\u8a31\u7528\u6236\u5728\u57f7\u884c SQL \u8a9e\u53e5\u6642\uff0c\u5c07\u8b8a\u91cf\u7684\u503c\u8207 SQL \u8a9e\u53e5\u5206\u958b\uff0c\u9019\u6a23\u4e0d\u50c5\u53ef\u4ee5\u6e1b\u5c11 SQL \u6ce8\u5165\u7684\u98a8\u96aa\uff0c\u9084\u80fd\u63d0\u9ad8\u67e5\u8a62\u7684\u6548\u7387\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e Oracle \u4e2d\u7d81\u5b9a\u8b8a\u91cf\u7684\u5be6\u969b\u64cd\u4f5c\uff0c\u5305\u62ec\u5176\u512a\u52e2\u3001\u4f7f\u7528\u65b9\u6cd5\u53ca\u793a\u4f8b\u3002<\/p>\n<h2 id=\"%e7%b6%81%e5%ae%9a%e8%ae%8a%e9%87%8f%e7%9a%84%e5%84%aa%e5%8b%a2-PnmgETWZBV\">\u7d81\u5b9a\u8b8a\u91cf\u7684\u512a\u52e2<\/h2>\n<ul>\n<li><strong>\u63d0\u9ad8\u6027\u80fd\uff1a<\/strong>\u4f7f\u7528\u7d81\u5b9a\u8b8a\u91cf\u53ef\u4ee5\u4f7f Oracle \u6578\u64da\u5eab\u91cd\u8907\u4f7f\u7528\u5df2\u7d93\u7de8\u8b6f\u7684\u57f7\u884c\u8a08\u5283\uff0c\u5f9e\u800c\u6e1b\u5c11\u89e3\u6790\u6642\u9593\u3002<\/li>\n<li><strong>\u589e\u5f37\u5b89\u5168\u6027\uff1a<\/strong>\u7d81\u5b9a\u8b8a\u91cf\u80fd\u6709\u6548\u9632\u6b62 SQL \u6ce8\u5165\u653b\u64ca\uff0c\u56e0\u70ba\u7528\u6236\u8f38\u5165\u7684\u6578\u64da\u4e0d\u6703\u76f4\u63a5\u62fc\u63a5\u5230 SQL \u8a9e\u53e5\u4e2d\u3002<\/li>\n<li><strong>\u7c21\u5316\u4ee3\u78bc\uff1a<\/strong>\u4f7f\u7528\u7d81\u5b9a\u8b8a\u91cf\u53ef\u4ee5\u4f7f SQL \u8a9e\u53e5\u66f4\u7c21\u6f54\uff0c\u6613\u65bc\u7dad\u8b77\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b6%81%e5%ae%9a%e8%ae%8a%e9%87%8f%e7%9a%84%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95-PnmgETWZBV\">\u7d81\u5b9a\u8b8a\u91cf\u7684\u4f7f\u7528\u65b9\u6cd5<\/h2>\n<p>\u5728 Oracle \u4e2d\uff0c\u7d81\u5b9a\u8b8a\u91cf\u7684\u4f7f\u7528\u4e3b\u8981\u4f9d\u8cf4\u65bc PL\/SQL \u548c SQL*Plus \u7b49\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<h3 id=\"1-%e4%bd%bf%e7%94%a8-sqlplus-PnmgETWZBV\">1. \u4f7f\u7528 SQL*Plus<\/h3>\n<p>\u5728 SQL*Plus \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 `VARIABLE` \u547d\u4ee4\u4f86\u5b9a\u7fa9\u7d81\u5b9a\u8b8a\u91cf\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>VARIABLE v_emp_id NUMBER;\nEXEC :v_emp_id := 101;\n\nSELECT * FROM employees WHERE employee_id = :v_emp_id;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba `v_emp_id` \u7684\u7d81\u5b9a\u8b8a\u91cf\uff0c\u7136\u5f8c\u5c07\u5176\u503c\u8a2d\u7f6e\u70ba 101\uff0c\u6700\u5f8c\u5728 SELECT \u8a9e\u53e5\u4e2d\u4f7f\u7528\u8a72\u8b8a\u91cf\u3002<\/p>\n<h3 id=\"2-%e4%bd%bf%e7%94%a8-pl-sql-PnmgETWZBV\">2. \u4f7f\u7528 PL\/SQL<\/h3>\n<p>\u5728 PL\/SQL \u4e2d\uff0c\u7d81\u5b9a\u8b8a\u91cf\u901a\u5e38\u7528\u65bc\u5b58\u5132\u904e\u7a0b\u6216\u51fd\u6578\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\uff1a<\/p>\n<pre><code>CREATE OR REPLACE PROCEDURE get_employee_details (emp_id IN NUMBER) IS\n    v_emp_name employees.first_name%TYPE;\nBEGIN\n    SELECT first_name INTO v_emp_name FROM employees WHERE employee_id = emp_id;\n    DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_emp_name);\nEND;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u540d\u70ba `get_employee_details` \u7684\u5b58\u5132\u904e\u7a0b\uff0c\u8a72\u904e\u7a0b\u63a5\u53d7\u4e00\u500b\u6574\u6578\u985e\u578b\u7684\u53c3\u6578 `emp_id`\uff0c\u4e26\u6839\u64da\u8a72\u53c3\u6578\u67e5\u8a62\u54e1\u5de5\u7684\u540d\u5b57\u3002<\/p>\n<h3 id=\"3-%e4%bd%bf%e7%94%a8-jdbc-PnmgETWZBV\">3. \u4f7f\u7528 JDBC<\/h3>\n<p>\u5728 Java \u4e2d\uff0c\u4f7f\u7528 JDBC \u9023\u63a5 Oracle \u6578\u64da\u5eab\u6642\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7d81\u5b9a\u8b8a\u91cf\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\uff1a<\/p>\n<pre><code>Connection conn = DriverManager.getConnection(\"jdbc:oracle:thin:@localhost:1521:xe\", \"username\", \"password\");\nPreparedStatement pstmt = conn.prepareStatement(\"SELECT * FROM employees WHERE employee_id = ?\");\npstmt.setInt(1, 101);\nResultSet rs = pstmt.executeQuery();<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 `PreparedStatement` \u4f86\u5275\u5efa\u4e00\u500b SQL \u67e5\u8a62\uff0c\u4e26\u4f7f\u7528 `setInt` \u65b9\u6cd5\u5c07\u7d81\u5b9a\u8b8a\u91cf\u7684\u503c\u8a2d\u7f6e\u70ba 101\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-PnmgETWZBV\">\u7e3d\u7d50<\/h2>\n<p>\u7d81\u5b9a\u8b8a\u91cf\u5728 Oracle \u6578\u64da\u5eab\u4e2d\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u63d0\u9ad8\u6027\u80fd\u548c\u5b89\u5168\u6027\u3002\u7121\u8ad6\u662f\u5728 SQL*Plus\u3001PL\/SQL \u9084\u662f JDBC \u4e2d\uff0c\u6b63\u78ba\u4f7f\u7528\u7d81\u5b9a\u8b8a\u91cf\u90fd\u80fd\u4f7f\u4ee3\u78bc\u66f4\u52a0\u9ad8\u6548\u548c\u5b89\u5168\u3002\u5c0d\u65bc\u9700\u8981\u8655\u7406\u5927\u91cf\u6578\u64da\u7684\u61c9\u7528\u7a0b\u5e8f\uff0c\u4f7f\u7528\u7d81\u5b9a\u8b8a\u91cf\u662f\u6700\u4f73\u5be6\u8e10\u4e4b\u4e00\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\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u5404\u7a2e\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Oracle \u7d81\u5b9a\u8b8a\u91cf\u7684\u5be6\u969b\u64cd\u4f5c\uff0c\u5b78\u7fd2\u5982\u4f55\u63d0\u9ad8\u67e5\u8a62\u6027\u80fd\u548c\u5b89\u5168\u6027\uff0c\u512a\u5316\u6578\u64da\u5eab\u61c9\u7528\u7a0b\u5e8f\u7684\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-154575","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/154575","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=154575"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/154575\/revisions"}],"predecessor-version":[{"id":154576,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/154575\/revisions\/154576"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=154575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=154575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=154575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}