{"id":97110,"date":"2024-10-20T15:05:18","date_gmt":"2024-10-20T07:05:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/97110\/"},"modified":"2024-10-20T15:05:19","modified_gmt":"2024-10-20T07:05:19","slug":"p%e6%95%b8%e6%93%9a%e6%b7%bb%e5%8a%a0%e5%95%8f%e9%a1%8c%ef%bc%9a%e4%bf%a1%e6%81%af%e7%84%a1%e6%b3%95%e5%82%b3%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab-jsp%e6%95%b8%e6%93%9a%e6%b7%bb%e5%8a%a0%e4%b8%8d","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/97110\/","title":{"rendered":"P\u6578\u64da\u6dfb\u52a0\u554f\u984c\uff1a\u4fe1\u606f\u7121\u6cd5\u50b3\u5165\u6578\u64da\u5eab (jsp\u6578\u64da\u6dfb\u52a0\u4e0d\u5230\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"p%e6%95%b8%e6%93%9a%e6%b7%bb%e5%8a%a0%e5%95%8f%e9%a1%8c%ef%bc%9a%e4%bf%a1%e6%81%af%e7%84%a1%e6%b3%95%e5%82%b3%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab-jsp%e6%95%b8%e6%93%9a%e6%b7%bb%e5%8a%a0%e4%b8%8d-zrRCgYXlTC\">P\u6578\u64da\u6dfb\u52a0\u554f\u984c\uff1a\u4fe1\u606f\u7121\u6cd5\u50b3\u5165\u6578\u64da\u5eab (jsp\u6578\u64da\u6dfb\u52a0\u4e0d\u5230\u6578\u64da\u5eab)<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u4f7f\u7528\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u3002\u7121\u8ad6\u662f\u7528\u65bc\u5b58\u5132\u7528\u6236\u4fe1\u606f\u3001\u4ea4\u6613\u8a18\u9304\u9084\u662f\u5176\u4ed6\u985e\u578b\u7684\u6578\u64da\uff0c\u6578\u64da\u5eab\u90fd\u626e\u6f14\u8457\u95dc\u9375\u89d2\u8272\u3002\u7136\u800c\uff0c\u958b\u767c\u8005\u5728\u4f7f\u7528 JSP\uff08JavaServer Pages\uff09\u9032\u884c\u6578\u64da\u6dfb\u52a0\u6642\uff0c\u7d93\u5e38\u6703\u9047\u5230\u6578\u64da\u7121\u6cd5\u6210\u529f\u50b3\u5165\u6578\u64da\u5eab\u7684\u554f\u984c\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u9019\u4e00\u554f\u984c\u7684\u5e38\u898b\u539f\u56e0\u53ca\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<h2 id=\"%e5%b8%b8%e8%a6%8b%e5%8e%9f%e5%9b%a0-zrRCgYXlTC\">\u5e38\u898b\u539f\u56e0<\/h2>\n<p>\u6578\u64da\u7121\u6cd5\u50b3\u5165\u6578\u64da\u5eab\u7684\u539f\u56e0\u591a\u7a2e\u591a\u6a23\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u5e38\u898b\u7684\u554f\u984c\uff1a<\/p>\n<ul>\n<li><strong>\u6578\u64da\u5eab\u9023\u63a5\u554f\u984c\uff1a<\/strong>\u5982\u679c JSP \u9801\u9762\u7121\u6cd5\u6b63\u78ba\u9023\u63a5\u5230\u6578\u64da\u5eab\uff0c\u5247\u7121\u6cd5\u57f7\u884c\u4efb\u4f55\u6578\u64da\u64cd\u4f5c\u3002\u9019\u53ef\u80fd\u662f\u7531\u65bc\u932f\u8aa4\u7684\u9023\u63a5\u5b57\u7b26\u4e32\u3001\u7528\u6236\u540d\u6216\u5bc6\u78bc\u5f15\u8d77\u7684\u3002<\/li>\n<li><strong>SQL \u8a9e\u6cd5\u932f\u8aa4\uff1a<\/strong>\u5728\u57f7\u884c SQL \u8a9e\u53e5\u6642\uff0c\u5982\u679c\u8a9e\u6cd5\u4e0d\u6b63\u78ba\uff0c\u5247\u6703\u5c0e\u81f4\u6578\u64da\u7121\u6cd5\u63d2\u5165\u3002\u4f8b\u5982\uff0c\u7f3a\u5c11\u5fc5\u8981\u7684\u5b57\u6bb5\u6216\u4f7f\u7528\u4e86\u932f\u8aa4\u7684\u6578\u64da\u985e\u578b\u3002<\/li>\n<li><strong>\u6578\u64da\u9a57\u8b49\u5931\u6557\uff1a<\/strong>\u5728\u5c07\u6578\u64da\u63d2\u5165\u6578\u64da\u5eab\u4e4b\u524d\uff0c\u61c9\u7528\u7a0b\u5e8f\u53ef\u80fd\u6703\u9032\u884c\u6578\u64da\u9a57\u8b49\u3002\u5982\u679c\u6578\u64da\u4e0d\u7b26\u5408\u9810\u671f\u683c\u5f0f\uff0c\u5247\u53ef\u80fd\u6703\u88ab\u62d2\u7d55\u3002<\/li>\n<li><strong>\u4e8b\u52d9\u8655\u7406\u554f\u984c\uff1a<\/strong>\u5982\u679c\u5728\u6578\u64da\u5eab\u64cd\u4f5c\u4e2d\u4f7f\u7528\u4e86\u4e8b\u52d9\uff0c\u4e26\u4e14\u5728\u63d0\u4ea4\u4e4b\u524d\u767c\u751f\u4e86\u932f\u8aa4\uff0c\u5247\u6240\u6709\u66f4\u6539\u90fd\u5c07\u88ab\u56de\u6efe\uff0c\u5c0e\u81f4\u6578\u64da\u672a\u80fd\u63d2\u5165\u3002<\/li>\n<\/ul>\n<h2 id=\"%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88-zrRCgYXlTC\">\u89e3\u6c7a\u65b9\u6848<\/h2>\n<p>\u91dd\u5c0d\u4e0a\u8ff0\u554f\u984c\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u63a1\u53d6\u4ee5\u4e0b\u63aa\u65bd\u4f86\u89e3\u6c7a\u6578\u64da\u7121\u6cd5\u6dfb\u52a0\u5230\u6578\u64da\u5eab\u7684\u554f\u984c\uff1a<\/p>\n<h3 id=\"1-%e6%aa%a2%e6%9f%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-zrRCgYXlTC\">1. \u6aa2\u67e5\u6578\u64da\u5eab\u9023\u63a5<\/h3>\n<pre><code>\nString url = \"jdbc:mysql:\/\/localhost:3306\/yourdatabase\";\nString user = \"yourusername\";\nString password = \"yourpassword\";\nConnection conn = DriverManager.getConnection(url, user, password);\n<\/code><\/pre>\n<p>\u78ba\u4fdd\u9023\u63a5\u5b57\u7b26\u4e32\u6b63\u78ba\uff0c\u4e26\u4e14\u6578\u64da\u5eab\u670d\u52d9\u6b63\u5728\u904b\u884c\u3002<\/p>\n<h3 id=\"2-%e9%a9%97%e8%ad%89-sql-%e8%aa%9e%e5%8f%a5-zrRCgYXlTC\">2. \u9a57\u8b49 SQL \u8a9e\u53e5<\/h3>\n<p>\u5728\u57f7\u884c SQL \u8a9e\u53e5\u4e4b\u524d\uff0c\u61c9\u4ed4\u7d30\u6aa2\u67e5\u8a9e\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u6b63\u78ba\u7684\u63d2\u5165\u8a9e\u53e5\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nString sql = \"INSERT INTO users (username, password) VALUES (?, ?)\";\nPreparedStatement pstmt = conn.prepareStatement(sql);\npstmt.setString(1, username);\npstmt.setString(2, password);\npstmt.executeUpdate();\n<\/code><\/pre>\n<h3 id=\"3-%e6%95%b8%e6%93%9a%e9%a9%97%e8%ad%89-zrRCgYXlTC\">3. \u6578\u64da\u9a57\u8b49<\/h3>\n<p>\u5728\u5c07\u6578\u64da\u63d2\u5165\u6578\u64da\u5eab\u4e4b\u524d\uff0c\u61c9\u9032\u884c\u5fc5\u8981\u7684\u9a57\u8b49\u3002\u4f8b\u5982\uff0c\u6aa2\u67e5\u7528\u6236\u540d\u662f\u5426\u5df2\u5b58\u5728\uff0c\u6216\u5bc6\u78bc\u662f\u5426\u7b26\u5408\u5b89\u5168\u8981\u6c42\u3002<\/p>\n<h3 id=\"4-%e4%ba%8b%e5%8b%99%e7%ae%a1%e7%90%86-zrRCgYXlTC\">4. \u4e8b\u52d9\u7ba1\u7406<\/h3>\n<p>\u5982\u679c\u4f7f\u7528\u4e8b\u52d9\uff0c\u8acb\u78ba\u4fdd\u5728\u6240\u6709\u64cd\u4f5c\u6210\u529f\u5f8c\u518d\u63d0\u4ea4\u4e8b\u52d9\u3002\u4ee5\u4e0b\u662f\u4e8b\u52d9\u8655\u7406\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>\ntry {\n    conn.setAutoCommit(false);\n    \/\/ \u57f7\u884c\u591a\u500b SQL \u64cd\u4f5c\n    conn.commit();\n} catch (SQLException e) {\n    conn.rollback();\n}\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-zrRCgYXlTC\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u4f7f\u7528 JSP \u9032\u884c\u6578\u64da\u6dfb\u52a0\u6642\uff0c\u958b\u767c\u8005\u53ef\u80fd\u6703\u9047\u5230\u591a\u7a2e\u554f\u984c\uff0c\u5c0e\u81f4\u6578\u64da\u7121\u6cd5\u6210\u529f\u50b3\u5165\u6578\u64da\u5eab\u3002\u901a\u904e\u6aa2\u67e5\u6578\u64da\u5eab\u9023\u63a5\u3001\u9a57\u8b49 SQL \u8a9e\u53e5\u3001\u9032\u884c\u6578\u64da\u9a57\u8b49\u4ee5\u53ca\u59a5\u5584\u7ba1\u7406\u4e8b\u52d9\uff0c\u53ef\u4ee5\u6709\u6548\u89e3\u6c7a\u9019\u4e9b\u554f\u984c\u3002\u5c0d\u65bc\u9700\u8981\u7a69\u5b9a\u548c\u9ad8\u6548\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\u7684\u958b\u767c\u8005\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u6216 <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u4f86\u90e8\u7f72\u61c9\u7528\u7a0b\u5e8f\u5c07\u662f\u660e\u667a\u4e4b\u8209\u3002\u9019\u4e0d\u50c5\u80fd\u63d0\u9ad8\u61c9\u7528\u7684\u6027\u80fd\uff0c\u9084\u80fd\u78ba\u4fdd\u6578\u64da\u7684\u5b89\u5168\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u89e3\u6c7aP\u6578\u64da\u6dfb\u52a0\u554f\u984c\uff0c\u4e86\u89e3\u5982\u4f55\u8655\u7406JSP\u6578\u64da\u7121\u6cd5\u50b3\u5165\u6578\u64da\u5eab\u7684\u60c5\u6cc1\uff0c\u78ba\u4fdd\u6578\u64da\u9806\u5229\u5b58\u5132\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-97110","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97110","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=97110"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97110\/revisions"}],"predecessor-version":[{"id":97111,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97110\/revisions\/97111"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=97110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=97110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=97110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}