java—在javafx中实现sql查询的问题

8wigbo56  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(237)

我想问一个关于在javafx应用程序中实现sql查询的问题,特别是,我一直在尝试创建一个“employee”应用程序,该应用程序连接到localhost中托管的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();
}
cunj1qz1

cunj1qz11#

正如shree提到的,我在写列名时犯了一个错误,我修复了这个错误,还修复了一个关于sysdate()函数被错误地写为only sysdate的错误。sql仍然不能工作,所以我去掉了开始行和结束行,现在它工作得很好,不知道为什么。

相关问题