[尝试使用以下代码从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
我不知道是什么原因导致此错误,因为在Access中执行SQL时,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);
}
select format(#2001-03-02#, 'yyyy/mm/dd') from dual.
尽管如此,缺少空参数的处理,这很可能会引起问题。所以select format(null, 'yyyy/mm/dd') from dual;
导致引发异常。即使我解决了此问题,由于函数FORMAT(varchar,varchar)和FORMAT(double,varchar)的歧义,也需要使用特定的hsqldb语法将空值转换为时间戳。因此,我建议采用这种(简化的)解决方法:
select nvl2(employeetbl.DateOfBirth,FORMAT(employeetbl.DateOfBirth, 'yyyy/mm/dd'),null) AS DateOfBirth from ...
注意,在访问中,mm表示月(而不是分钟)。
格式化月份和分钟的正确方法是什么?
因为我正在这样使用它:格式(fecha_recepcion,“ yyyy-mm-dd HH:MM:ss”)然后在分钟中出现的月份值相同。我尝试了所有选项,但几分钟总是得到一个月。例如,在数据库中,Datetime的加载方式如下:23/12/2019 00:00:01然后在应用格式函数后,UCanAccess返回以下内容:2019-12-23 00:12:01代替:2019-12-23 00:00:01
谢谢!