{"id":97090,"date":"2024-10-20T14:55:15","date_gmt":"2024-10-20T06:55:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/97090\/"},"modified":"2024-10-20T14:55:16","modified_gmt":"2024-10-20T06:55:16","slug":"p%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8%e6%93%9a%e5%af%ab%e5%85%a5-jsp%e5%b0%87%e6%95%b8%e6%93%9a%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/97090\/","title":{"rendered":"P\u5be6\u73fe\u6578\u64da\u5eab\u6578\u64da\u5beb\u5165 (jsp\u5c07\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"p%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8%e6%93%9a%e5%af%ab%e5%85%a5-jsp%e5%b0%87%e6%95%b8%e6%93%9a%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab-vFKXFeoodB\">P\u5be6\u73fe\u6578\u64da\u5eab\u6578\u64da\u5beb\u5165 (jsp\u5c07\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab)<\/h1>\n<p>\u5728\u73fe\u4ee3\u7684\u7db2\u7d61\u61c9\u7528\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u4f7f\u7528\u5df2\u7d93\u6210\u70ba\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002JavaServer Pages (JSP) \u662f\u4e00\u7a2e\u57fa\u65bcJava\u7684\u6280\u8853\uff0c\u80fd\u5920\u8f15\u9b06\u5730\u5c07\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528JSP\u5c07\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-vFKXFeoodB\">\u6578\u64da\u5eab\u9023\u63a5<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u78ba\u4fdd\u5df2\u7d93\u8a2d\u7f6e\u597d\u6578\u64da\u5eab\u74b0\u5883\u3002\u9019\u88e1\u4ee5MySQL\u70ba\u4f8b\uff0c\u9996\u5148\u9700\u8981\u5728JSP\u4e2d\u52a0\u8f09JDBC\u9a45\u52d5\u7a0b\u5e8f\uff0c\u7136\u5f8c\u5efa\u7acb\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002<\/p>\n<pre><code>Class.forName(\"com.mysql.cj.jdbc.Driver\");\nConnection conn = DriverManager.getConnection(\"jdbc:mysql:\/\/localhost:3306\/your_database\", \"username\", \"password\");<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u78bc\u4e2d\uff0c\u8acb\u5c07<code>your_database<\/code>\u3001<code>username<\/code>\u548c<code>password<\/code>\u66ff\u63db\u70ba\u5be6\u969b\u7684\u6578\u64da\u5eab\u540d\u7a31\u3001\u7528\u6236\u540d\u548c\u5bc6\u78bc\u3002<\/p>\n<h2 id=\"%e5%89%b5%e5%bb%ba%e6%95%b8%e6%93%9a%e5%af%ab%e5%85%a5%e5%8a%9f%e8%83%bd-vFKXFeoodB\">\u5275\u5efa\u6578\u64da\u5beb\u5165\u529f\u80fd<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b\u65b9\u6cd5\u4f86\u5c07\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab\u3002\u9019\u88e1\u4ee5\u4e00\u500b\u7c21\u55ae\u7684\u7528\u6236\u8a3b\u518a\u70ba\u4f8b\uff0c\u5c07\u7528\u6236\u540d\u548c\u5bc6\u78bc\u5beb\u5165\u6578\u64da\u5eab\u3002<\/p>\n<pre><code>public void insertUser(String username, String password) {\n    String sql = \"INSERT INTO users (username, password) VALUES (?, ?)\";\n    try {\n        PreparedStatement pstmt = conn.prepareStatement(sql);\n        pstmt.setString(1, username);\n        pstmt.setString(2, password);\n        pstmt.executeUpdate();\n    } catch (SQLException e) {\n        e.printStackTrace();\n    }\n}<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86<code>PreparedStatement<\/code>\u4f86\u9632\u6b62SQL\u6ce8\u5165\u653b\u64ca\uff0c\u4e26\u78ba\u4fdd\u6578\u64da\u7684\u5b89\u5168\u6027\u3002<\/p>\n<h2 id=\"%e5%be%9ejsp%e9%a0%81%e9%9d%a2%e7%8d%b2%e5%8f%96%e6%95%b8%e6%93%9a-vFKXFeoodB\">\u5f9eJSP\u9801\u9762\u7372\u53d6\u6578\u64da<\/h2>\n<p>\u5728JSP\u9801\u9762\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528HTML\u8868\u55ae\u4f86\u7372\u53d6\u7528\u6236\u8f38\u5165\u7684\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u8868\u55ae\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;form action=\"register.jsp\" method=\"post\"&gt;\n    \u7528\u6236\u540d: &lt;input type=\"text\" name=\"username\"&gt;&lt;br&gt;\n    \u5bc6\u78bc: &lt;input type=\"password\" name=\"password\"&gt;&lt;br&gt;\n    &lt;input type=\"submit\" value=\"\u8a3b\u518a\"&gt;\n&lt;\/form&gt;<\/code><\/pre>\n<p>\u7576\u7528\u6236\u63d0\u4ea4\u8868\u55ae\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u5728<code>register.jsp<\/code>\u4e2d\u8655\u7406\u9019\u4e9b\u6578\u64da\uff1a<\/p>\n<pre><code>&lt;% \n    String username = request.getParameter(\"username\");\n    String password = request.getParameter(\"password\");\n    insertUser(username, password);\n%&gt;<\/code><\/pre>\n<h2 id=\"%e9%97%9c%e9%96%89%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-vFKXFeoodB\">\u95dc\u9589\u6578\u64da\u5eab\u9023\u63a5<\/h2>\n<p>\u5728\u5b8c\u6210\u6578\u64da\u5beb\u5165\u5f8c\uff0c\u8a18\u5f97\u95dc\u9589\u6578\u64da\u5eab\u9023\u63a5\uff0c\u4ee5\u91cb\u653e\u8cc7\u6e90\uff1a<\/p>\n<pre><code>try {\n    if (conn != null) {\n        conn.close();\n    }\n} catch (SQLException e) {\n    e.printStackTrace();\n}<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-vFKXFeoodB\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u4f7f\u7528JSP\u5c07\u6578\u64da\u5beb\u5165\u6578\u64da\u5eab\u3002\u9019\u7a2e\u65b9\u6cd5\u4e0d\u50c5\u7c21\u55ae\u6613\u7528\uff0c\u9084\u80fd\u78ba\u4fdd\u6578\u64da\u7684\u5b89\u5168\u6027\u3002\u5c0d\u65bc\u9700\u8981\u8655\u7406\u5927\u91cf\u6578\u64da\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684\u4e3b\u6a5f\u74b0\u5883\u81f3\u95dc\u91cd\u8981\u3002\u82e5\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\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\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\u90fd\u80fd\u70ba\u60a8\u7684\u61c9\u7528\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to implement data writing to a database using JSP, ensuring efficient data management and seamless integration in your web applications.<\/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-97090","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97090","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=97090"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97090\/revisions"}],"predecessor-version":[{"id":97091,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97090\/revisions\/97091"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=97090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=97090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=97090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}