config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>数据库连接配置</comment>
<entry key="driver">oracle.jdbc.driver.OracleDriver</entry>
<entry key="url">jdbc:oracle:thin:@localhost:1521:orcl</entry>
<entry key="user">test</entry>
<entry key="pass">123456</entry>
</properties>
ConfigReader
import java.io.IOException;
import java.util.Properties;
/** 配置文件读取类 */
public class ConfigReader {
private Properties prop = null;
private ConfigReader(){
prop = new Properties();
try {
prop.loadFromXML(ConfigReader.class.getResourceAsStream("config.xml"));
} catch (IOException e) {
e.printStackTrace();
}
}
private static ConfigReader obj = null;
public static ConfigReader getConfigReader(){
//懒汉式
if(obj == null){
obj = new ConfigReader();
}
return obj;
}
public String getProperty(String key){
return prop.getProperty(key);
}
}
BaseDao
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class BaseDao {
public static final String DRIVER = ConfigReader.getConfigReader()
.getProperty("driver");
public static final String URL = ConfigReader.getConfigReader()
.getProperty("url");
public static final String USER = ConfigReader.getConfigReader()
.getProperty("user");
public static final String PASS = ConfigReader.getConfigReader()
.getProperty("pass");
/**
* 获取分页的页数
* @param tableName 表名
* @param rows 每页的行数
* @param condition 分页的查询条件(可变参数 可传参数 无参数 当作字符参数数组看 可以为空)
* @return
* @throws SQLException
*/
public static int getPageCount(String tableName, int rows,
String... condition) throws SQLException {
String cond = "";
if(condition != null || !"".equals(condition)){
cond = " where " + condition[0];
}
String sql = "select count(*) from " + tableName;
sql += cond;
//System.out.println("************************"+sql);
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
BaseDao dao = new BaseDao();
try {
conn = dao.getConn();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
rs.next();
// 总行数
int count = rs.getInt(1);
// 每页行数
int pageCount = count / rows;
if (count % rows > 0)
pageCount++;
return pageCount;
} finally {
dao.closeAll(conn, ps, rs);
}
}
/**
* 加载驱动,获取连接对象
*
* @return
* @throws SQLException
*/
public Connection getConn() throws SQLException {
// 1.加载驱动
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
throw new SQLException(e);
}
// 2.获得连接对象
return DriverManager.getConnection(URL, USER, PASS);
}
/**
* 关闭连接及相关对象
*
* @param conn
* @param s
* @param rs
* @throws SQLException
*/
public void closeAll(Connection conn, Statement s, ResultSet rs)
throws SQLException {
if (rs != null)
rs.close();
if (s != null)
s.close();
if (conn != null)
conn.close();
}
/**
* 统一执行增删改命令的方法
*
* @param sql
* @param params
* @return
* @throws SQLException
*/
public int executeUpdateSQL(String sql, Object[] params)
throws SQLException {
Connection conn = null;
PreparedStatement s = null;
try {
conn = this.getConn();
s = conn.prepareStatement(sql);
if (params != null) {
for (int i = 0; i < params.length; i++) {
Object obj = params[i];
if (obj instanceof java.util.Date) { // 考虑日期的转换
obj = new java.sql.Timestamp(
((java.util.Date) obj).getTime());
}
s.setObject(i + 1, obj);
}
}
return s.executeUpdate();
} finally {
this.closeAll(conn, s, null);
}
}
public static void main(String[] args) {
BaseDao dao = new BaseDao();
Connection conn = null;
try {
conn = dao.getConn();
System.out.println("打开连接OK!");
dao.closeAll(conn, null, null);
System.out.println("关闭连接OK!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
还看到一个不错的封装
http://blog.csdn.net/weixin_36380516/article/details/72904214