以tomcat6.0为例:
讲数据库驱动jar包导入到tomcat6.0的/lib下,修改以下类
//连接数据库的类
class connectDB:
public static Connection getConnection(){
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/restrant");
conn = ds.getConnection();
}
在你的项目的配置文件web.xml中增加下面一段:
web.xml
<resource-ref>
<res-ref-name>jdbc/restrant </res-ref-name>
<res-type>javax.sql.DataSource </res-type>
<res-auth>Container</res-auth>
</resource-ref>
最后,在tomcat/webapps/yourpro/META-INF/下增加context.xml文件
新建txt文件,复制下面代码,重命名为context.xml
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Resource name="jdbc/restrant"
type="javax.sql.DataSource"
//增加数据库驱动,我的是sqlserver2005
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
//指定数据库,并定义登录名和密码
url="jdbc:sqlserver://localhost:1433;databaseName=restrant"
username="sa" password="sa" maxActive="200" maxIdle="10"
maxWait="-1"/>
</Context>
重新启动服务器
结束