【问题标题】:UCanAccess Java Execution ErrorUCanAccess Java 执行错误
【发布时间】: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


【解决方案1】:

我会从您的 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);
}

【讨论】:

  • 非常感谢您帮助它对格式错误进行排序,但是现在它给出了一个新错误:net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.0 parameter marker not allowed
  • 也许您可以编辑您的原始帖子并将您的新 SQL 放入其中。看到你的代码很难说。也许您更改了某些内容并在某处添加了无效的parameter
  • 没关系,我在我的 sql 中犯了一个错误,并在错误的位置创建了一个参数,现在它已排序,非常感谢您的帮助
【解决方案2】:

对于以后的读者:格式化功能是在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 ...

请注意,在 access 中,mm 表示月(而不是分钟)。

【讨论】:

    猜你喜欢
    • 2018-11-23
    • 2017-01-19
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    相关资源
    最近更新 更多