簡體   English   中英

JDBC刪除和更新方法

[英]JDBC Delete and Update Methods

我正在嘗試使用SQLite創建醫生JDBC,但是當我調用Delete和Update方法時,出現以下錯誤消息:

線程“主”中的異常java.sql.SQLException:醫生未刪除

[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;

   }

我可以在您的代碼中看到一個明顯的錯誤-對於Update,您說的是

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

那你在做

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

這是相反的順序。 按照您的查詢名稱是第一個參數, id是第二個參數,但是您要反向設置值。

由於您粘貼的代碼不是有效的代碼,因此不能為數據庫鎖定錯誤提供太多建議,但為避免數據庫鎖定錯誤,即使出現Exception ,也建議關閉連接,因此請嘗試在finally塊內關閉連接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM