java - 如何在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");
}
数据库输出看起来像这样
解决方案
// 这是从表中获取特定列的数据的代码。
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
推荐阅读
- angular - 日期管道问题
- windows - nsis 安装程序:如何在静默模式下关闭“验证安装程序”框?
- python - Groupby 类和计数特征中的缺失值
- javascript - 如何在功能上为多个 React 组件加载新数据
- 2d - 与两个运动物体的二维碰撞
- mysql - DataStudio 中与 MySQL 数据库的连接丢失
- c# - 如何通过 WCF 服务发送 PDF 文件?
- amazon-web-services - AWS中ETL流程的最佳方式是什么
- angular - Angular Route Animations 不工作(我正在使用来自 angular.io 的官方文档的示例动画)
- c# - 为什么不能推断这个泛型类的类型参数?