【问题标题】:Why does this procedure run in MYSQL but not in Java?为什么这个过程在 MYSQL 中运行而不在 Java 中运行?
【发布时间】:2016-12-24 18:43:46
【问题描述】:

我是 Java 新手,有点挣扎。

我已经在 mysql 中设置了一个程序,以便在输入姓氏时返回员工详细信息:

CREATE PROCEDURE getEmployeeByLastName(IN in_last_name VARCHAR(16))
SELECT emp_no, first_name, last_name, gender, hire_date FROM oop_employees
WHERE last_name = in_last_name;

当我执行它时,这在 phpmyadmin 中有效。

在我的 Java 主方法中,我要求用户输入姓氏...

System.out.println("Please enter the last name of the employee.");
String last_name = keyboard.next();
Employee emp = getEmployeeByLastName(dbc.getConnection(), last_name);           
System.out.println(emp);

getEmployeeByLastName 是:

public static Employee getEmployeeByLastName(Connection conn, String lname) {
    Employee emp = null;
    try {
        String sql = "CALL getEmployeeByLastName(\""+ lname +"\")";
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery(sql);     
        while (rs.next())       
            emp = new Employee(rs.getInt("emp_no"), rs.getDate("birth_date"),rs.getString("first_name"),rs.getString("last_name"), rs.getString("gender"),rs.getDate("hire_date"));
            rs.close(); 
            st.close(); 
    }
    catch (SQLException e) {
        e.printStackTrace();
    }
    return emp;
}

当我搜索姓氏时,我得到了几个 SQL 异常错误,以及上面代码中的两个错误:

emp = new Employee(rs.getInt("emp_no"), rs.getDate("birth_date"),rs.getString("first_name"),rs.getString("last_name"), rs.getString("gender"),rs.getDate("hire_date"));

和..

Employee emp = getEmployeeByLastName(dbc.getConnection(), last_name);

我能够使用员工类创建其他显示数据库数据的程序,这是我第一个需要用户输入的程序。

这在 mysql 中有效但在 eclipse 中无效有明显的原因吗?非常感谢所有帮助,我发现这很难调试。如果需要更多信息,请告诉我。

编辑:

例外情况

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:959)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:862)
at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1076)
at com.mysql.jdbc.ResultSetImpl.getDate(ResultSetImpl.java:2034)

【问题讨论】:

  • 有哪些例外?发布最小的堆栈跟踪
  • 在您的问题中包含例外情况
  • rs.getDate("birth_date") - 我不知道这是否是唯一的错误,但您的程序没有获取该列。
  • 你的意思是异常号吗?
  • 我的意思是你得到什么类型的例外?

标签: java mysql stored-procedures


【解决方案1】:

您的查询从oop_employees 表中选择了 5 列:

SELECT emp_no, first_name, last_name, gender, hire_date FROM oop_employees

但是您的 Java 代码尝试从 ResultSet 中读取 6 列:

emp = new Employee(rs.getInt("emp_no"), rs.getDate("birth_date"),rs.getString("first_name"),rs.getString("last_name"), rs.getString("gender"),rs.getDate("hire_date"));

您忘记在 SQL 语句中包含 birth_date 列。

【讨论】:

    【解决方案2】:

    这个问题是因为 JDBC 代码无法解析结果集。该过程很好,但需要修复 Java 代码。 SQL 和结果集之间存在差异。正如另一位评论员 (@Eran) 所指出的,您只选择了 SQL 中的 5 列。

    SELECT emp_no, first_name, last_name, gender, hire_date FROM oop_employees WHERE last_name = in_last_name;
    

    但您希望结果集查找 6 列。

    emp = new Employee(rs.getInt("emp_no"), rs.getDate("birth_date"),rs.getString("first_name"),rs.getString("last_name"), rs.getString("gender"),rs.getDate("hire_date"));
    

    在 SQL 中添加“birth_date”列,然后重试。

    另外,请在发布问题时粘贴完整的堆栈跟踪。您提供的堆栈跟踪不完整。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-01
      • 2013-06-26
      • 2012-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多