- 拷貝MySQL的JDBC驅動到Tomcat的lib路徑下
- 配置全局數據源或者單個Web應用的局部數據源
局部數據源
在Tomcat的conf/Catalina/localhost下的 app.xml中添加
< Context path ="/test" docBase ="test" debug ="5" reloadable ="true" > < Resource name ="jdbc/test" auth ="Container" type ="javax.sql.DataSource" factory ="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory" driverClassName ="com.mysql.jdbc.Driver" url ="jdbc:mysql://localhost:3306/你的DB名字" username ="用戶名" password ="密碼" maxActive ="20" maxIdle ="10" maxWait ="-1" /> </ Context >
?
全局數據源
/conf/server.xml? <Context>元素,添加如上<Resource/>元素
3. 在Tomcat4.x或者之前的版本中需要在web.xml中添加
< resource-ref > < description > Datasource example </ description > < res-ref-name > jndi/test </ res-ref-name > < res-type > javax.sql.DataSource </ res-type > < res-auth > Container </ res-auth > </ resource-ref >
4、在Java代碼中使用
Context ctx = new InitialContext(); String strLookup = "java:comp/env/jdbc/test" ; DataSource ds = (DataSource) ctx.lookup(strLookup); Connection con = ds.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs =stmt.executeQuery("select * from user");
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
