下面是context.xml才设置 <!-- The contents of this file will be loaded for each web application --> <Context> <Resource name="jdbc/DBtest" auth="Container" type="javax.sql.DataSource" maxActive="30" maxIdle="10" maxWait="10000" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/hsp" username="root" password="root" /> <WatchedResource>WEB-INF/web.xml</WatchedResource> </Context>
下面是连接数据库的代码: Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/hsp"); con = ds.getConnection();