public void save() {
Connection conn = DBcon.getConnection();
PreparedStatement preStmt = null;
String sql = "insert into yonghu values(null,?,?,?,?,?)";
try {
preStmt = conn.prepareStatement(sql);
preStmt.setString(1, username);
preStmt.setString(2, password);
preStmt.setString(3, question);
preStmt.setString(4, result);
preStmt.setTimestamp(5, rDate);
preStmt.executeUpdate();
} catch (SQLException e) {
System.out.println("往用户表中插入新用户的记录时发生了异常!!!");
e.printStackTrace();
} finally {
DBcon.closeStatement(preStmt);
DBcon.closeConnection(conn);
}
}
------解决方案--------------------
String sql = "insert into yonghu values(null,?,?,?,?,?)";
应该是null的问题,如果你的Null对应的字段是递增的,那就不用写了。
------解决方案--------------------
null 的问题。如果是默认用default
如果是自动增长最好写insert into xxx(xxx,xx,xx)values(xxx,xxx,xxx)
------解决方案--------------------
- SQL code
inser tableName(col1,col2,col3...)value(?,?,?...)--不要用null
------解决方案--------------------
- Java code
preStmt = conn.prepareStatement(sql); 这里就出错1.表示sql语句的编译就出错了。。。如上面所说。。2.conn为null,没有初始化成功。。。
------解决方案--------------------
conn为空,很明显的错误,检查下你的DBcon