{"id":37,"date":"2013-01-31T18:36:59","date_gmt":"2013-01-31T10:36:59","guid":{"rendered":"http:\/\/www.yeetrack.com\/post\/2013-01-31\/tomcat%20mysql%E8%BF%9E%E6%8E%A5%E6%B1%A0%E9%85%8D%E7%BD%AE"},"modified":"2013-04-20T17:17:02","modified_gmt":"2013-04-20T09:17:02","slug":"tomcatmysql%e8%bf%9e%e6%8e%a5%e6%b1%a0%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/www.yeetrack.com\/?p=37","title":{"rendered":"tomcat mysql\u8fde\u63a5\u6c60\u914d\u7f6e"},"content":{"rendered":"<p><span>\u672c\u65b9\u6cd5\u9700\u8981\u4fee\u6539tomcat conf\u76ee\u5f55\u4e0b\u7684context.xml\u548c\u9879\u76ee\u76ee\u5f55\u4e0bWEB-INF\/web.xml\u4e24\u4e2a\u6587\u4ef6:<\/span><\/p>\n<p><span><\/span><br \/>context.xml\u5728&lt;context&gt;&lt;\/context&gt;\u4e4b\u95f4\u6dfb\u52a0\u8fde\u63a5\u6c60\u5982\u4e0b:<br \/>&lt;Resource name=&quot;jdbc\/mysql&quot; auth=&quot;Container&quot; <br \/>type=&quot;javax.sql.DataSource&quot; <\/p>\n<p><!--more--><\/p>\n<p>maxActive=&quot;50&quot; maxIdle=&quot;10&quot; maxWait=&quot;5000&quot; <br \/>username=&quot;\u4f60\u7684mysql\u7528\u6237&quot; password=&quot;\u4f60\u7684mysql\u5bc6\u7801&quot; <br \/>driverClassName=&quot;com.mysql.jdbc.Driver&quot;<br \/>url=&quot;jdbc:mysql:\/\/localhost\/test1 \/&gt;<\/p>\n<p>web.xml\u4e2d\u7684&lt;web-app&gt;&lt;\/web-app&gt;\u4e4b\u95f4\u52a0\u5165:<br \/>&lt;resource-ref&gt; <br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;description&gt;DB Connection&lt;\/description&gt; <br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;res-ref-name&gt;jdbc\/mysql&lt;\/res-ref-name&gt; <br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;res-type&gt;javax.sql.DataSource&lt;\/res-type&gt; <br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;res-auth&gt;Container&lt;\/res-auth&gt; <br \/> &nbsp;&nbsp;&nbsp;&lt;\/resource-ref&gt;<\/p>\n<p><span>\u6ce8\u610f\u7684\u5730\u65b9: context.xml\u6587\u4ef6\u4e2d\u7684name=&quot;jdbc\/mysql&quot;\u8981\u548cweb.xml\u4e2d\u7684&lt;res-ref-name&gt;jdbc\/mysql&lt;\/res-ref-name&gt;\u8981\u4e00\u81f4;<\/span><br \/>mysql \u7684jdbc\u9a71\u52a8\u590d\u5236\u5230\u914d\u7f6etomcat\u4e0b\u7684lib\u76ee\u5f55;<\/p>\n<p>OK,\u5230\u4e9b\u5c31\u914d\u7f6e\u5b8c\u4e86,\u4e0b\u6765\u5c31\u662f\u5199\u6d4b\u8bd5\u4ee3\u7801:<\/p>\n<p>\u6253\u5f00Eclipse\u5176\u624b\u5de5\u5efa\u4e2aWEB project(\u6211\u7684\u5de5\u7a0b\u540dmysql),\u52a0\u5165mysql JDBC\u9a71\u52a8,\u8bb0\u5f97\u8fd9\u4e2a\u4e00\u5b9a\u8981\u52a0\u5165;<\/p>\n<p>\u65b0\u5efajsp\u6587\u4ef6:<br \/>&lt;%@ page language=&quot;java&quot; pageEncoding=&quot;UTF-8&quot;%&gt;<br \/>&lt;%@ page import=&quot;java.sql.*&quot;%&gt; <br \/>&lt;%@ page import=&quot;javax.sql.*&quot;%&gt; <br \/>&lt;%@ page import=&quot;javax.naming.*&quot;%&gt;<br \/>&lt;!DOCTYPE HTML PUBLIC &quot;-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN&quot;&gt;<br \/>&lt;html&gt;<br \/> &nbsp;&nbsp;&lt;head&gt;<br \/>&lt;title&gt;mysql\u8fde\u63a5\u6c60\u6d4b\u8bd5&lt;\/title&gt;<br \/>&lt;meta http-equiv=&quot;pragma&quot; content=&quot;no-cache&quot;&gt;<br \/>&lt;meta http-equiv=&quot;cache-control&quot; content=&quot;no-cache&quot;&gt;<br \/>&lt;meta http-equiv=&quot;expires&quot; content=&quot;0&quot;&gt; <br \/>&lt;meta http-equiv=&quot;keywords&quot; content=&quot;keyword1,keyword2,keyword3&quot;&gt;<br \/>&lt;meta http-equiv=&quot;description&quot; content=&quot;This is my page&quot;&gt;<br \/>&lt;!--<br \/>&lt;link rel=&quot;stylesheet&quot; type=&quot;text\/css&quot; href=&quot;styles.css&quot;&gt;<br \/>--&gt; <br \/> &nbsp;&nbsp;&lt;\/head&gt;<br \/> &nbsp;<br \/> &nbsp;&nbsp;&lt;body&gt; <br \/>&lt;% &nbsp;<br \/>out.print(&quot;\u6d4b\u8bd5\u5f00\u59cb&lt;br&gt;&quot;); <br \/>DataSource ds = null; <br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try{ <br \/>InitialContext ctx=new InitialContext(); <br \/>ds=(DataSource)ctx.lookup(&quot;java:comp\/env\/jdbc\/mysql&quot;); <br \/>Connection conn = ds.getConnection(); &nbsp;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Statement stmt = conn.createStatement(); <br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String strSql = &quot; select * from pet&quot;; <br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ResultSet rs = stmt.executeQuery(strSql); <br \/> &nbsp;&nbsp;while(rs.next()){<br \/>out.print(rs.getString(1));<br \/>out.print(rs.getString(2));<br \/>out.print( rs.getString(&quot;birth&quot;));<br \/>out.println(&quot;&lt;br&gt;&quot;);<br \/>}<br \/>rs.close();<br \/>stmt.close();<br \/>conn.close();<br \/> &nbsp;&nbsp;&nbsp;}catch(Exception ex){<br \/> &nbsp;&nbsp;&nbsp;&nbsp;ex.printStackTrace();<br \/> &nbsp;&nbsp;&nbsp;} <br \/>%&gt; &nbsp;<\/p>\n<p> &nbsp;&nbsp;&lt;\/body&gt;<br \/>&lt;\/html&gt;<\/p>\n<p>\u6d4b\u8bd5\u6210\u529f\uff0c\u5728DAO\u5f00\u53d1\u65f6\uff0cDatabaseConnection\u7c7b\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u65b9\u6cd5\u9700\u8981\u4fee\u6539tomcat conf\u76ee\u5f55\u4e0b\u7684context.xml\u548c\u9879\u76ee\u76ee\u5f55\u4e0bWEB-INF\/web.xml\u4e24\u4e2a\u6587\u4ef6: context.xml\u5728&lt;context&gt;&lt;\/context&#038;&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[34],"tags":[8,7,5,13],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-software","tag-java","tag-7","tag-5","tag-13"],"views":2530,"_links":{"self":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=37"}],"version-history":[{"count":1,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions"}],"predecessor-version":[{"id":337,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions\/337"}],"wp:attachment":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}