对象参数在MySQL DAO java中消失

时间:2015-10-07 09:20:02

标签: java mysql jdbc

我使用参数

创建了一个对象成员
  

firstName,lastName,memberID,个人号码和nBoats。

构造对象和设置参数值工作正常,debug显示它们已设置,但是当将对象发送到SQLDAO时,memberID以某种方式丢失并设置为null。因此,对象无法保存并给出错误

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: 
Column memID cannot be null 

我们用来在sql数据库中保存新成员的代码是  SQLDAO.saveMember(newMember);,这是在memberdatahandling类中。

在DAO课上我们写了这个:

 public static void saveMember(Member member) {

    String query2 = "INSERT into member(firstName, lastName, memID, pnr, nBoats)"
            + " values (?, ?, ?, ?, ?)";


    try {
        Connection conn = openConnection();
        PreparedStatement preparedStmt = conn.prepareStatement(query2);

        preparedStmt.setString(1, member.getMemberFirstName());
        preparedStmt.setString(2, member.getMemberLastName());
        preparedStmt.setString(3, member.getMemberID());
        preparedStmt.setString(4, member.getMemberPersonalNumber());
        preparedStmt.setInt(5, member.getMemberNBoats());

        preparedStmt.executeUpdate();
        preparedStmt.close();

        conn.close();
    }
    catch (ClassNotFoundException| SQLException | IllegalAccessException | InstantiationException e) {
        System.out.println("Couldn't save Member because of a database connection error.");
        e.printStackTrace();
    }
}

0 个答案:

没有答案