下面的JDBC代码将数据插入MySQL数据库的表中。除了out.println响应和requestdispatcher之外,它工作正常。更新成功后,应打印"成功创建员工帐户"。同样,如果更新不成功。但是,它不打印输出。
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/payroll_system", "root", "");
PreparedStatement ps = con.prepareStatement("INSERT INTO payroll_system.employee_info(employeeID, FirstName, LastName, Admin,DOB,Address,Email,HourlyRate,Gender,ALeaveBalance,SLeaveBalance,ActiveStatus,Role,BSB,BankName,AccNumber,SuperNumber,SuperCompany) values(?,?,?,?,?,?,?,?,?,?,?,1,?,?,?,?,?,?)");
ps.setString(1, employee_id);
ps.setString(2, firstName);
ps.setString(3, lastName);
ps.setString(4, admin);
ps.setString(5, DOB);
ps.setString(6, address);
ps.setString(7, email);
ps.setString(8, HPR);
ps.setString(9, gender);
ps.setString(10, ALB);
ps.setString(11, SLB);
ps.setString(12, Role);
ps.setString(13, BSB);
ps.setString(14, BankName);
ps.setString(15, BAN);
ps.setString(16, SAC);
ps.setString(17, SAN);
int count = ps.executeUpdate();
if(count>0) {
out.println("Employee Account Successfully Created");
}
else {
out.println("Failed to create account");
}
}catch(Exception e)
{
e.printStackTrace();
}
答案 0 :(得分:2)
您正在呼叫executeUpdate
两次,第二次呼叫将不返回任何内容,您应该删除呼叫。
所以,只需替换:
ps.executeUpdate();
int count = ps.executeUpdate();
with:
int count = ps.executeUpdate();