日期:2014-05-16 浏览次数:20555 次
经过前面的一篇文章的学习、学会了利用Class.formName("")去获取Class对象、在通过Class对象提供的静态方法、获取类或接口的字段、方法、构造这些成员。
了解了反射的一些基础、个人觉得学习编程应该充分的动起手来。在使用过Hibernate的查询过后、突然觉得普通的JDBC查询对查询结果的封装很是麻烦!
于是仿造它、构建一个简单的JDBC查询。
数据库连接类:
/**
* 数据连接类
* @author 胡汉三
*
*/
public class UtilDao {
static Properties properties = null;
public UtilDao(){
//读取属性文件
properties = new Properties();
java.io.InputStream in = (java.io.InputStream) this.getClass()
.getResourceAsStream("/mysqlDB.properties");
try {
properties.load(in);
} catch (IOException ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
public Connection getConn(){
Connection connection = null;
try{
Class.forName(properties.getProperty("DBDriver"));
connection = DriverManager.getConnection(properties.getProperty("url"),properties.getProperty("name"),properties.getProperty("pass"));
}catch (Exception err) {
System.out.println("连接ConDB-->getCon()____JDBC错误!");
err.printStackTrace();
return null;
}
return connection;
}
/**
* 以下是关闭重载方法
* @param rs
* @param st
* @param cs
* @param conn
* @throws SQLException
*/
public void closeAll(ResultSet rs,Statement st ,CallableStatement cs ,Connection conn) throws SQLException{
if(rs!=null){
rs.close();
}
if(st!=null){
st.close();
}
if(cs!=null){
cs.close();
}
if(conn!=null){
conn.close();
}
}
public void closeAll(ResultSet rs,Statement st,Connection conn) throws SQLException{
if(rs!=null){
rs.close();
}
if(st!=null){
st.close();
}
if(conn!=null){
conn.close();
}
}
public void closeAll(ResultSet rs,PreparedStatement ps,Connection conn) throws SQLException{
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(conn!=null){
conn.close();
}
}
public void closeAll(PreparedStatement ps,Connection conn) throws SQLException{
if(ps!=null){
ps.close();
}
if(conn!=null){
conn.close();
}
}
public void closeAll(Statement st,Connection conn) throws SQLException{
if(st!=null){
st.close();
}
if(conn!=null){
conn.close();
}
}
public void closeAll(Connection conn) throws SQLException{
if(conn!=null){
conn.close();
}
}
}属性文件:
DBDriver=com.mysql.jdbc.Driver url=jdbc\:mysql\://localhost\:3306/hzw name=root pass=root characterEncoding=utf8数据层接口:
/**
* 访问数据方法接口
* @author 胡汉三
* @param <T>
*
*/
@SuppressWarnings("unchecked")
public interface IDao<T> {
/**
* 分页查询方法
* @param objClass 类对象
* @param sql 查询语句
* @param params 参数
* @return 分页数据
* @throws Exception
*/
public List<T> findList(Object objClass,String sql, List params)throws Exception;
/**
* 查询一条数据
* @param objClass 类对象
* @param sql 查询语句
* @param params 参数
* @return 一条数据信息
* @throws Exception
*/
public T findInfo(Object objClass,String sql ,List params)throws Exception;
}
数据层接口实现:
/**
* 访问数据方法实现类
* @author 胡汉三
*
* @param <T>
*/
@SuppressWarnings({"unchecked","unused"})
public class DaoImpl<T> implements IDao<T> {
private UtilDao dao = new UtilDao();
private Connection conn = null;
private ResultSet rs = null;
private PreparedStatement ps = null;
/*
* 查询一条数据
*/
public T findInfo(Object objClass,String sql, List params) throws Exception {
Class c = objClass.getClass();
try{
conn = dao.getConn();
ps = conn.prepareStatement(sql);
for (int i = 0; i < params.size(); i++) {
if(params.get(i)!=null){
Object obj = params.get(i);
if(obj.getClass().