预备工作:把mysql驱动包分别放到%TOMCAT_HOME%/lib 和你的应用的WEB-INF/lib文件夹下面
1、修改%TOMCAT_HOME%/conf/server.xml,添加如下内容(部分参数请本地化):
<Context path="/eshopping" docBase="eshopping" debug="0" reloadable="true" crossContext="true">
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver" maxIdle="20" maxWait="5000" username="root" password="root"
url="jdbc:mysql://localhost:3306/mydb1" maxActive="100" removeAbandoned="true"
removeAbandonedTimeout="60" logAbandoned="true"/>
</Context>
2、应用实例如下:
private void init()
{
try
{
// Class.forName("com.mysql.jdbc.Driver").newInstance();
// String url = "jdbc:mysql://localhost/mydb1";
// conn = DriverManager.getConnection(url,"root","root");
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
conn = ds.getConnection();
}
catch(Exception e)
{
e.printStackTrace();
}
}
3、重启tomcat即可。