调用工具类的方法:
/**
* sql语句执行update、insert、delete方法
* @param sql
* @param param
* @return
* @throws Exception
*/
public static int executeUpdate(String sql, Object[] param) throws Exception {
Connection con = null;
PreparedStatement pstmt = null;
int updateCount = 0;
try {
con = DBConnection.getConnection();
pstmt = con.prepareStatement(sql);
for (int i = 0; i < param.length; i++) {
pstmt.setObject(i + 1, param[i]);
}
updateCount = pstmt.executeUpdate();
} finally {
release(pstmt, con);
}
return updateCount;
}
PS:以下方法仅仅是一个简单示例,在实际使用过程中请注意配合分页使用,否则数据量大的话将会对服务器造成很大压力。
/**
* 查询语句返回List<Map>
* @param sql sql语句
* @param param 参数
* @return
*/
public static List<Map<String, Object>> executeSelect(String sql, Object[] param) {
List<Map<String, Object>> resultMap = new ArrayList<Map<String, Object>>();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = DBConnection.getConnection();
pstmt = con.prepareStatement(sql);
for (int i = 0; i < param.length; i++) {
pstmt.setObject(i + 1, param[i]);
}
rs = pstmt.executeQuery();
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
while (rs.next()) {
Map<String, Object> rowData = new HashMap<String, Object>();
for (int i = 1; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), rs.getObject(i));
}
resultMap.add(rowData);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
release(rs, pstmt, con);
}
return resultMap;
}
DBConnection工具类:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
public class DBConnection {
private static final String DRIVER;
private static final String URL;
private static final String USER;
private static final String PASSWORD;
static {
DRIVER = ResourceBundle.getBundle("dbconfig").getString("driver");
URL = ResourceBundle.getBundle("dbconfig").getString("url");
USER = ResourceBundle.getBundle("dbconfig").getString("user");
PASSWORD = ResourceBundle.getBundle("dbconfig").getString("password");
}
public static void loadDriver() {
try {
Class.forName(DRIVER);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
throw new RuntimeException("驱动加载失败!");
}
}
public static Connection getConnection() {
Connection con = null;
try {
loadDriver();
con = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
public static void release(PreparedStatement pstmt, Connection con) {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pstmt = null;
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
con = null;
}
}
public static void release(ResultSet rs, PreparedStatement pstmt,Connection con) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rs = null;
}
release(pstmt, con);
}
}
使用线程池 DBConnPool 工具类:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DBConnPool {
private static DataSource DS;
// 获取数据源
public static DataSource getDataSource() {
Context initCtx;
try {
initCtx = new InitialContext();
// 查找java jndi容器对象时,必须先找 java:comp/env(路径固定 是java默认的命名空间)
Context envCtx = (Context) initCtx.lookup("java:comp/env");
// 查找自定义name 对应对象 ,这里写查找name
DS = (DataSource) envCtx.lookup("jdbc/oracle");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return DS;
}
// 快速建立数据库连接
public static Connection getConnection() throws SQLException {
Connection conn = null;
if (DS == null) {
DS = getDataSource();
}
try {
if (DS != null) {
conn = DS.getConnection();
}
} catch (Exception e) {
// 用JDBC的形式获取数据库连接对象 加载驱动,获取连接
}
return conn;
}
// 释放insert update delete资源
public static void release(Statement stmt, Connection conn) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn = null;
}
}
// 释放查询资源
public static void release(ResultSet rs, Statement stmt, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rs = null;
}
release(stmt, conn);
}
}