【发布时间】:2016-12-09 05:23:00
【问题描述】:
我正在尝试使用 SQLite 创建一个医生 JDBC,但是当我调用 Delete 和 Update 方法时,我收到以下错误消息:
线程“main”java.sql.SQLException 中的异常:Doctor 未删除
[SQLITE_BUSY] 数据库文件被锁定(数据库被锁定)
这是DoctorDAO类中的主类和两个方法。
public static void main(String[] args) throws SQLException, IOException {
scanner = new Scanner(System.in);
sc = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
DoctorDAO a = new DoctorDAO() ;
Doctor d1 = new Doctor(null,null,null,(char) 0,null,null,null,null,null,null);
boolean ins = a.insertEmployee(d1);
if(ins){System.out.println("has added");}
else{System.out.println("not added");}
System.out.println("Give Doctor's Id");
String id1 = sc.nextLine();
a.updateDoctrorByIds(id1);
}
public boolean updateDoctrorByIds(String Id) throws SQLException, IOException
{
boolean b = true;
try
{
getConnection();
c.setAutoCommit(false);
// create the java mysql update preparedstatement
Scanner sca = new Scanner(System.in);
System.out.println("Give the new Name");
String newName = sca.nextLine();
String query = "update doctors set Name = ? where Id = ?";
PreparedStatement preparedStmt = c.prepareStatement(query);
preparedStmt.setString(1,Id);
preparedStmt.setString (2,newName);
// execute the java preparedstatement
preparedStmt.executeUpdate();
c.commit();
closeConnection();
}
catch (Exception e)
{ b =false ;
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
return b;
}
public boolean deleteDoctrorById(String Id) throws SQLException` {
boolean b = true;
try {
getConnection();
c.setAutoCommit(false);
System.out.println("Delete operation");
String query = "DELETE FROM doctors WHERE ID ='"+Id+"';";
s = c.createStatement();
s.executeUpdate(query);
//System.out.println(res);
c.commit();
s.close();
c.close();
//closeConnection();
System.out.println("/");
System.out.println("/");
System.out.println("Successfully Deleted");
System.out.println("/");
System.out.println("/");
}
catch (SQLException s){
b = false;
throw new SQLException("Doctor not deleted"); }
return b;
}
【问题讨论】: