tomcat配置oracle数据源 帮帮忙!还有是不是在context.xml中配数据源的

项目的web.xml中
图片说明
tomcat的config下的context.xml中

WEB-INF/web.xml

    <Resource        --------**从这开始是我自己加的**
          name="jdbc/oracle"
          type="javax.sql.DataSource"
          driverClassName="oracle.jdbc.driver.OracleDriver"
          maxIdle="2"
          maxWait="5000"
          username="ses"
          password="ses"
          url="jdbc:oracle:thin:@localhost:1521:orcl"
          maxActive="4"/>     ----------**结束**

java代码获取数据源
private DataSource getDataSource() {
DataSource ds = null;
try {
Context ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/oracle");
} catch (Exception e) {
System.out.println("数据源获取失败");
e.printStackTrace();
}
return ds;
}

报错:javax.naming.NameNotFoundException: Name [jdbc/oracle] is not bound in this Context. Unable to find [jdbc].

在context之间加入

``` maxActive="100" maxIdle="30" maxWait="10000" username="oracle" password="apache"
driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@localhost:1521:ORCL" />

在src下建立工具类java
package cn.voicecyber.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class BaseDao {
    protected Connection conn;
    private ResultSet rs;

    // 连接数据库
    protected Connection getConnection() {
        try {
            Context cxt = new InitialContext();
            DataSource ds = (DataSource) cxt
                    .lookup("java:comp/env/jdbc/oracle");
            this.conn = ds.getConnection();
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    // 获得结果集,执行select语句
    public ResultSet getResultSet(String sql, Object[] prames) {
        this.getConnection();
        try {
            PreparedStatement smt = conn.prepareStatement(sql);
            if (prames != null) {
                for (int i = 0; i < prames.length; i++) {
                    smt.setObject((i + 1), prames[i]);
                }
            }
            this.rs = smt.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }

    // 执行insert,update,delete 语句
    public boolean getupload(String sql, Object[] prames) {
        int num = 0;
        boolean flag = false;
        this.getConnection();
        try {
            PreparedStatement smt = conn.prepareStatement(sql);
            if (prames != null) {
                for (int i = 0; i < prames.length; i++) {
                    smt.setObject((i + 1), prames[i]);
                }
            }
            num = smt.executeUpdate();
            if (num == 1) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;

    }

    // 关闭资源
    public void close() {
        try {
            if (rs != null) {
                rs.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}



需要注意的是:工具类中连接数据库    DataSource ds = (DataSource) cxt.lookup("java:comp/env/jdbc/**oracle**");与Resource name="jdbc/**oracle**"
保持一致