gpt4 book ai didi

java - UCanAccess Java 执行错误

转载 作者:行者123 更新时间:2023-11-30 03:17:50 27 4
gpt4 key购买 nike

当尝试使用以下代码从 MS Access 数据库获取详细信息时:

//returns the details of a specific employee for use in the update GUI
public static String getEmployeeDetails(int empID) throws SQLException{
String employee = "";
Statement stmt = conn.createStatement();
String query = "SELECT employeetbl.Department, "
+ "employeetbl.Surname, "
+ "employeetbl.FirstName, "
+ "employeetbl.CurrentPosition, "
+ "FORMAT(employeetbl.DateOfBirth, 'yyyy/mm/dd') AS DateOfBirth, "
+ "employeetbl.TotalYearsRelevantExperience, "
+ "employeetbl.HighestQualification, "
+ "employeetbl.EmailAddress, "
+ "employeetbl.PhoneNo, "
+ "FORMAT(employeetbl.DateOfEmployment, 'yyyy/mm/dd') AS DateOfEmployment "
+ "FROM employeetbl WHERE EmployeeID = "+empID+";";
ResultSet rs = stmt.executeQuery(query);
while(rs.next()){
employee = rs.getString("Department")
+"#"+rs.getString("Surname")
+"#"+rs.getString("FirstName")
+"#"+rs.getString("CurrentPosition")
+"#"+rs.getString("DateOfBirth")
+"#"+rs.getString("TotalYearsRelevantExperience")
+"#"+rs.getString("HighestQualification")
+"#"+rs.getString("EmailAddress")
+"#"+rs.getString("PhoneNo")
+"#"+rs.getString("DateOfEmployment");
}
return employee;
}

从此方法调用:

if(cmbTable.getSelectedItem().equals("Employees")){
String[] tmp = cmbRecord.getSelectedItem().toString().split("-");
int empID = Integer.parseInt(tmp[0]);
String employeeDetails = Master.getEmployeeDetails(empID);
String[] employee = employeeDetails.split("#");
cmbDepartment.setSelectedItem(employee[0]);
txtSurname.setText(employee[1]);
txtFirstName.setText(employee[2]);
txtCurrentPos.setText(employee[3]);
txtDOB.setText(employee[4]);
txtExperience.setText(employee[5]);
txtQualification.setText(employee[6]);
txtEmail.setText(employee[7]);
txtPhone.setText(employee[8]);
txtEmployment.setText(employee[9]);
}

我遇到了以下错误

error: net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.0 Java execution: FORMAT

我不知道是什么导致了这个错误,因为 SQL 在 Access 中执行时工作得很好,并且需要格式,否则它会输出记录信息,包括未设置或使用的时间。

最佳答案

我将从 SQL 字符串中删除 FORMAT 并用 Java 格式化日期。我不知道返回的日期格式是什么,但这应该允许您解析它,然后在 Calendar.set() 方法中输入字段,如下所示:

public static void main(String[] args) {
final Calendar calendar = Calendar.getInstance();
calendar.set(2015, Calendar.AUGUST, 21, 9, 27);
final Date date = calendar.getTime();
final String formattedDate = new SimpleDateFormat("yyyy/MM/dd").format(date);
System.out.println("formattedDate = " + formattedDate);
}

关于java - UCanAccess Java 执行错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32133366/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com