【发布时间】: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