{"id":163333,"date":"2024-11-04T22:38:19","date_gmt":"2024-11-04T14:38:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/163333\/"},"modified":"2024-11-04T22:38:20","modified_gmt":"2024-11-04T14:38:20","slug":"%e8%a7%a3%e6%b1%bap%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%e5%95%8f%e9%a1%8c%ef%bc%88jsp%e5%af%ab%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e6%96%87%e4%ba%82","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/163333\/","title":{"rendered":"\u89e3\u6c7aP\u5beb\u5165\u6578\u64da\u5eab\u4e2d\u6587\u4e82\u78bc\u554f\u984c\uff08JSP\u5beb\u6578\u64da\u5eab\u4e2d\u6587\u4e82\u78bc\uff09"},"content":{"rendered":"<h1 id=\"%e8%a7%a3%e6%b1%bap%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e6%96%87%e4%ba%82%e7%a2%bc%e5%95%8f%e9%a1%8c%ef%bc%88jsp%e5%af%ab%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e6%96%87%e4%ba%82-jYccnqZZLt\">\u89e3\u6c7aP\u5beb\u5165\u6578\u64da\u5eab\u4e2d\u6587\u4e82\u78bc\u554f\u984c\uff08JSP\u5beb\u6578\u64da\u5eab\u4e2d\u6587\u4e82\u78bc\uff09<\/h1>\n<p>\u5728\u7576\u4eca\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\u3002\u7279\u5225\u662f\u5728\u8655\u7406\u4e2d\u6587\u6578\u64da\u6642\uff0c\u4e82\u78bc\u554f\u984c\u7d93\u5e38\u56f0\u64fe\u8457\u958b\u767c\u8005\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5728JSP\u4e2d\u5beb\u5165\u6578\u64da\u5eab\u6642\u51fa\u73fe\u7684\u4e2d\u6587\u4e82\u78bc\u554f\u984c\u53ca\u5176\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<h2 id=\"%e4%ba%82%e7%a2%bc%e5%95%8f%e9%a1%8c%e7%9a%84%e6%a0%b9%e6%ba%90-jYccnqZZLt\">\u4e82\u78bc\u554f\u984c\u7684\u6839\u6e90<\/h2>\n<p>\u4e2d\u6587\u4e82\u78bc\u554f\u984c\u901a\u5e38\u6e90\u65bc\u5b57\u7b26\u7de8\u78bc\u4e0d\u4e00\u81f4\u3002\u7576\u6578\u64da\u5f9e\u4e00\u500b\u7cfb\u7d71\u50b3\u8f38\u5230\u53e6\u4e00\u500b\u7cfb\u7d71\u6642\uff0c\u5982\u679c\u5169\u8005\u7684\u5b57\u7b26\u7de8\u78bc\u4e0d\u5339\u914d\uff0c\u5c31\u6703\u5c0e\u81f4\u4e82\u78bc\u3002\u5e38\u898b\u7684\u5b57\u7b26\u7de8\u78bc\u5305\u62ecUTF-8\u3001GBK\u548cISO-8859-1\u7b49\u3002\u5728JSP\u548c\u6578\u64da\u5eab\u4e4b\u9593\uff0c\u82e5\u672a\u6b63\u78ba\u8a2d\u7f6e\u7de8\u78bc\uff0c\u5c07\u6703\u51fa\u73fe\u4e82\u78bc\u3002<\/p>\n<h2 id=\"jsp%e4%b8%ad%e7%9a%84%e5%ad%97%e7%ac%a6%e7%b7%a8%e7%a2%bc%e8%a8%ad%e7%bd%ae-jYccnqZZLt\">JSP\u4e2d\u7684\u5b57\u7b26\u7de8\u78bc\u8a2d\u7f6e<\/h2>\n<p>\u5728JSP\u4e2d\uff0c\u8a2d\u7f6e\u6b63\u78ba\u7684\u5b57\u7b26\u7de8\u78bc\u662f\u9632\u6b62\u4e82\u78bc\u7684\u7b2c\u4e00\u6b65\u3002\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u65b9\u5f0f\u8a2d\u7f6eJSP\u9801\u9762\u7684\u5b57\u7b26\u7de8\u78bc\uff1a<\/p>\n<pre><code>&lt;%@ page contentType=\"text\/html; charset=UTF-8\" %&gt;<\/code><\/pre>\n<p>\u9019\u884c\u4ee3\u78bc\u544a\u8a34\u4f3a\u670d\u5668\u4f7f\u7528UTF-8\u7de8\u78bc\u4f86\u8655\u7406\u8a72\u9801\u9762\u3002\u78ba\u4fdd\u5728\u6240\u6709JSP\u9801\u9762\u4e2d\u90fd\u5305\u542b\u9019\u4e00\u8a2d\u7f6e\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%ad%97%e7%ac%a6%e9%9b%86%e8%a8%ad%e7%bd%ae-jYccnqZZLt\">\u6578\u64da\u5eab\u7684\u5b57\u7b26\u96c6\u8a2d\u7f6e<\/h2>\n<p>\u9664\u4e86JSP\u9801\u9762\uff0c\u6578\u64da\u5eab\u7684\u5b57\u7b26\u96c6\u8a2d\u7f6e\u4e5f\u81f3\u95dc\u91cd\u8981\u3002\u4ee5MySQL\u70ba\u4f8b\uff0c\u5275\u5efa\u6578\u64da\u5eab\u6642\u53ef\u4ee5\u6307\u5b9a\u5b57\u7b26\u96c6\uff1a<\/p>\n<pre><code>CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;<\/code><\/pre>\n<p>\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u6578\u64da\u5eab\u80fd\u5920\u6b63\u78ba\u5b58\u5132\u548c\u8655\u7406\u4e2d\u6587\u5b57\u7b26\u3002\u82e5\u6578\u64da\u5eab\u5df2\u7d93\u5b58\u5728\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4fee\u6539\u5b57\u7b26\u96c6\uff1a<\/p>\n<pre><code>ALTER DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;<\/code><\/pre>\n<h2 id=\"jdbc%e9%80%a3%e6%8e%a5%e7%9a%84%e5%ad%97%e7%ac%a6%e7%b7%a8%e7%a2%bc%e8%a8%ad%e7%bd%ae-jYccnqZZLt\">JDBC\u9023\u63a5\u7684\u5b57\u7b26\u7de8\u78bc\u8a2d\u7f6e<\/h2>\n<p>\u5728\u4f7f\u7528JDBC\u9023\u63a5\u6578\u64da\u5eab\u6642\uff0c\u4e5f\u9700\u8981\u6307\u5b9a\u5b57\u7b26\u7de8\u78bc\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\u9023\u63a5\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre><code>String url = \"jdbc:mysql:\/\/localhost:3306\/mydb?useUnicode=true&amp;characterEncoding=UTF-8\";<\/code><\/pre>\n<p>\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u5728\u9023\u63a5\u6578\u64da\u5eab\u6642\u4f7f\u7528UTF-8\u7de8\u78bc\uff0c\u5f9e\u800c\u907f\u514d\u4e82\u78bc\u554f\u984c\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e6%8f%92%e5%85%a5%e7%a4%ba%e4%be%8b-jYccnqZZLt\">\u6578\u64da\u63d2\u5165\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728JSP\u4e2d\u63d2\u5165\u4e2d\u6587\u6578\u64da\u5230\u6578\u64da\u5eab\uff1a<\/p>\n<pre><code>\n&lt;% \n    String name = request.getParameter(\"name\");\n    Class.forName(\"com.mysql.cj.jdbc.Driver\");\n    Connection conn = DriverManager.getConnection(url, user, password);\n    String sql = \"INSERT INTO users (name) VALUES (?)\";\n    PreparedStatement pstmt = conn.prepareStatement(sql);\n    pstmt.setString(1, name);\n    pstmt.executeUpdate();\n    pstmt.close();\n    conn.close();\n%&gt;\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528\u4e86PreparedStatement\u4f86\u9632\u6b62SQL\u6ce8\u5165\uff0c\u540c\u6642\u78ba\u4fdd\u4e2d\u6587\u6578\u64da\u80fd\u5920\u6b63\u78ba\u63d2\u5165\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-jYccnqZZLt\">\u7e3d\u7d50<\/h2>\n<p>\u89e3\u6c7aJSP\u5beb\u5165\u6578\u64da\u5eab\u4e2d\u6587\u4e82\u78bc\u554f\u984c\u7684\u95dc\u9375\u5728\u65bc\u78ba\u4fdd\u5b57\u7b26\u7de8\u78bc\u7684\u4e00\u81f4\u6027\u3002\u901a\u904e\u6b63\u78ba\u8a2d\u7f6eJSP\u9801\u9762\u3001\u6578\u64da\u5eab\u5b57\u7b26\u96c6\u4ee5\u53caJDBC\u9023\u63a5\u7684\u5b57\u7b26\u7de8\u78bc\uff0c\u53ef\u4ee5\u6709\u6548\u907f\u514d\u4e82\u78bc\u554f\u984c\u7684\u767c\u751f\u3002\u5c0d\u65bc\u9700\u8981\u8655\u7406\u5927\u91cf\u4e2d\u6587\u6578\u64da\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684\u4f3a\u670d\u5668\u548c\u6578\u64da\u5eab\u914d\u7f6e\u4e5f\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u82e5\u60a8\u9700\u8981\u9032\u4e00\u6b65\u4e86\u89e3\u5982\u4f55\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u670d\u52d9\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u89e3\u6c7aJSP\u5beb\u5165\u6578\u64da\u5eab\u6642\u4e2d\u6587\u4e82\u78bc\u554f\u984c\uff0c\u63d0\u4f9b\u6709\u6548\u65b9\u6cd5\u8207\u6280\u5de7\uff0c\u78ba\u4fdd\u6578\u64da\u6b63\u78ba\u986f\u793a\u8207\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-163333","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/163333","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=163333"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/163333\/revisions"}],"predecessor-version":[{"id":163334,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/163333\/revisions\/163334"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=163333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=163333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=163333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}