亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么新增女神的時候,會在isdel的字段位置報null

新增女神,除了id,user_name兩個字段,其他的字段默認為null,但是新增女神的時候,只輸入姓名,性別,年齡,郵箱,手機號,新增報錯,create_user和update_user我也沒有輸入,也沒有報錯偏偏在isdel報錯。

我給isdel的字段賦值后,新增成功了,也沒有報錯,請各位同學幫忙看下。

歡迎來到女神禁區:
下面是女神禁區的功能列表:
[MAIN/M]:主菜單
[QUERY/Q]:查看女神的全部信息
[GET/G]:查看某位女神的詳細信息
[ADD/A]:添加女神信息
[UPDATE/U]:更新女神信息
[DELETE/D]:刪除女神
[SEARCH/S]:查詢女神信息(根據姓名,手機號來查詢)
[EXIT/E]:退出女神禁區
[BREAK/B]:退出當前功能,返回主菜單
A
請說出女神的[姓名]
xioa
請輸出女神的[性別]
1
請輸出女神的[年齡]
21
請輸出女神的[生日],格式為yyyy-MM-dd
1995-4-9
請輸入女神的[郵箱]
[email protected]
請輸出女神的[手機號]
123123123
請輸入女神的[是否刪除]
2
新增女神成功


數據庫的表創建語句

CREATE TABLE `imooc_goddess` (
? `id` int(11) NOT NULL AUTO_INCREMENT,
? `user_name` varchar(30) NOT NULL,
? `sex` int(11) DEFAULT NULL,
? `age` int(11) DEFAULT NULL,
? `birthday` date DEFAULT NULL,
? `email` varchar(30) DEFAULT NULL,
? `mobile` varchar(11) DEFAULT NULL,
? `create_user` varchar(30) DEFAULT NULL,
? `create_date` date DEFAULT NULL,
? `update_user` varchar(30) DEFAULT NULL,
? `update_date` date DEFAULT NULL,
? `isdel` int(11) DEFAULT NULL,-----------這里默認為null啊
? PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

?? // 新增女神
?? ?public void addGoddess(Goddess g) throws Exception {
?? ??? ?Connection conn = DBUtil.getConnection();
?? ??? ?String sql = ""+
?? ??? ??? ??? ?"insert into imooc_goddess"+
?? ??? ??? ??? ?"(user_name,sex,age,birthday,email,mobile,create_user,"+
?? ??? ??? ??? ?"create_date,update_user,update_date,isdel) values"+
?? ??? ??? ??? ?"(?,?,?,?,?,?,?,current_date(),?,current_date(),?)";
?? ??? ?PreparedStatement pstmt = conn.prepareStatement(sql);
?? ??? ?
?? ??? ?pstmt.setString(1, g.getUser_name());
?? ??? ?pstmt.setInt(2, g.getSex());
?? ??? ?pstmt.setInt(3, g.getAge());
?? ??? ?pstmt.setDate(4, new Date(g.getBirthday().getTime()));
?? ??? ?pstmt.setString(5, g.getEmail());
?? ??? ?pstmt.setString(6, g.getMobile());
?? ??? ?pstmt.setString(7, g.getCreate_user());
?? ??? ?pstmt.setString(8, g.getUpdate_user());
?? ??? ?pstmt.setInt(9, g.getIsdel());----------------這個位置報錯null
?? ??? ?pstmt.execute();
?? ?}

這是后邊我加上了 給isdel賦值

else if (OPERATION_ADD.equals(in) || OPERATION_ADD.substring(0, 1).equals(in)
?? ??? ??? ??? ??? ?|| OPERATION_ADD.equals(previous)) {
?? ??? ??? ??? ?previous = OPERATION_ADD;
?? ??? ??? ??? ?// 根據step的值來執行新增女神的第幾步操作
?? ??? ??? ??? ?if (1 == step) {
?? ??? ??? ??? ??? ?System.out.println("請說出女神的[姓名]");
?? ??? ??? ??? ?} else if (2 == step) {
?? ??? ??? ??? ??? ?goddess.setUser_name(in);
?? ??? ??? ??? ??? ?System.out.println("請輸出女神的[性別]");
?? ??? ??? ??? ?} else if (3 == step) {
?? ??? ??? ??? ??? ?goddess.setSex(Integer.valueOf(in));
?? ??? ??? ??? ??? ?System.out.println("請輸出女神的[年齡]");
?? ??? ??? ??? ?} else if (4 == step) {
?? ??? ??? ??? ??? ?goddess.setAge(Integer.valueOf(in));
?? ??? ??? ??? ??? ?System.out.println("請輸出女神的[生日],格式為yyyy-MM-dd");
?? ??? ??? ??? ?} else if (5 == step) {
?? ??? ??? ??? ??? ?SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
?? ??? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ??? ?Date birthday = sdf.parse(in);
?? ??? ??? ??? ??? ??? ?goddess.setBirthday(birthday);
?? ??? ??? ??? ??? ??? ?System.out.println("請輸入女神的[郵箱]");
?? ??? ??? ??? ??? ?} catch (ParseException e) {
?? ??? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ??? ??? ?System.out.println("您輸入的格式有誤,請重新輸入");
?? ??? ??? ??? ??? ??? ?step = 3;// 需要減一次,下面會自增1
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?} else if (6 == step) {
?? ??? ??? ??? ??? ?goddess.setEmail(in);
?? ??? ??? ??? ??? ?System.out.println("請輸出女神的[手機號]");
?? ??? ??? ??? ?} else if (7 == step) {
?? ??? ??? ??? ??? ?goddess.setMobile(in);
?? ??? ??? ??? ??? ?System.out.println("請輸入女神的[是否刪除]");
?? ??? ??? ??? ?} else if (8 == step) {
?? ??? ??? ??? ??? ?goddess.setIsdel(Integer.valueOf(in));
?? ??? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ??? ?action.add(goddess);
?? ??? ??? ??? ??? ??? ?System.out.println("新增女神成功");
?? ??? ??? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ??? ??? ?System.out.println("新增女神失敗");
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?if (OPERATION_ADD.equals(previous)) {
?? ??? ??? ??? ?step++;
?? ??? ??? ?}

正在回答

1 回答

dao里調用了goddess的創建人更新人還有isdel,但是你在view里沒有給它賦值啊,導致的空指針,要想解決,在Goddess.java里給這些屬性賦值為null就行了,或者在view里添加通過scanner為這3個屬性賦值。這個和數據庫的語句沒關系的,因為你的dao里的add方法里的那些?不會自動賦值的。

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

為什么新增女神的時候,會在isdel的字段位置報null

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號