java mysql插入记录问题

来源:百度知道 编辑:UC知道 时间:2024/06/21 06:48:09
public boolean add(User user) {
boolean bool=false;
conn=DBCONNFactory.getMysqlCONN();
String sql="insert into user values (?,?,?,?,?,?,?,?)";
try {
ps=conn.prepareStatement(sql);
ps.setInt(1, maxId()+1);
ps.setString(2, user.getUsername());
ps.setString(3, user.getPassword());
ps.setString(4, user.getEmail());
if(user.getAddress()==null)
user.setAddress(" ");
ps.setString(5, user.getAddress());
if(user.getTel()==null)
user.setTel(" ");
ps.setString(6, user.getTel());
if(user.getRealname()==null)
user.setRealname(" ");
ps.setString(7, user.getRealname());
ps.setInt(8, 0);
int i=ps.executeUpdate();
if(i>0)
bool=true;
return bool;
} catch (SQLException e) {
e.printStackTrace();
throw new MyRuntimeException("添加用户时失败!",e);
}fin

我没遇到过这样的问题,给你分析一下可能出现问题的地方

有可能是DBCONNFactory.getMysqlCONN()方法有问题,看看是不是有别的线程也调用了该connection。
也有可能是是表结构的问题,你把mysql中表的结构改变成innodb试试
再检查DBCONNFactory.close(rs, conn, ps)方法,一般是这样写的
try {
if (conn != null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (st != null)
st.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

总之,问题是你在在statement关闭的时候操作没有成功
多检查一下你的程序,你上面写的代码应该没有问题

这个主要是因为你的连接关闭了,你仔细检查从得到连接到执行ps.executeUpdate();时有没有关闭连接的操作,尽从你给的代码来看,应该没错。 建议你看看你的ps 及conn相关代码的是不是有还没执行就关闭了连接

从这些代码来看没有问题,提示是statement 关闭了,这个要看看你自己定义的方法DBCONNFactory.getMysqlCONN();里面是不是把statement给关闭了