一 server.xml
- <Context docBase="BCS2" path="/BCS2" reloadable="true" source="org.eclipse.jst.jee.server:BCS2">
- <Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" maxIdle="10" maxWait="1000" name="jdbc/uinfo" password="mysql" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/test?autoReconnect=true" username="root"/>
- </Context>
二项目web.xml
- <resource-ref>
- <res-ref-name>jdbc/uinfo</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
三DB
- public static Connection getConnection(){
- DataSource ds;
- try {
- Context ctx=new InitialContext();
- ds = (DataSource) ctx.lookup("java:comp/env/jdbc/uinfo");
- con = ds.getConnection();
- } catch (NamingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return con;
- }
注意:一定要将mysql-connector-java-5.1.7-bin.jar拷贝到tomcat的lib下