我想问一个有关在JavaFX应用程序中实现SQL查询的问题,特别是,我一直在尝试创建一个“雇员”应用程序,该应用程序连接到本地主机上托管的MySQL数据库。我正在使用DAO模式来执行此操作,并且代码显然是正确的,我遇到的唯一问题是,在尝试向表中添加新员工时,我总是遇到错误。具体来说,我遇到了SQL语法错误,不知道代码出了什么问题。
我将EmployeeDAO类的代码放在那里,请忽略所有方法中的SQL错误(我还没有更正),我已经更正并且仍然给我带来问题的方法是insertEmp( )方法。
package Model;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import Util.DBUtil;
import java.sql.*;
public class EmployeeDAO {
//Select an Employee
public static Employee searchEmployee (String empId) throws SQLException, ClassNotFoundException{
String selectStmt = "SELECT * FROM employees WHERE employeeId="+empId;
try{
ResultSet rsEmp = DBUtil.dbExecuteQuery(selectStmt);
Employee employee = getEmployeeFromResultSet(rsEmp);
return employee;
}catch (SQLException e){
System.out.println("While Searching An Employee With "+empId+" Id, An Error Occurred");
e.printStackTrace();
throw e;
}
}
private static Employee getEmployeeFromResultSet(ResultSet rs) throws SQLException{
Employee emp = null;
if(rs.next()){
emp = new Employee();
emp.setEmployeeId(rs.getInt("EMPLOYEE_ID"));
emp.setFirstName(rs.getString("FIRST_NAME"));
emp.setLastName(rs.getString("LAST_NAME"));
emp.setEmail(rs.getString("EMAIL"));
emp.setPhoneNumber(rs.getString("PHONE_NUMBER"));
emp.setHireDate(rs.getDate("HIRE_DATE"));
emp.setJobId(rs.getString("JOB_ID"));
emp.setSalary(rs.getInt("SALARY"));
emp.setCommissionPct(rs.getDouble("COMMISSION_PCT"));
emp.setManagerId(rs.getInt("MANAGER_ID"));
emp.setDepartmentId(rs.getInt("DEPARTMENT_ID"));
}
return emp;
}
//Select Employees
public static ObservableList<Employee> searchEmployees() throws SQLException,ClassNotFoundException{
String selectStmt="SELECT * FROM employees";
try{
ResultSet rsEmps = DBUtil.dbExecuteQuery(selectStmt);
ObservableList<Employee> empList = getEmployeeList(rsEmps);
return empList;
}catch(SQLException e){
System.out.println("SQL Select Operation Failed");
e.printStackTrace();
throw e;
}
}
//Select * from employees operation
private static ObservableList<Employee> getEmployeeList(ResultSet rs) throws SQLException,ClassNotFoundException{
ObservableList<Employee> empList = FXCollections.observableArrayList();
while(rs.next()){
Employee emp = new Employee();
emp.setEmployeeId(rs.getInt("EMPLOYEE_ID"));
emp.setFirstName(rs.getString("FIRST_NAME"));
emp.setLastName(rs.getString("LAST_NAME"));
emp.setEmail(rs.getString("EMAIL"));
emp.setPhoneNumber(rs.getString("PHONE_NUMBER"));
emp.setHireDate(rs.getDate("HIRE_DATE"));
emp.setJobId(rs.getString("JOB_ID"));
emp.setSalary(rs.getInt("SALARY"));
emp.setCommissionPct(rs.getDouble("COMMISSION_PCT"));
emp.setManagerId(rs.getInt("MANAGER_ID"));
emp.setDepartmentId(rs.getInt("DEPARTMENT_ID"));
empList.add(emp);
}
return empList;
}
//Update an employee's email address
public static void updateEmpEmail(String empId, String empEmail) throws SQLException, ClassNotFoundException{
String updateStmt = "BEGIN\n" +
" UPDATE employees\n" +
" SET EMAIL = '" + empEmail + "'\n" +
" WHERE EMPLOYEE_ID = " + empId + ";\n" +
" COMMIT;\n" +
"END;";
try{
DBUtil.dbExecuteQuery(updateStmt);
}catch(SQLException e){
System.out.println("An Error Occurred While Updating The Information");
e.printStackTrace();
throw e;
}
}
public static void deleteEmpWithId(String empId) throws SQLException, ClassNotFoundException{
String updateStmt = "BEGIN\n" +
" DELETE FROM employees\n" +
" WHERE employee_id ="+ empId +";\n" +
" COMMIT;\n" +
"END;";
try{
DBUtil.dbExecuteQuery(updateStmt);
}catch(SQLException e){
System.out.println("An Error Occurred While Deleting An Employee With Id: "+empId);
e.printStackTrace();
throw e;
}
}
public static void insertEmp(String name, String lastName, String email) throws SQLException, ClassNotFoundException{
String updateStmt = "BEGIN\n" +
" INSERT INTO employees ('FIRST_NAME', 'LAST_NAME', 'EMAIL', 'HIRE_DATE', 'JOB_ID')\n" +
" VALUES\n" +
" (?, ?, ?, SYSDATE, 'IT_PROG');\n" +
" END;";
try{
DBUtil.dbPreparedStatement(updateStmt, name, lastName, email);
}catch(SQLException e){
System.out.println("An Error Occurred While Adding A New Employee To The Table");
e.printStackTrace();
throw e;
}
}
}
我还将在此处添加使用insertEmp方法的代码。
public static void dbPreparedStatement(String sqlStmt, String name, String lastName, String email) throws SQLException,ClassNotFoundException{
PreparedStatement stmt = null;
try{
dbConnect();
stmt=conn.prepareStatement(sqlStmt);
stmt.setString(1, name);
stmt.setString(2, lastName);
stmt.setString(3, email);
stmt.execute();
}catch(SQLException e){
System.out.println("Error Occured At ExecutePreparedStatement Operation");
e.printStackTrace();
throw e;
}
dbDisconnect();
}
答案 0 :(得分:0)
正如shree所提到的,我在写下列名时犯了一个错误,我对此进行了修正,还修正了关于将SYSDATE()函数仅作为SYSDATE误写的错误。 SQL仍然不起作用,所以我取出了BEGIN和END行,现在它可以正常工作,不知道为什么。