📄 generaldao.java
字号:
package cn.edu.hlju.oa.kygl.db.dao;
import cn.edu.hlju.oa.kygl.db.entities.GeneralEntity;
import cn.edu.hlju.oa.kygl.db.DBConnection;
import java.sql.*;
import java.util.*;
/**
* 规定了绝大多数DAO应该完成的工作
*/
public abstract class GeneralDao
{
/**
* 加入新的纪录
* @param entity GeneralEntity 表示纪录的实体类
* @throws Exception
*/
public abstract void addNew(GeneralEntity entity) throws Exception;
/**
* 对纪录进行更新
* @param entity GeneralEntity 表示纪录的实体类
* @throws Exception
*/
public abstract void update(GeneralEntity entity,String whereCondition) throws Exception;
/**
* 删除纪录
* @throws Exception
*/
public abstract void delete(String whereCondition) throws Exception;
/**************************************
*
* @param startFrom int 开始查询的位置
* @param count int 要查询的数据量
* @param orderPropertyName String 要排序的数姓名
* @param tableName String 表名
* @return List 查询数据
* @throws Exception
*************************************/
public List selectAll(int startFrom, int count,String orderPropertyName,String tableName) throws Exception{
PreparedStatement p;
Connection conn;
String sql;
if (count > 0) {
sql="select * from "+ tableName + " order by "+ orderPropertyName +" limit ?,?"; //MySql的
conn=DBConnection.getConnection();
p=conn.prepareStatement(sql);
p.setInt(1, startFrom);
p.setInt(2, count);
} else {
sql="select * from " + tableName;
conn=DBConnection.getConnection();
p=conn.prepareStatement(sql);
}
ResultSet rs = p.executeQuery();
List list = this.fillList(rs);
rs.close();
p.close();
DBConnection.releaseConnection(conn);
//返回数据列表
return list;
}
/**
* 将数据装入一个List中
* @param rs ResultSet
* @return List
* @throws Exception
*/
public abstract List fillList(ResultSet rs) throws Exception ;
/**
* 获得全部数据的方法
* @param tableName String
* @return List
* @throws Exception
*/
public List selectAll(String orderPropertyName,String tableName) throws Exception {
return selectAll(0, 0, orderPropertyName,tableName);
}
/************************
* 获得某一个表中的记录的数量
***********************/
public int selectCount(String tableName) throws Exception{
PreparedStatement p;
Connection conn;
String sql;
int count;
sql="select Count(*) from " + tableName;
conn=DBConnection.getConnection();
p=conn.prepareStatement(sql);
ResultSet rs = p.executeQuery();
rs.next();
count=rs.getInt(1);
//关闭连接
rs.close();
p.close();
DBConnection.releaseConnection(conn);
return count;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -