首页 新闻 会员 周边

插入数据库没成功

0
[已解决问题] 解决于 2016-04-26 17:05

这是要插入数据的表

create table message(
    id int primary key unique not null auto_increment,
    nickname varchar(30) not null,
    email varchar(50),
    text text,
    sex varchar(10),
    time TIMESTAMP default current_Timestamp
);

这是获得页面插入的数据,能够打印出来,没错

        String nickname=req.getParameter("name");
        String email=req.getParameter("email");
        String sex=req.getParameter("selection");
        String message=req.getParameter("message");
        String sql="insert into message(nickname,email,text,sex) values('"+nickname+"','"+email+"','"+message+"','"+sex+"')";
        Boolean isOK=new DB().executeUpdate(sql);

isOK是false;

int count=excuteUpdate(sql);这里失败了

这是DB工具public boolean executeUpdate(String sql){

if(con==null){
             createCon();
         }else if(stmt==null){
             getStm();
         }
         try{
             int count=stmt.executeUpdate(sql);
             System.out.println("为什么不行能?count:"+count);
             return true;
         }catch(Exception e){
             System.out.println(e);
//报了java.lang.NullPointerException
return false; }finally{ this.closed(); } }

 

 

不被女生喜欢好多年的主页 不被女生喜欢好多年 | 初学一级 | 园豆:39
提问于:2016-04-26 16:04
< >
分享
最佳答案
0

把  executeUpdate     换成  execute  再试试

奖励园豆:5
丢了幸福的猪227 | 菜鸟二级 |园豆:261 | 2016-04-26 16:08

没成功。还是报 java.lang.NullPointerException

不被女生喜欢好多年 | 园豆:39 (初学一级) | 2016-04-26 16:22

@~biubiu:  你数据库用的 mysql?    字段有非空的吗?  还有,你可以大断点跟一下,看到底是哪行报错!!!

丢了幸福的猪227 | 园豆:261 (菜鸟二级) | 2016-04-26 16:25

@丢了幸福的猪227: mysql 表的设计,我发了

不被女生喜欢好多年 | 园豆:39 (初学一级) | 2016-04-26 16:34

@~biubiu: 把你后台打印的sql  直接在管理工具中 运行一下 看看到底是咋回事

丢了幸福的猪227 | 园豆:261 (菜鸟二级) | 2016-04-26 16:36

@丢了幸福的猪227: 管理工具里能插入成功的

不被女生喜欢好多年 | 园豆:39 (初学一级) | 2016-04-26 16:37

@~biubiu: 插入语句是没错的

不被女生喜欢好多年 | 园豆:39 (初学一级) | 2016-04-26 16:38

@~biubiu: 你不应该啊

丢了幸福的猪227 | 园豆:261 (菜鸟二级) | 2016-04-26 16:38

@~biubiu:       int count=stmt.executeUpdate(sql);  断点看看 stmt   这个是不是为空!!!

丢了幸福的猪227 | 园豆:261 (菜鸟二级) | 2016-04-26 16:45

@丢了幸福的猪227:

     public boolean executeUpdate(String sql){
         /*if(con==null){
             createCon();
         }else if(stmt==null){
             getStm();
         }*/
         try{
             createCon();
             stmt=con.createStatement();
             int count=stmt.executeUpdate(sql);
             return true;
         }catch(Exception e){
             System.out.println("失败____________________________________");
             System.out.println(e);
             return false;
         }finally{
             this.closed();
         }
     }

stmt 不知道为什么没实例化,现在改成这样能插入了

不被女生喜欢好多年 | 园豆:39 (初学一级) | 2016-04-26 16:58

@~biubiu: /*if(con==null){ createCon(); }else if(stmt==null){ getStm(); }*/


写成这样if(con==null){ createCon(); }
if(stmt==null){ getStm(); }        就好了!!!

丢了幸福的猪227 | 园豆:261 (菜鸟二级) | 2016-04-26 17:00

@丢了幸福的猪227: 对,哎找了一下午

不被女生喜欢好多年 | 园豆:39 (初学一级) | 2016-04-26 17:04
其他回答(1)
0

String message=req.getParameter("message");你的插入表里不是这个参数么text text,

 

wtu文 | 园豆:161 (初学一级) | 2016-04-26 16:43

对啊,我sql语句里面有啊

不知道是不是我理解错了还是什么String message=req.getParameter("message")这里你得到message的属性  但是你插入语句里insert into message(nickname,email,text,sex) values('"+nickname+"','"+email+"','"+message+"','"+sex+"')" text的vaule值是message

支持(0) 反对(0) wtu文 | 园豆:161 (初学一级) | 2016-04-26 16:56

@wtu文: 数据库message表中有text这一元组 ,类型为text,我sql中是把得到message传进去,没错的。

问题找到了,DB工具类理executeUpdate 中stmt没实例化。thankyou

@~biubiu: 没事  也没有帮到你 

支持(0) 反对(0) wtu文 | 园豆:161 (初学一级) | 2016-04-26 17:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册