我是用Mysql数据库的;
这是我创建的表
CREATE TABLE note
(
id int not null auto_increment primary key ,
title varchar(20) not null ,
author varchar(20) not null ,
content varchar(50) not null
) ;主要不明白的就是这一段小地方!
String sql="insert into note values(null,?,?,?)";
try{
Class.forName(driver);
conn=DriverManager.getConnection(durl,dname,dpassword);
pst=conn.prepareStatement(sql);
pst.setString(1,title);
pst.setString(2,author);
pst.setString(3,content);
pst.executeUpdate() ;//当执行到这步就会跳到;catch
pst.close(); //这里就不执行了,这是为什么啊???????
conn.close();
}catch(Exception e){
System.out.println(flag);//执行这里
}
这是我创建的表
CREATE TABLE note
(
id int not null auto_increment primary key ,
title varchar(20) not null ,
author varchar(20) not null ,
content varchar(50) not null
) ;主要不明白的就是这一段小地方!
String sql="insert into note values(null,?,?,?)";
try{
Class.forName(driver);
conn=DriverManager.getConnection(durl,dname,dpassword);
pst=conn.prepareStatement(sql);
pst.setString(1,title);
pst.setString(2,author);
pst.setString(3,content);
pst.executeUpdate() ;//当执行到这步就会跳到;catch
pst.close(); //这里就不执行了,这是为什么啊???????
conn.close();
}catch(Exception e){
System.out.println(flag);//执行这里
}
但没有报错的啊!
在这里输出:
”System.out.println(flag);//执行这里“
false.
因为我本来设了flag为false.这里我只是为了验证是否跳到这里执行!
//这句不要了System.out.println(flag);//执行这里
e.printStackTrace();
}
改成这样就会有错误然后你把错误贴出来。估计是字段不能为空的问题
先看这一句 由于你没有指定参数所对应数据库里的列名所以:你的一个参数也就是null对应的是数据库里的主键。
主键是不能为空的。所以,会出异常。