配置程序的运行目录与JDBC数据源: <Context path="/test" docBase="F:/JSPLesson/test" reloadable="true"> <Resource name="jdbc/bookstore" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="root" password="123456" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/bookstore?autoReconnect=true"/> </Context> JSP中通过如下方式得到数据库连接: <% Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/bookstore"); Connection conn = ds.getConnection(); %>