普通项目(非javaweb项目),项目文件路径如图
database.properties
driver=oracle.jdbc.driver.OracleDriver
username=neu
password=oracle
url=jdbc\:oracle\:thin\:@127.0.0.1\:1521\:ORACLE
DBUtil
package com.duoduo.util;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;
public class DBUtil {
private static Connection connection;
private static PreparedStatement pstmt;
private static ResultSet resultSet;
private static String password;
private static String username;
private static String driver;
private static String url;
static {
try {
init();
register();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/***
* 注册驱动 *
**/
private static void register() throws ClassNotFoundException {
Class.forName(driver);
}
/***
* 初始化配置 *
**/
private static void init() throws IOException {
Properties properties = new Properties();
FileInputStream in = new FileInputStream(
"properties/database.properties");
properties.load(in);
in.close();
driver = properties.getProperty("driver");
username = properties.getProperty("username");
password = properties.getProperty("password");
url = properties.getProperty("url");
Logger logger = Logger.getLogger("com.duoduo.jdbc");
logger.info(driver);
logger.info(username);
logger.info(password);
logger.info(url);
}
/***
* 获得连接 *
**/
public static Connection getConnection2() throws SQLException {
// 确保资源已经释放
clear();
connection = DriverManager.getConnection(url, username, password);
return connection;
}
/***
* 获得连接 *
**/
public static void getConnection() throws SQLException {
// 确保资源已经释放
clear();
connection = DriverManager.getConnection(url, username, password);
// return connection;
}
/***
* 开始事务 *
**/
public static void startTrans(Connection conn) throws SQLException {
if (conn != null) {
conn.setAutoCommit(false);
}
}
public static void startTrans() throws SQLException {
if (connection != null) {
connection.setAutoCommit(false);
}
}
/***
* 结束事务 *
*
* @throws SQLException
**/
public static void endTrans(Connection conn) throws SQLException {
if (conn != null) {
conn.commit();
}
}
public static void endTrans() throws SQLException {
if (connection != null) {
connection.commit();
}
}
/***
* 回滚事务 *
*
* @throws SQLException
**/
public static void rollbackTrans(Connection conn) throws SQLException {
if (conn != null) {
conn.rollback();
}
}
public static void rollbackTrans() throws SQLException {
if (connection != null) {
connection.rollback();
}
}
/***
* 清空资源 *
*
* @throws SQLException
**/
public static void clear(Connection conn) throws SQLException {
if (conn != null) {
conn.close();
}
}
/***
* 清空资源 *
**/
public static void clear(Connection conn, PreparedStatement ps)
throws SQLException {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
}
/***
* 清空资源 *
**/
public static void clear(Connection conn, PreparedStatement ps, ResultSet rs)
throws SQLException {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
}
public static void clear() throws SQLException {
if (resultSet != null) {
resultSet.close();
resultSet = null;
}
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
if (connection != null) {
connection.close();
connection = null;
}
}
private static void setParamsToPreparedStatement(PreparedStatement pstmt,
List<Object> params) throws SQLException {
int index = 1;
if (params != null && !params.isEmpty()) {
for (int i = 0; i < params.size(); i++) {
pstmt.setObject(index++, params.get(i));
}
}
}
public static Map<String, Object> findSimpleResult(String sql,
List<Object> params) throws SQLException {
Map<String, Object> map = new HashMap<String, Object>();
// index:占位符地址
pstmt = connection.prepareStatement(sql);
setParamsToPreparedStatement(pstmt, params);
resultSet = pstmt.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int col_len = metaData.getColumnCount();
while (resultSet.next()) {
for (int i = 0; i < col_len; i++) {
String cols_name = metaData.getColumnName(i + 1);
Object cols_value = resultSet.getObject(cols_name);
if (cols_value == null) {