开发环境:Eclipse luna、tomcat 7、Oracle
配置Oracle datasource步骤
第一步:打开tomcat目录下的 context.xml 文件,添加 Resource 标签内容
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- 3 Licensed to the Apache Software Foundation (ASF) under one or more 4 contributor license agreements. See the NOTICE file distributed with 5 this work for additional information regarding copyright ownership. 6 The ASF licenses this file to You under the Apache License, Version 2.0 7 (the "License"); you may not use this file except in compliance with 8 the License. You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 See the License for the specific language governing permissions and 16 limitations under the License. 17 --><!-- The contents of this file will be loaded for each web application --><Context> 18 19 <!-- Default set of monitored resources --> 20 <WatchedResource>WEB-INF/web.xml</WatchedResource> 21 22 <!-- Uncomment this to disable session persistence across Tomcat restarts --> 23 <!-- 24 <Manager pathname="" /> 25 --> 26 27 <!-- Uncomment this to enable Comet connection tacking (provides events 28 on session expiration as well as webapp lifecycle) --> 29 <!-- 30 <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> 31 --> 32 33 <!-- oracle datasource --> 34 <Resource name="jdbc/orcl" auth="Container" type="javax.sql.DataSource" 35 maxActive="100" maxIdel="30" maxWait="1000" 36 username="scott" password="goodluck" driverClassName="oracle.jdbc.driver.OracleDriver" 37 url="jdbc:oracle:thin://localhost:1521/orcl?allowMultiQueries=true"/> 38 39 </Context>
第二步:编辑工程下面的 web.xml文件,插入 resource-ref 标签内容
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>web01</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 <resource-ref> 14 <description>DB Connection</description> 15 <res-ref-name>jdbc/orcl</res-ref-name> 16 <res-type>javax.sql.DataSource</res-type> 17 <res-auth>Container</res-auth> 18 </resource-ref> 19 20 </web-app>
第三步:数据库连接程序中添加三行代码
1 Context sourceCtx = new InitialContext(); 2 DataSource ds = (DataSource) sourceCtx.lookup("java:comp/env/jdbc/orcl"); 3 conn = ds.getConnection();
第四步:测试程序
重构 Java Web MVC实例 数据库连接程序
1 package com.test.dao; 2 3 import java.sql.Connection; 4 import java.sql.SQLException; 5 6 import javax.naming.Context; 7 import javax.naming.InitialContext; 8 import javax.naming.NamingException; 9 import javax.sql.DataSource; 10 11 public class BaseDao { 12 Connection conn = null; 13 14 public BaseDao() { 15 } 16 17 public Connection dbConnection() throws SQLException{ 18 try { 19 Context sourceCtx = new InitialContext(); 20 DataSource ds = (DataSource) sourceCtx.lookup("java:comp/env/jdbc/orcl"); 21 conn = ds.getConnection(); 22 System.out.println("----> Connection Success!"); 23 } catch (NamingException e) { 24 e.printStackTrace(); 25 } 26 27 return conn; 28 } 29 30 public void dbDisconnection() throws SQLException{ 31 conn.close(); 32 System.out.println("----> Connection End!"); 33 } 34 }
重构调用处的代码
1 package com.test.service; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.util.ArrayList; 8 9 import com.test.bean.EmpBean; 10 import com.test.dao.BaseDao; 11 12 public class EmpService { 13 int idx = 1; 14 15 public ArrayList<EmpBean> getEmpList(EmpBean eb){ 16 17 Connection conn = null; 18 PreparedStatement pstmt = null; 19 ResultSet rs = null; 20 21 ArrayList<EmpBean> empList = new ArrayList<EmpBean>(); 22 23 BaseDao baseDao = new BaseDao(); 24 try { 25 conn = baseDao.dbConnection(); 26 } catch (SQLException e1) { 27 e1.printStackTrace(); 28 } 29 30 // 3. 执行SQL语句 31 StringBuffer sqlBf = new StringBuffer(); 32 sqlBf.setLength(0); 33 34 sqlBf.append("SELECT EMPNO "); 35 sqlBf.append(" , ENAME "); 36 sqlBf.append(" , JOB "); 37 sqlBf.append(" , MGR "); 38 sqlBf.append(" , TO_CHAR(HIREDATE, 'YYYYMMDD') HIREDATE "); 39 sqlBf.append(" , SAL "); 40 sqlBf.append(" , COMM "); 41 sqlBf.append(" , DEPTNO "); 42 sqlBf.append("FROM EMP "); 43 sqlBf.append("WHERE ENAME LIKE UPPER(?) || '%' "); 44 45 System.out.println(sqlBf.toString()); 46 47 try { 48 pstmt = conn.prepareStatement(sqlBf.toString()); 49 idx = 1; 50 pstmt.setString(idx++, eb.getEname()); 51 52 // 4. 获取结果集 53 rs = pstmt.executeQuery(); 54 while (rs.next()) { 55 EmpBean emp = new EmpBean(); 56 57 emp.setEmpNo(rs.getInt("empno")); 58 emp.setEname(rs.getString("ename")); 59 emp.setJob(rs.getString("job")); 60 emp.setMgr(rs.getInt("mgr")); 61 emp.setHiredate(rs.getString("hiredate")); 62 emp.setSal(rs.getDouble("sal")); 63 emp.setComm(rs.getDouble("comm")); 64 emp.setDeptno(rs.getInt("deptno")); 65 66 empList.add(emp); 67 } 68 } catch (SQLException e) { 69 e.printStackTrace(); 70 } 71 72 try { 73 baseDao.dbDisconnection(); 74 } catch (SQLException e) { 75 e.printStackTrace(); 76 } 77 return empList; 78 } 79 }
第五步:重新启动web工程测试已有程序是不是访问正常
参考文章:http://pengjianbo1.iteye.com/blog/503326