📄 incomedao.java
字号:
package com.j1132.dao.income;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.j1132.bean.IncomeBean;
import com.j1132.dao.AbstractDao;
public class IncomeDao extends AbstractDao implements IincomeDao {
public List<IncomeBean> selectIncome() {
//
List<IncomeBean> list = new ArrayList<IncomeBean>();
Connection conn = getConnection();
String sql = "select i.*, d.dept_name from income i left outer join dept d on i.dept_id = d.dept_id";
Statement stm = null;
ResultSet rs = null;
try {
stm = conn.createStatement();
rs = stm.executeQuery(sql);
while (rs.next()) {
IncomeBean ib = new IncomeBean();
long income_id = rs.getLong(1);
long dept_id = rs.getLong(2);
int daily_income = rs.getInt(3);
Date bussiness_date = rs.getDate(4);
Date lst_mod_timestemp = rs.getDate(5);
String dept_name = rs.getString(6);
ib.setIncome_id(income_id);
ib.setDept_id(dept_id);
ib.setDaily_income(daily_income);
ib.setBussiness_date(bussiness_date);
ib.setLst_mod_timestemp(lst_mod_timestemp);
ib.setDept_name(dept_name);
list.add(ib);
}
} catch (Exception e) {
System.out.println("数据库查询有误");
e.printStackTrace();
} finally {
close(rs, stm, conn);
}
return list;
}
public IncomeBean getIncomeById(long income_id) {
IncomeBean ib = new IncomeBean();
Connection conn = getConnection();
String sql = "select i.*from income i where i.income_id =?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
ps.setLong(1, income_id);
rs = ps.executeQuery();
while (rs.next()) {
long income_id1 = rs.getLong(1);
long dept_id = rs.getLong(2);
int daily_income = rs.getInt(3);
Date bussiness_date = rs.getDate(4);
Date lst_mod_timestemp = rs.getDate(5);
ib.setIncome_id(income_id1);
ib.setDept_id(dept_id);
ib.setDaily_income(daily_income);
ib.setBussiness_date(bussiness_date);
ib.setLst_mod_timestemp(lst_mod_timestemp);
}
} catch (Exception e) {
System.out.println("数据库查询有误");
e.printStackTrace();
} finally {
close(rs, ps, conn);
}
return ib;
}
public boolean updateIncome(IncomeBean ib) {
boolean b = true;
Connection conn = getConnection();
String sql = "UPDATE INCOME SET DEPT_ID=?, DAILY_INCOME =?,LST_MOD_TIMESTEMP = ?WHERE INCOME_ID = ?";
PreparedStatement ps = null;
long income_id = ib.getIncome_id();
long dept_id = ib.getDept_id();
int daily_income = ib.getDaily_income();
java.util.Date lst_time = new java.util.Date();
try {
ps = conn.prepareStatement(sql);
ps.setLong(1, dept_id);
ps.setInt(2, daily_income);
ps.setDate(3, new java.sql.Date(lst_time.getTime()));
ps.setLong(4, income_id);
ps.executeUpdate();
} catch (Exception e) {
b = false;
System.out.println("更新数据有误");
e.printStackTrace();
} finally {
close(null, ps, conn);
}
return b;
}
public boolean saveIncome(IncomeBean ib) {
boolean b = true;
long income_id = ib.getIncome_id();
long dept_id = ib.getDept_id();
int daily_income = ib.getDaily_income();
java.util.Date business_date = ib.getBussiness_date();
java.util.Date last_mod_time = ib.getLst_mod_timestemp();
Connection conn = getConnection();
String sql = "insert into income i(income_id,dept_id,daily_income,business_date,lst_mod_timestemp) values (?,?,?,?,?)";
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ps.setLong(1, income_id);
ps.setLong(2, dept_id);
ps.setInt(3, daily_income);
ps.setDate(4, new java.sql.Date(business_date.getTime()));
ps.setDate(5, new java.sql.Date(last_mod_time.getTime()));
ps.executeUpdate();
} catch (Exception e) {
b = false;
System.out.println("插入数据错误");
e.printStackTrace();
} finally {
close(null, ps, conn);
}
return b;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -