最近做了一个小东西,对于接口实现类。发现很多都是一个套路,所以备份下,以后可以经常用到! 这几个是单纯查询的实现类。

来源:互联网 发布:java中原生态接口开发 编辑:程序博客网 时间:2024/04/24 18:28
/**
 * 收支汇总报表---收入(收支项目和金额)
 */
public List receiptsReportFormsFx1(String time1, String time2) {
// TODO Auto-generated method stub
List list = new ArrayList();
System.out.println("------------------");
//String time1 = 2011-01-1
try{
conn = ConfigUtil.getConnection();//调用configutil()连接oracle数据库
String sql = "select ab_name,ab_money from account_book "+
"where ab_fx=1 and (ab_time between to_Date(?,'yyyy-mm-dd') and to_Date(?,'yyyy-mm-dd'))";//对数据库信息进行查询
pstmt = conn.prepareStatement(sql);//创建一个 PreparedStatement 对象来将参数化的 SQL 语句发送到数据库。
pstmt.setString(1, time1);
pstmt.setString(2, time2);
pstmt.executeUpdate(); //在此 PreparedStatement 对象中执行 SQL 语句
rs = pstmt.executeQuery();//执行给定的 SQL 语句,该语句返回单个 ResultSet 对象
while(rs.next()){
ReportForm reportform = new ReportForm();
reportform.setAb_name(rs.getString(1));
reportform.setAb_money(rs.getInt(2));
list.add(reportform);//将获得的数据添加到list表中
}
}catch(Exception e){
e.printStackTrace();
}finally{
/**
* 关闭数据连接
*/
ConfigUtil.closeRs(rs);
ConfigUtil.closePstmt(pstmt);
ConfigUtil.closeConn(conn);
}
return list;//返回数据列表
}
/**
 * 收支汇总报表---支出(支出项目和金额)
 */
public List receiptsReportFormsFx2(String time1, String time2) {
// TODO Auto-generated method stub
List list = new ArrayList();
try{
conn = ConfigUtil.getConnection();//调用configutil()连接oracle数据库
String sql = "select ab_name,ab_money from account_book "+
"where ab_fx=0 and (ab_time between to_Date(?,'yyyy-mm-dd') and to_Date(?,'yyyy-mm-dd'))";//对数据库信息进行查询
pstmt = conn.prepareStatement(sql);//创建一个 PreparedStatement 对象来将参数化的 SQL 语句发送到数据库。
pstmt.setString(1,time1);
pstmt.setString(2,time2);
pstmt.executeUpdate(); //在此 PreparedStatement 对象中执行 SQL 语句
rs = pstmt.executeQuery();//执行给定的 SQL 语句,该语句返回单个 ResultSet 对象
while(rs.next()){
ReportForm reportform = new ReportForm();
reportform.setAb_name(rs.getString(1));
reportform.setAb_money(rs.getInt(2));
list.add(reportform);//将获得的数据添加到list表中
}
}catch(Exception e){
e.printStackTrace();
}finally{
/**
* 关闭数据连接
*/
ConfigUtil.closeRs(rs);
ConfigUtil.closePstmt(pstmt);
ConfigUtil.closeConn(conn);
}
return list;//返回数据列表
}
/**
 * 年度收支报表统计(支出项目,金额和支出总金额)
 */
public List yearReceiptsReportForms1(String year) {
List list = new ArrayList();
try{
conn = ConfigUtil.getConnection();//调用configutil()连接oracle数据库
String sql =" select ab_name,sum(ab_money) from account_book where to_char(ab_time,'yyyy')=? and ab_fx=0"+ 
            "group by ab_name, to_char(ab_time,'mm')" +
            "order by to_char(ab_time,'mm')asc";//对数据库信息进行查询
pstmt = conn.prepareStatement(sql);//创建一个 PreparedStatement 对象来将参数化的 SQL 语句发送到数据库。
pstmt.setString(1, year);
pstmt.executeUpdate();//在此 PreparedStatement 对象中执行 SQL 语句
rs = pstmt.executeQuery();//执行给定的 SQL 语句,该语句返回单个 ResultSet 对象
while(rs.next()){
ReportForm reportform = new ReportForm();
reportform.setAb_name(rs.getString(1));
reportform.setSumab_money(rs.getDouble(2));
list.add(reportform);//将获得的数据添加到list表中
}

}catch(Exception e){
e.printStackTrace();
}finally{
/**
* 关闭数据连接
*/
ConfigUtil.closeRs(rs);
ConfigUtil.closePstmt(pstmt);
ConfigUtil.closeConn(conn);

}
// TODO Auto-generated method stub
return list;//返回数据列表
}
/**
 * 年度收支报表统计(收入项目,金额和收入总金额)
 */
public List yearReceiptsReportForms2(String year) {
// TODO Auto-generated method stub
List list = new ArrayList();
try{
conn = ConfigUtil.getConnection();//调用configutil()连接oracle数据库
String sql =" select ab_name,sum(ab_money) from account_book where to_char(ab_time,'yyyy')=? and ab_fx=1"+ 
            "group by ab_name, to_char(ab_time,'mm')" +
            "order by to_char(ab_time,'mm')asc";//对数据库信息进行查询
pstmt = conn.prepareStatement(sql);//创建一个 PreparedStatement 对象来将参数化的 SQL 语句发送到数据库。
pstmt.setString(1, year);
pstmt.executeUpdate();//在此 PreparedStatement 对象中执行 SQL 语句
rs = pstmt.executeQuery();//执行给定的 SQL 语句,该语句返回单个 ResultSet 对象
while(rs.next()){
ReportForm reportform = new ReportForm();
reportform.setAb_name(rs.getString(1));
reportform.setSumab_money(rs.getDouble(2));
list.add(reportform);//将获得的数据添加到list表中
}
}catch(Exception e){
e.printStackTrace();
}finally{
/**
* 关闭数据连接
*/
ConfigUtil.closeRs(rs);
ConfigUtil.closePstmt(pstmt);
ConfigUtil.closeConn(conn);

}
// TODO Auto-generated method stub
return list;//返回数据列表
}
原创粉丝点击