【发布时间】:2015-08-21 06:02:09
【问题描述】:
当尝试使用以下代码从 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 中执行时工作正常,并且需要格式,否则它会输出记录信息,包括未设置或未使用的时间。
【问题讨论】:
-
你看这个问题了吗? stackoverflow.com/questions/26191830/…
-
否,但它与我的问题无关,因为错误不同。并且在那里提出的所有建议我已经实施无济于事。
-
好的,我认为它可能有用。我怀疑这可能是您的设置:错误的 jdbc 驱动程序、错误的连接字符串等...尤其是因为您说 SQL 可与 MS Access 一起使用。我会在谷歌上搜索有关使用 JDBC 访问 MS Access 的教程,看看你是否能找出什么不起作用。
-
只要我不使用 FORMAT 函数,它就可以完美运行,但是它会以错误的格式给我日期,我无法在我的程序中正确使用它
-
猜猜这是
"FORMAT(employeetbl.DateOfBirth, 'yyyy/mm/dd') AS DateOfBirth, "的问题。 JDBC 可能不支持该功能。我会删除它并格式化单词后的日期。 SimpleDateFormat 代替。
标签: java ms-access ucanaccess