[英]UCanAccess Java Execution Error
嘗試使用以下代碼從 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);
}
對於未來的讀者:格式功能在ucanaccess中實現,並且最初發布的代碼是正確的。 所以下面的代碼在 ucanaccess 上運行良好:
select format(#2001-03-02#, 'yyyy/mm/dd') from dual.
然而,在處理空參數方面缺乏,這可能會導致問題。 所以select format(null, 'yyyy/mm/dd') from dual;
導致拋出異常。 即使我修復了這個問題,也需要使用特定的 hsqldb 語法將空值轉換為時間戳,因為函數FORMAT(varchar,varchar) 和 FORMAT(double,varchar)存在歧義。 所以我建議這個(簡化的)解決方法:
select nvl2(employeetbl.DateOfBirth,FORMAT(employeetbl.DateOfBirth, 'yyyy/mm/dd'),null) AS DateOfBirth from ...
請注意,與訪問一樣,mm 表示月(而不是分鍾)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.