csnd_xxnn
无悔zz
2017-06-04 13:13

学生成绩管理系统添加班级模块错误,下面是数据库的操作代码和控制台的代码,还有错误提示,求帮忙

  • java
  • 数据库

//添加班级
public void addClass(int pid,String className) {
String sql="insert into class (pid,classname) values(?,?)";
Connection connection=null;
PreparedStatement p=null;
try {
connection=DbUtil.getConnection();
//手动提交事务
connection.setAutoCommit(false);
p=(PreparedStatement) connection.prepareStatement(sql);
p.setInt(1, pid);
p.setString(2, className);
p.executeUpdate();
//获取当前班级的父班级
Classes parentClass=findClassById(pid);
//如果父班级为叶子
if (parentClass.getLeaf()==1) {
modLeaf(connection, parentClass.getClassId(), 0);
}
//提交事务
connection.commit();
} catch (Exception e) {
e.printStackTrace();
if (connection!=null) {
try {
connection.rollback();
} catch (Exception e1) {
e1.printStackTrace();
}
}
} finally {
DbUtil.colse(p);
//恢复成自动提交
DbUtil.setAutoCommit(connection, true);
DbUtil.colse(connection);
}
}
/**
修改叶子值如果为叶子
@throws SQLException */

private void modLeaf(Connection connection,int classId,int leaf) throws SQLException{
String sql="update class set leaf=? where classid=?";
PreparedStatement p=null;

try {
p=(PreparedStatement) connection.prepareStatement(sql);
p.setInt(1, leaf);
p.setInt(2, classId);
p.executeUpdate();
} finally {
DbUtil.colse(p);
}
}

//根据ID查找班级
查询父班级是否为叶子
如果存在返回班级,不存在返回空

public Classes findClassById(int classId) {

String sql="select *form class where classid=?" ;

Connection connection=null;
PreparedStatement p=null;
ResultSet rs=null;
Classes classes=null;
try {
connection=DbUtil.getConnection();
p=(PreparedStatement) connection.prepareStatement(sql);
rs=(ResultSet) p.executeQuery();
if(rs.next()) {
classes=new Classes();
classes.setClassId(classId);
classes.setClassName(rs.getString("classname"));
classes.setPid(rs.getInt("pid"));
classes.setLeaf(rs.getInt("leaf"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DbUtil.colse(rs);
DbUtil.colse(p);
DbUtil.colse(connection);
}
return classes;
}
}

控制台代码,应该没错,s为输出的添加信息
int pid=Integer.parseInt(s.substring(s.indexOf("=")+1, s.indexOf(",")));
String className=s.substring(s.lastIndexOf("=")+1, s.length());
ClassManage.getInstance().addClass(pid, className);
错误信息:
1-添加班级
2-删除班级
3-修改班级
4-查询班级
q-退出
1
请输入添加的班级名(classname=#):
pid=3,classname=一年一班_01

java.sql.SQLException: Field 'leaf' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3256)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1313)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1585)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1500)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1485)
at com.coursemanage.lsq.ClassManage.addClass(ClassManage.java:67)
at com.coursecontroller.lsq.ClassController.main(ClassController.java:47)

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答