简体   繁体   English

JDBC删除和更新方法

[英]JDBC Delete and Update Methods

I'm trying to make a doctor JDBC with SQLite, but when I call the Delete and the Update methods, I get these error messages: 我正在尝试使用SQLite创建医生JDBC,但是当我调用Delete和Update方法时,出现以下错误消息:

Exception in thread "main" java.sql.SQLException: Doctor not deleted 线程“主”中的异常java.sql.SQLException:医生未删除

[SQLITE_BUSY] The database file is locked (database is locked) [SQLITE_BUSY]数据库文件已锁定(数据库已锁定)

These are the main class and the two methods in the DoctorDAO class. 这些是主类,也是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;

   }

One obvious error that I can see in your code - for Update, you are saying 我可以在您的代码中看到一个明显的错误-对于Update,您说的是

String query = "update doctors set Name = ? where Id = ?";

then you are doing, 那你在做

preparedStmt.setString(1,Id);
preparedStmt.setString (2,newName);

which is the reversed order. 这是相反的顺序。 As per your query name is first parameter and id is second parameter but you are setting values in reverse. 按照您的查询名称是第一个参数, id是第二个参数,但是您要反向设置值。

Since code that you pasted isn't working code, can't suggest much for database is locked error but to avoid database is locked error, closing connection is recommended, even if you get an Exception so try to close connection inside a finally block. 由于您粘贴的代码不是有效的代码,因此不能为数据库锁定错误提供太多建议,但为避免数据库锁定错误,即使出现Exception ,也建议关闭连接,因此请尝试在finally块内关闭连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM