Subsidy(补贴表) 字段名 类型 注释 其他 id int 序号 主键,非空 subsidyname varchar(50) 考勤 空 Amount decimal(10) 补贴金额 空 根据以上内容用java写出增删改查方法类
时间: 2023-06-16 08:06:34 浏览: 105
以下是使用Java语言实现的增删改查方法类:
```java
import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class SubsidyDao {
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
// 连接数据库
public Connection getConnection() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
return conn;
}
// 关闭连接
public void closeConnection() throws Exception {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
}
// 添加补贴记录
public void addSubsidy(Subsidy subsidy) throws Exception {
conn = getConnection();
String sql = "INSERT INTO subsidy (id, subsidyname, Amount) VALUES (?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, subsidy.getId());
pstmt.setString(2, subsidy.getSubsidyname());
pstmt.setBigDecimal(3, subsidy.getAmount());
pstmt.executeUpdate();
closeConnection();
}
// 删除补贴记录
public void deleteSubsidy(int id) throws Exception {
conn = getConnection();
String sql = "DELETE FROM subsidy WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
closeConnection();
}
// 修改补贴记录
public void updateSubsidy(Subsidy subsidy) throws Exception {
conn = getConnection();
String sql = "UPDATE subsidy SET subsidyname=?, Amount=? WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, subsidy.getSubsidyname());
pstmt.setBigDecimal(2, subsidy.getAmount());
pstmt.setInt(3, subsidy.getId());
pstmt.executeUpdate();
closeConnection();
}
// 查询所有补贴记录
public List<Subsidy> queryAllSubsidy() throws Exception {
conn = getConnection();
String sql = "SELECT * FROM subsidy";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
List<Subsidy> list = new ArrayList<>();
while (rs.next()) {
Subsidy subsidy = new Subsidy();
subsidy.setId(rs.getInt("id"));
subsidy.setSubsidyname(rs.getString("subsidyname"));
subsidy.setAmount(rs.getBigDecimal("Amount"));
list.add(subsidy);
}
closeConnection();
return list;
}
// 根据id查询补贴记录
public Subsidy querySubsidyById(int id) throws Exception {
conn = getConnection();
String sql = "SELECT * FROM subsidy WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
Subsidy subsidy = null;
if (rs.next()) {
subsidy = new Subsidy();
subsidy.setId(rs.getInt("id"));
subsidy.setSubsidyname(rs.getString("subsidyname"));
subsidy.setAmount(rs.getBigDecimal("Amount"));
}
closeConnection();
return subsidy;
}
}
```
其中,Subsidy类如下所示:
```java
public class Subsidy {
private int id;
private String subsidyname;
private BigDecimal amount;
public Subsidy() {}
public Subsidy(int id, String subsidyname, BigDecimal amount) {
this.id = id;
this.subsidyname = subsidyname;
this.amount = amount;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSubsidyname() {
return subsidyname;
}
public void setSubsidyname(String subsidyname) {
this.subsidyname = subsidyname;
}
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
}
```
使用方法:
```java
public class Main {
public static void main(String[] args) {
SubsidyDao dao = new SubsidyDao();
// 添加补贴记录
Subsidy subsidy1 = new Subsidy(1, "考勤", new BigDecimal("1000.00"));
try {
dao.addSubsidy(subsidy1);
} catch (Exception e) {
e.printStackTrace();
}
// 查询所有补贴记录
List<Subsidy> list = null;
try {
list = dao.queryAllSubsidy();
} catch (Exception e) {
e.printStackTrace();
}
for (Subsidy subsidy : list) {
System.out.println(subsidy.getId() + " " + subsidy.getSubsidyname() + " " + subsidy.getAmount());
}
// 修改补贴记录
Subsidy subsidy2 = new Subsidy(1, "出勤", new BigDecimal("1500.00"));
try {
dao.updateSubsidy(subsidy2);
} catch (Exception e) {
e.printStackTrace();
}
// 查询特定补贴记录
Subsidy subsidy3 = null;
try {
subsidy3 = dao.querySubsidyById(1);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(subsidy3.getId() + " " + subsidy3.getSubsidyname() + " " + subsidy3.getAmount());
// 删除补贴记录
try {
dao.deleteSubsidy(1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
阅读全文