Java Prepared Statement不支持检索数据

时间:2016-09-12 08:32:10

标签: java mysql jdbc prepared-statement

在公共和私人函数中使用预准备语句会有区别吗?

我有两个函数insertRecord()GetAllRecords(),问题是employeeCode在我GetAllRecords上调用它时没有值,但它有效使用insertRecord(),因为我可以在数据库中存储和读取它们。我无法检索它们以进行显示,因为employeeCode似乎GetAllRecords中没有值private void insertRecord(Connection conn){ try { PreparedStatement pstmnt= conn.prepareStatement("insert into "+sqlInsert()+"(EmployeeName, EmployeeCode, EmployeeSales, EmployeeGross, EmployeeCommission, EmployeeResult)"+ "values(?,?,?,?,?,?)"); pstmnt.setString(1, employeeName); pstmnt.setString(2, employeeCode); pstmnt.setDouble(3, employeeSales); pstmnt.setDouble(4, gross); pstmnt.setDouble(5, commission); pstmnt.setDouble(6, result); //now commit to database pstmnt.executeUpdate(); } catch(SQLException sqle) { sqle.printStackTrace(); } }

public ResultSet getAllRecords(Connection conn) {
    ResultSet records = null;
    try{
        String sql="SELECT * FROM (SELECT * FROM SalesCodeA UNION ALL SELECT * FROM SalesCodeB UNION ALL SELECT * FROM SalesCodeC ) t WHERE t.EmployeeCode=?";
        PreparedStatement pstmnt= conn.prepareStatement(sql);
        pstmnt.setString(1, employeeCode);

        records= pstmnt.executeQuery();
    } catch(SQLException sqle){
        sqle.printStackTrace();
    }
    return records;
}

这是我的GetAllRecords函数

pstmnt.setString(1, employeeCode);

如果我更改pstmnt.setString(1, "A");的值,让它说EmployeeCode = "A"它将输出包含{{1}列的表,它将正常工作1}}。

这就是我在Bean中声明employeeCode的方式:

private String employeeCode;

public String getEmployeeCode() {
    return employeeCode;
}

public void setEmployeeCode(String employeeCode) {
    this.employeeCode=employeeCode;
}

和Helper类:

public class EmployeeHelper {

    public static EmployeeBean getInstance(String employeeCode) {
        employee.setEmployeeCode(employeeCode);
        return employee;
    }

}

0 个答案:

没有答案