ResultSet rs = stmt.executeQuery(id);
rs.next();
int custId =1000;
if(rs.wasNull()) {
custId = 1000;
} else {
custId = rs.getInt(8)+1;
}
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1, name);
ps.setString(2, fname);
ps.setString(6, phone);
ps.setString(4, email);
ps.setString(5, sex);
ps.setString(7, date);
ps.setString(3, address);
ps.setInt(8, custId);
答案 0 :(得分:0)
试试这个:
ResultSet rs = stmt.executeQuery(id);
boolean thisResultSetHasRecords;
if (rs.isBeforeFirst()) {
thisResultSetHasRecords = true;
while(rs.next()) {
//do stuff with every record,
//rs.next() returns false if empty or after last
}
} else {
thisResultSetHasRecords = false;
}
这一个
rs.wasNull()
检查现有行的列( - >非空ResultSet)是否具有SQL值NULL,不要与Java null混淆。
此外,您可能不需要手动增加id字段;你可以在表AUTO_INCREMENT中创建字段以避免第二个错误 - >见http://www.w3schools.com/sql/sql_autoincrement.asp