{"id":61833,"date":"2024-10-13T14:04:48","date_gmt":"2024-10-13T06:04:48","guid":{"rendered":"https:\/\/server.hk\/cnblog\/61833\/"},"modified":"2024-10-13T14:04:49","modified_gmt":"2024-10-13T06:04:49","slug":"java-jsp-tomcat6-mysql%e9%80%a3%e6%8e%a5%e6%b1%a0%e7%9a%84%e6%ad%a3%e7%a2%ba%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/61833\/","title":{"rendered":"java jsp tomcat6 MySQL\u9023\u63a5\u6c60\u7684\u6b63\u78ba\u914d\u7f6e"},"content":{"rendered":"<h1 id=\"java-jsp-tomcat6-mysql%e9%80%a3%e6%8e%a5%e6%b1%a0%e7%9a%84%e6%ad%a3%e7%a2%ba%e9%85%8d%e7%bd%ae-zVUvBiYeOz\">Java JSP Tomcat6 MySQL\u9023\u63a5\u6c60\u7684\u6b63\u78ba\u914d\u7f6e<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u958b\u767c\u4e2d\uff0cJava JSP\uff08JavaServer Pages\uff09\u8207MySQL\u6578\u64da\u5eab\u7684\u7d50\u5408\u662f\u975e\u5e38\u5e38\u898b\u7684\u3002\u70ba\u4e86\u63d0\u9ad8\u61c9\u7528\u7684\u6027\u80fd\u548c\u53ef\u64f4\u5c55\u6027\uff0c\u4f7f\u7528\u9023\u63a5\u6c60\u4f86\u7ba1\u7406\u6578\u64da\u5eab\u9023\u63a5\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5728Tomcat6\u4e2d\u6b63\u78ba\u914d\u7f6eMySQL\u9023\u63a5\u6c60\uff0c\u4ee5\u78ba\u4fdd\u61c9\u7528\u7684\u9ad8\u6548\u904b\u884c\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e9%80%a3%e6%8e%a5%e6%b1%a0%ef%bc%9f-zVUvBiYeOz\">\u4ec0\u9ebc\u662f\u9023\u63a5\u6c60\uff1f<\/h2>\n<p>\u9023\u63a5\u6c60\u662f\u4e00\u7a2e\u7528\u65bc\u7ba1\u7406\u6578\u64da\u5eab\u9023\u63a5\u7684\u6280\u8853\u3002\u5b83\u901a\u904e\u9810\u5148\u5275\u5efa\u4e00\u5b9a\u6578\u91cf\u7684\u6578\u64da\u5eab\u9023\u63a5\uff0c\u4e26\u5728\u9700\u8981\u6642\u91cd\u8907\u4f7f\u7528\u9019\u4e9b\u9023\u63a5\uff0c\u5f9e\u800c\u6e1b\u5c11\u4e86\u6bcf\u6b21\u8acb\u6c42\u6642\u5275\u5efa\u548c\u92b7\u6bc0\u9023\u63a5\u7684\u958b\u92b7\u3002\u9019\u4e0d\u50c5\u63d0\u9ad8\u4e86\u6027\u80fd\uff0c\u9084\u6e1b\u5c11\u4e86\u8cc7\u6e90\u7684\u6d88\u8017\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87tomcat6%ef%bc%9f-zVUvBiYeOz\">\u70ba\u4ec0\u9ebc\u9078\u64c7Tomcat6\uff1f<\/h2>\n<p>Tomcat6\u662f\u4e00\u500b\u6d41\u884c\u7684\u958b\u6e90Servlet\u5bb9\u5668\uff0c\u652f\u6301Java EE\u6280\u8853\u3002\u5b83\u7684\u8f15\u91cf\u7d1a\u7279\u6027\u548c\u826f\u597d\u7684\u6027\u80fd\u4f7f\u5176\u6210\u70ba\u8a31\u591aJava Web\u61c9\u7528\u7684\u9996\u9078\u3002\u901a\u904e\u6b63\u78ba\u914d\u7f6e\u9023\u63a5\u6c60\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5145\u5206\u767c\u63eeTomcat6\u7684\u512a\u52e2\u3002<\/p>\n<h2 id=\"%e9%85%8d%e7%bd%aemysql%e9%80%a3%e6%8e%a5%e6%b1%a0%e7%9a%84%e6%ad%a5%e9%a9%9f-zVUvBiYeOz\">\u914d\u7f6eMySQL\u9023\u63a5\u6c60\u7684\u6b65\u9a5f<\/h2>\n<h3 id=\"1-%e6%b7%bb%e5%8a%a0mysql%e9%a9%85%e5%8b%95-zVUvBiYeOz\">1. \u6dfb\u52a0MySQL\u9a45\u52d5<\/h3>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u78ba\u4fddMySQL JDBC\u9a45\u52d5\u5df2\u7d93\u6dfb\u52a0\u5230Tomcat\u7684lib\u76ee\u9304\u4e2d\u3002\u60a8\u53ef\u4ee5\u5f9eMySQL\u7684\u5b98\u65b9\u7db2\u7ad9\u4e0b\u8f09\u6700\u65b0\u7684JDBC\u9a45\u52d5\uff0c\u7136\u5f8c\u5c07\u5176\u653e\u7f6e\u5728\u4ee5\u4e0b\u8def\u5f91\uff1a<\/p>\n<pre><code>\/path\/to\/tomcat\/lib\/mysql-connector-java-x.x.x.jar<\/code><\/pre>\n<h3 id=\"2-%e9%85%8d%e7%bd%aecontext-xml-zVUvBiYeOz\">2. \u914d\u7f6econtext.xml<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u9700\u8981\u5728Tomcat\u7684context.xml\u6587\u4ef6\u4e2d\u914d\u7f6e\u6578\u64da\u6e90\u3002\u8a72\u6587\u4ef6\u901a\u5e38\u4f4d\u65bc\u4ee5\u4e0b\u8def\u5f91\uff1a<\/p>\n<pre><code>\/path\/to\/tomcat\/conf\/context.xml<\/code><\/pre>\n<p>\u5728\u6b64\u6587\u4ef6\u4e2d\uff0c\u60a8\u53ef\u4ee5\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/p>\n<pre><code>&lt;Context&gt;\n    &lt;Resource name=\"jdbc\/MySQLDB\"\n              auth=\"Container\"\n              type=\"javax.sql.DataSource\"\n              maxActive=\"100\"\n              maxIdle=\"30\"\n              maxWait=\"10000\"\n              username=\"yourUsername\"\n              password=\"yourPassword\"\n              driverClassName=\"com.mysql.jdbc.Driver\"\n              url=\"jdbc:mysql:\/\/localhost:3306\/yourDatabase?useUnicode=true&amp;characterEncoding=UTF-8\"&gt;\n    &lt;\/Resource&gt;\n&lt;\/Context&gt;<\/code><\/pre>\n<p>\u5728\u9019\u88e1\uff0c\u60a8\u9700\u8981\u6839\u64da\u5be6\u969b\u60c5\u6cc1\u66ff\u63dbusername\u3001password\u548curl\u7684\u503c\u3002<\/p>\n<h3 id=\"3-%e5%9c%a8web-xml%e4%b8%ad%e9%85%8d%e7%bd%ae%e8%b3%87%e6%ba%90%e5%bc%95%e7%94%a8-zVUvBiYeOz\">3. \u5728web.xml\u4e2d\u914d\u7f6e\u8cc7\u6e90\u5f15\u7528<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u9700\u8981\u5728\u61c9\u7528\u7684web.xml\u6587\u4ef6\u4e2d\u5f15\u7528\u9019\u500b\u8cc7\u6e90\u3002\u9019\u6a23\uff0c\u60a8\u7684JSP\u9801\u9762\u6216Servlet\u5c31\u53ef\u4ee5\u4f7f\u7528\u9019\u500b\u6578\u64da\u6e90\u4e86\u3002\u4ee5\u4e0b\u662f\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;resource-ref&gt;\n    &lt;description&gt;DB Connection&lt;\/description&gt;\n    &lt;res-ref-name&gt;jdbc\/MySQLDB&lt;\/res-ref-name&gt;\n    &lt;res-type&gt;javax.sql.DataSource&lt;\/res-type&gt;\n    &lt;res-link&gt;jdbc\/MySQLDB&lt;\/res-link&gt;\n&lt;\/resource-ref&gt;<\/code><\/pre>\n<h3 id=\"4-%e4%bd%bf%e7%94%a8%e9%80%a3%e6%8e%a5%e6%b1%a0-zVUvBiYeOz\">4. \u4f7f\u7528\u9023\u63a5\u6c60<\/h3>\n<p>\u5728\u60a8\u7684JSP\u6216Servlet\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u904eJNDI\u67e5\u627e\u4f86\u7372\u53d6\u6578\u64da\u6e90\uff0c\u7136\u5f8c\u4f7f\u7528\u5b83\u4f86\u7372\u53d6\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>InitialContext ctx = new InitialContext();\nDataSource ds = (DataSource) ctx.lookup(\"java:comp\/env\/jdbc\/MySQLDB\");\nConnection conn = ds.getConnection();\n\/\/ \u4f7f\u7528\u9023\u63a5\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\nconn.close();<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-zVUvBiYeOz\">\u7e3d\u7d50<\/h2>\n<p>\u6b63\u78ba\u914d\u7f6eJava JSP\u3001Tomcat6\u548cMySQL\u7684\u9023\u63a5\u6c60\u4e0d\u50c5\u80fd\u63d0\u9ad8\u61c9\u7528\u7684\u6027\u80fd\uff0c\u9084\u80fd\u6709\u6548\u7ba1\u7406\u8cc7\u6e90\u3002\u901a\u904e\u4e0a\u8ff0\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u8a2d\u7f6e\u9023\u63a5\u6c60\uff0c\u5f9e\u800c\u70ba\u60a8\u7684\u61c9\u7528\u63d0\u4f9b\u7a69\u5b9a\u7684\u6578\u64da\u5eab\u652f\u6301\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684Java\u61c9\u7528\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to correctly configure a Java JSP application with Tomcat 6 and MySQL connection pooling for optimal performance and resource management.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-61833","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61833","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=61833"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61833\/revisions"}],"predecessor-version":[{"id":61834,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/61833\/revisions\/61834"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=61833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=61833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=61833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}