我正在为我的最后一年项目创建一个简单的薪资系统。我遇到了向员工添加休假的问题。如果我添加休假,它将添加到属于公司的所有员工。但我无法完成我尝试的任务,所以我在下面编写了代码。 例如 如果我选择临时休假为 20 天。它在列表中添加了所有员工编号。
员工表
离开餐 table
我试过的代码
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");
}
数据库输出看起来像这样
最佳答案
//这是从表中获取特定列数据的代码。
String empNoValue;
while (rs.next()) {
empNoValue = rs.getString("empno"); // empno is the column name
}
在那之前,
1 您需要在查询 pst1 中添加一些 WHERE 条件以获取特定员工。
否则,
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
关于java - java和mysql如何为列表中的所有员工添加请假,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56698051/