在maven项目里面的problemDAO模块中插入操作出现问题,但是删除,修改等操作都没有问题。
可能是int ret = statement.executeUpdate();
这里出了问题(?)
public class ProblemDAO {
/**
* 插入
* @param problem
*/
public void insert(Problem problem){
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DBUtil.getConnection();
String sql = "insert into oj_table values(null,?,?,?,?,?)";
statement = connection.prepareStatement(sql);
statement.setString(1, problem.getTitle());
statement.setString(2, problem.getLevel());
statement.setString(3,problem.getDescription());
statement.setString(4, problem.getTestCode());
statement.setString(5, problem.getTemplateCode());
int ret = statement.executeUpdate();
if(ret != 1){
System.out.println("题目新增失败!");
}else {
System.out.println("题目新增成功!");
}
} catch (SQLException e) {
//System.out.println("出错了");
e.printStackTrace();
}finally {
DBUtil.close(connection,statement,null);
}
}
/**
* 删除
* @param id
*/
public void delete(int id){
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DBUtil.getConnection();
String sql = "delete from oj_table where id = ?";
statement = connection.prepareStatement(sql);
statement.setInt(1,id);
int ret = statement.executeUpdate();
if(ret != 1){
System.out.println("删除失败!");
}else {
System.out.println("删除成功!");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(connection,statement,null);
}
}
/**
* 查找题目列表
* @return
*/
public List<Problem> selectAll(){
ArrayList<Problem> problems = new ArrayList<>();
PreparedStatement statement = null;
ResultSet resultSet = null;
Connection connection = null;
try {
connection = DBUtil.getConnection();
String sql = "select id,title,level from oj_table";
statement = connection.prepareStatement(sql);
resultSet = statement.executeQuery();//执行查询
while(resultSet.next()){
Problem problem = new Problem();
problem.setId(resultSet.getInt("id"));
problem.setTitle(resultSet.getString("title"));
problem.setLevel(resultSet.getString("level"));
problems.add(problem);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(connection,statement,resultSet);
}
return problems;
}
public static void main(String[] args) {
Problem problem = new Problem();
ProblemDAO problemDAO = new ProblemDAO();
problem.setTitle("两数相加");
problem.setLevel("简单");
problem.setDescription("2");
problem.setTemplateCode("2");
problem.setTestCode("2");
problemDAO.insert(problem);
System.out.println("插入成功");
}
}
运行结果及报错内容
java.sql.SQLException: #HY000
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3933)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3869)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2675)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2465)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1915)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2136)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2070)
at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5187)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2055)
at dao.ProblemDAO.insert(ProblemDAO.java:35)
at dao.ProblemDAO.main(ProblemDAO.java:154)
插入成功
Process finished with exit code 0