在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**"
保持一致