首页 > 解决方案 > 如何在java和mysql中将休假添加到列表中的所有员工

问题描述

我正在为我的最后一年项目创建一个简单的薪资系统。我遇到了向员工添加休假的问题。如果我添加休假,它将添加到属于公司的所有员工。但是我无法完成我尝试过的任务,所以我在下面编写了代码。例如,如果我选择休假临时休假为 20 天。它在列表中添加了所有员工编号。

员工表

在此处输入图像描述

离开表

在此处输入图像描述

在此处输入图像描述

我尝试过的代码

    String cas = txtcas.getValue().toString();
     String anu = txtanu.getValue().toString();
     String med = txtmed.getValue().toString();
     String year = txtyear.getText();

    try {
        int c;
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost/spay","root","");
       PreparedStatement pst1 = con.prepareStatement("select empno from employee");
        ResultSet rs = pst1.executeQuery();

        ArrayList<String> empnos = new ArrayList<>();
        while (rs.next()) {
     empnos.add(rs.getString(1));

        pst = con.prepareStatement("insert into leaves(empno,casual,annual,address,medical,year)values(?,?,?,?,?)");
        pst.setString(1,) ); // employee no how to give here
        pst.setString(2, cas);
        pst.setString(3, anu);
         pst.setString(4, med);
        pst.setString(5, year);         
        pst.executeUpdate();
        JOptionPane.showMessageDialog(null,"Leave Insertedddddd");
         }

数据库输出看起来像这样

在此处输入图像描述

标签: java

解决方案


// 这是从表中获取特定列的数据的代码。

String empNoValue; 
while (rs.next()) {
 empNoValue = rs.getString("empno"); // empno is the column name
}

在那之前,

1 您需要添加一些 WHERE 条件以在查询 pst1 中获取特定员工。

否则,

1 您需要为该员工设置session值或hidden前一页中的值。

1.1 之后,您获得了数据添加到表中的特定员工(来自该员工hidden或价值)。session

String employeeValue = hidden or session value // from privious page

然后将employeeValue添加到插入查询中,如下所示

pst.setString(1, employeeValue); // employeeValue is the your specific empno get from privious page

推荐阅读