【发布时间】:2018-12-05 09:16:40
【问题描述】:
我想问一个关于在 JavaFX 应用程序中实现 SQL 查询的问题,具体来说,我一直在尝试创建一个连接到托管在 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();
}
【问题讨论】:
-
您不需要在列名周围加上引号,即只需使用 FIRST_NAME 而不是 'FIRST_NAME' 等等
-
@shree.pat18 正如你所说,我修复了列名,还将 SYSDATE 修复为 SYSDATE() 并且由于某种原因它一直给我一个语法错误。去掉了 BEGIN 和 END 行,现在它可以正常工作了,但不知道为什么