server.xml
?<Context path="/bookstore" docBase="bookstore" debug="0"?????reloadable="true">
?????<Resource ????</Context> |
web.xml
<?xml version="1.0" encoding="UTF-8"?> ?? <web-app> ??? <welcome-file-list> ??????? <welcome-file>Dbjsp1.jsp</welcome-file> ??????? <welcome-file>index.htm</welcome-file> ??????? <welcome-file>index.jsp</welcome-file> ??? </welcome-file-list> ?<resource-ref> ??<description>DB connection</description> ??<res-ref-name>jdbc/BookDB</res-ref-name> ??<res-type>javax.sql.Datasource</res-type> ??<res-auth>Container</res-auth> ??<res-sharing-scope>Shareable</res-sharing-scope> ?</resource-ref> </web-app> |
//數據庫連接代碼
??Connection con; ??Statement stmt; ??ResultSet rs; ?? ??Context ctx=new InitialContext(); ??DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/BookDB"); ??con=ds.getConnection(); ?? ??stmt=con.createStatement(); |