jeudi 13 août 2015

delete a row with prestatement?

I would like to delete a row but I can not connect to my database. I have difficulties to connect with me. My problem is after DELETE FROM outil WHERE id_outil=?"; try { public class DeleteOutil extends SwingWorker { private final String outil; private final JButton toEnable;

    public DeleteOutil(String outil, JButton toEnable) {
        this.outil = outil;
        this.toEnable = toEnable;
    }
    @Override
   public Void doInBackground() {
      PreparedStatement stmt=null;
        String wql = "DELETE  FROM outil WHERE id_outil=?";
        try {
            Connexion con = Connexion.getConnection();
            stmt = con.prepareStatement(wql);

            stmt.setString(1, "outil");
            stmt.executeUpdate();
      }
      catch (Exception e)   
        }
     finally {
   if ( stmt!=null ) {
          // fermer/libérer la ressource
       try {
          stmt.close();
       }
       catch (Exception e) {   
        }
   }
        }  return null;
    }
    @Override
    protected void done() {
        toEnable.setEnabled(true);
    }    
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:

    jButton1.setEnabled(false);
    DeleteOutil worker = new DeleteOutil(TableOutil.getValueAt(TableOutil.getSelectedRow(), 0).toString(), jButton1);
    worker.execute();
}

this my connect code:

public class Connexion  {
    String urlPilote="com.mysql.jdbc.Driver";//Direction pour charger le pilote
    String urlBasedonnees="jdbc:mysql://localhost:3306/bdboiteoutil";// Direction pour la connexion à la base de données
    Connection conn;
    public Connexion () {
    //On charge  notre pilote
    try{
       Class.forName(urlPilote);
       System.out.println("Le pilote est chargé");
      }
    catch(ClassNotFoundException ex){
      System.out.println(ex);
     }
    // On se connecte à la base de donnée
    try{
        conn=DriverManager.getConnection(urlBasedonnees,"root","");
        System.out.println("La Base de données est chargé");
    }
    catch(SQLException  ex){
        System.out.println(ex);
    }
        }
    Connection ObtenirConnexion(){
         return conn;
     }
     public void setAutoCommit(boolean autoCommit) throws SQLException {
       conn.setAutoCommit(autoCommit);   
}
    public void close() throws SQLException {
       conn.close();
    }
     public void rollback() throws SQLException {
       conn.rollback();
     }
     public void commit() throws SQLException {
       conn.commit();
    }
     public PreparedStatement prepareStatement(String sql) throws SQLException {
       return conn.prepareStatement(sql);
}

public static void printSQLException(SQLException ex) {
    for (Throwable e : ex) {
      if (e instanceof SQLException) {
        if (ignoreSQLException(((SQLException)e).getSQLState()) == false) {
          e.printStackTrace(System.err);
          System.err.println("SQLState: " + ((SQLException)e).getSQLState());
          System.err.println("Error Code: " + ((SQLException)e).getErrorCode());
          System.err.println("Message: " + e.getMessage());
          Throwable t = ex.getCause();
          while (t != null) {
            System.out.println("Cause: " + t);
            t = t.getCause();
          }
        }
      }
    }
  }
public static boolean ignoreSQLException(String sqlState) {
    if (sqlState == null) {
      System.out.println("The SQL state is not defined!");
      return false;
    }
    // X0Y32: Jar file already exists in schema
    if (sqlState.equalsIgnoreCase("X0Y32"))
      return true;
    // 42Y55: Table already exists in schema
    if (sqlState.equalsIgnoreCase("42Y55"))
      return true;
    return false;
  }
}      



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire