{"id":97120,"date":"2024-10-20T15:10:23","date_gmt":"2024-10-20T07:10:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/97120\/"},"modified":"2024-10-20T15:10:24","modified_gmt":"2024-10-20T07:10:24","slug":"p%e8%88%87mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%ae%8c%e7%be%8e%e9%80%a3%e6%8e%a5%e6%95%99%e7%a8%8b-jsp-mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/97120\/","title":{"rendered":"P\u8207MySQL\u6578\u64da\u5eab\u7684\u5b8c\u7f8e\u9023\u63a5\u6559\u7a0b (jsp mysql\u6578\u64da\u5eab\u914d\u7f6e)"},"content":{"rendered":"<h1 id=\"p%e8%88%87mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%ae%8c%e7%be%8e%e9%80%a3%e6%8e%a5%e6%95%99%e7%a8%8b-jsp-mysql%e6%95%b8%e6%93%9a%e5%ba%ab%e9%85%8d%e7%bd%ae-VmIBJGYvdr\">P\u8207MySQL\u6578\u64da\u5eab\u7684\u5b8c\u7f8e\u9023\u63a5\u6559\u7a0b (jsp mysql\u6578\u64da\u5eab\u914d\u7f6e)<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u958b\u767c\u4e2d\uff0cJava Server Pages (JSP) \u548c MySQL \u6578\u64da\u5eab\u7684\u7d50\u5408\u662f\u975e\u5e38\u5e38\u898b\u7684\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u6307\u5c0e\u60a8\u5982\u4f55\u5728 JSP \u4e2d\u914d\u7f6e MySQL \u6578\u64da\u5eab\u7684\u9023\u63a5\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u7bc4\u4f8b\u548c\u6b65\u9a5f\uff0c\u5e6b\u52a9\u60a8\u9806\u5229\u5b8c\u6210\u9019\u4e00\u904e\u7a0b\u3002<\/p>\n<h2 id=\"%e7%92%b0%e5%a2%83%e6%ba%96%e5%82%99-VmIBJGYvdr\">\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\u8edf\u4ef6\uff1a<\/p>\n<ul>\n<li>Java Development Kit (JDK)<\/li>\n<li>Apache Tomcat \u6216\u5176\u4ed6 JSP \u652f\u6301\u7684\u4f3a\u670d\u5668<\/li>\n<li>MySQL \u6578\u64da\u5eab<\/li>\n<li>MySQL Connector\/J\uff08JDBC \u9a45\u52d5\uff09<\/li>\n<\/ul>\n<h2 id=\"%e6%ad%a5%e9%a9%9f%e4%b8%80%ef%bc%9a%e5%ae%89%e8%a3%9d-mysql-%e6%95%b8%e6%93%9a%e5%ba%ab-VmIBJGYvdr\">\u6b65\u9a5f\u4e00\uff1a\u5b89\u88dd MySQL \u6578\u64da\u5eab<\/h2>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88dd MySQL \u6578\u64da\u5eab\u3002\u53ef\u4ee5\u5f9e <a href=\"https:\/\/dev.mysql.com\/downloads\/mysql\/\" rel=\"nofollow noopener\" target=\"_blank\">MySQL \u5b98\u65b9\u7db2\u7ad9<\/a> \u4e0b\u8f09\u4e26\u5b89\u88dd\u3002\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u60a8\u9700\u8981\u5275\u5efa\u4e00\u500b\u6578\u64da\u5eab\u548c\u7528\u6236\u3002<\/p>\n<pre><code>CREATE DATABASE mydatabase;\nCREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';\nGRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';\nFLUSH PRIVILEGES;<\/code><\/pre>\n<h2 id=\"%e6%ad%a5%e9%a9%9f%e4%ba%8c%ef%bc%9a%e4%b8%8b%e8%bc%89-mysql-connector-j-VmIBJGYvdr\">\u6b65\u9a5f\u4e8c\uff1a\u4e0b\u8f09 MySQL Connector\/J<\/h2>\n<p>MySQL Connector\/J \u662f\u4e00\u500b JDBC \u9a45\u52d5\u7a0b\u5e8f\uff0c\u5141\u8a31 Java \u61c9\u7528\u7a0b\u5e8f\u8207 MySQL \u6578\u64da\u5eab\u9032\u884c\u901a\u4fe1\u3002\u60a8\u53ef\u4ee5\u5f9e <a href=\"https:\/\/dev.mysql.com\/downloads\/connector\/j\/\" rel=\"nofollow noopener\" target=\"_blank\">MySQL Connector\/J \u5b98\u65b9\u7db2\u7ad9<\/a> \u4e0b\u8f09\u6700\u65b0\u7248\u672c\u3002\u4e0b\u8f09\u5f8c\uff0c\u5c07 JAR \u6587\u4ef6\u653e\u7f6e\u5728 Tomcat \u7684 lib \u76ee\u9304\u4e2d\u3002<\/p>\n<h2 id=\"%e6%ad%a5%e9%a9%9f%e4%b8%89%ef%bc%9a%e9%85%8d%e7%bd%ae-jsp-%e9%a0%81%e9%9d%a2-VmIBJGYvdr\">\u6b65\u9a5f\u4e09\uff1a\u914d\u7f6e JSP \u9801\u9762<\/h2>\n<p>\u5728\u60a8\u7684 JSP \u9801\u9762\u4e2d\uff0c\u60a8\u9700\u8981\u52a0\u8f09 JDBC \u9a45\u52d5\u4e26\u5efa\u7acb\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>&lt;%@ page import=\"java.sql.*\" %&gt;\n&lt;% \n    String url = \"jdbc:mysql:\/\/localhost:3306\/mydatabase\";\n    String user = \"myuser\";\n    String password = \"mypassword\";\n    Connection conn = null;\n    \n    try {\n        Class.forName(\"com.mysql.cj.jdbc.Driver\");\n        conn = DriverManager.getConnection(url, user, password);\n        out.println(\"\u6210\u529f\u9023\u63a5\u5230\u6578\u64da\u5eab\uff01\");\n    } catch (SQLException e) {\n        out.println(\"\u6578\u64da\u5eab\u9023\u63a5\u5931\u6557: \" + e.getMessage());\n    } catch (ClassNotFoundException e) {\n        out.println(\"JDBC \u9a45\u52d5\u672a\u627e\u5230: \" + e.getMessage());\n    } finally {\n        if (conn != null) {\n            try {\n                conn.close();\n            } catch (SQLException e) {\n                out.println(\"\u95dc\u9589\u9023\u63a5\u6642\u51fa\u932f: \" + e.getMessage());\n            }\n        }\n    }\n%&gt;<\/code><\/pre>\n<h2 id=\"%e6%ad%a5%e9%a9%9f%e5%9b%9b%ef%bc%9a%e5%9f%b7%e8%a1%8c-sql-%e6%9f%a5%e8%a9%a2-VmIBJGYvdr\">\u6b65\u9a5f\u56db\uff1a\u57f7\u884c SQL \u67e5\u8a62<\/h2>\n<p>\u4e00\u65e6\u6210\u529f\u9023\u63a5\u5230\u6578\u64da\u5eab\uff0c\u60a8\u53ef\u4ee5\u57f7\u884c SQL \u67e5\u8a62\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u67e5\u8a62\u6578\u64da\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>&lt;% \n    String query = \"SELECT * FROM mytable\";\n    Statement stmt = null;\n    ResultSet rs = null;\n    \n    try {\n        stmt = conn.createStatement();\n        rs = stmt.executeQuery(query);\n        \n        while (rs.next()) {\n            out.println(\"\u6578\u64da: \" + rs.getString(\"column_name\") + \"&lt;br&gt;\");\n        }\n    } catch (SQLException e) {\n        out.println(\"\u67e5\u8a62\u5931\u6557: \" + e.getMessage());\n    } finally {\n        if (rs != null) try { rs.close(); } catch (SQLException e) {}\n        if (stmt != null) try { stmt.close(); } catch (SQLException e) {}\n    }\n%&gt;<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-VmIBJGYvdr\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u60a8\u61c9\u8a72\u80fd\u5920\u6210\u529f\u5730\u5728 JSP \u4e2d\u914d\u7f6e MySQL \u6578\u64da\u5eab\u7684\u9023\u63a5\u3002\u9019\u7a2e\u7d44\u5408\u4e0d\u50c5\u80fd\u5920\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u9084\u80fd\u5920\u7c21\u5316\u6578\u64da\u7ba1\u7406\u7684\u904e\u7a0b\u3002\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u4f86\u904b\u884c\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\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\u670d\u52d9\uff0c\u60a8\u90fd\u53ef\u4ee5\u5728\u6211\u5011\u7684\u7db2\u7ad9\u4e0a\u627e\u5230\u5408\u9069\u7684\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5b8c\u7f8e\u9023\u63a5P\u8207MySQL\u6578\u64da\u5eab\uff0c\u638c\u63e1JSP\u6578\u64da\u5eab\u914d\u7f6e\u7684\u8a73\u7d30\u6b65\u9a5f\uff0c\u63d0\u5347\u4f60\u7684\u958b\u767c\u6280\u80fd\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-97120","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97120","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=97120"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97120\/revisions"}],"predecessor-version":[{"id":97121,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97120\/revisions\/97121"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=97120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=97120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=97120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}