Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it L'avatar di 1sirena
    Registrato dal
    Mar 2008
    Messaggi
    163

    cancellare dati database da JtextField

    prima ho aperto una discussione simile,
    ma stavolta riguarda la cancellazione dal database ....

    si puo' cancellare il contenuto di una riga prelevando i dati da delle textfield???

    io ho provato a fare cosi':

    [codice]

    if(jButton2 == null) {
    jButton2= new JButton();
    jButton2.setText("Cancella dati");
    jButton2.setCursor(Cursor.getPredefinedCursor(Curs or.HAND_CURSOR));
    jButton2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    try {
    //System.out.println("ciao....");
    cancellaarchivio(e);
    } catch (SQLException ex) {
    ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
    ex.printStackTrace();
    }
    }
    });
    }
    return jButton2;
    }
    @SuppressWarnings("deprecation")
    public void cancellaarchivio(ActionEvent e)throws SQLException, ClassNotFoundException{


    SQLManager sqlm = new SQLManager("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/schedule","root","root");

    String query = "DELETE nome = '"+ TextNome.getText()+"', cognome= '"+ TextCognome.getText() +"', password ='"+ jPasswordField1.getText() +"', " +
    " id_utente = '"+ TextID_Utente.getText() +"', telefono = '"+ TextTelefono.getText() +"',fax = '"+ TextFax.getText() +"', tipo_utente = '"+ TextTipo_Utente.getText() +"', email = '"+ TextEmail.getText() +"'," +
    "dipartimento = '"+ TextDipartimento.getText() +"', img ='"+ TextImg.getText() +"' FROM Utente WHERE cognome = '" + this.getCognomeDocente() + "'";

    System.out.println(query);



    //System.out.println("**********: " + query);
    //@SuppressWarnings("unused")
    //ResultSet rs = sqlm.execute(query);
    //while (rs.next()) {
    //System.out.println("nome dalla query: " + rs.getString("nome"));


    //}
    //eseguire la query
    //settare i parametri

    //@SuppressWarnings("unused")
    //boolean inserisci = sqlm.esegui(query);
    //@SuppressWarnings("unused")
    sqlm.esegui(query);
    sqlm.close();

    TextNome.setText("");
    TextCognome.setText("");
    jPasswordField1.setText("");
    TextID_Utente.setText("");
    TextTelefono.setText("");
    TextFax.setText("");
    TextTipo_Utente.setText("");
    TextEmail.setText("");
    TextDipartimento.setText("");
    TextImg.setText("");
    TextNome.requestFocus();


    }
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

    }


    [/codice]



    questa è l'eccezione:

    [ECCEZIONE]
    DELETE nome = 'Luciano', cognome= 'Fashon', password ='11111', id_utente = '10', telefono = '',fax = '', tipo_utente = '', email = '',dipartimento = '', img ='' FROM Utente WHERE cognome = 'Fashon'
    java.sql.SQLException: Syntax error or access violation message from server: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Luciano', cognome= 'Fashon', password ='11111', id_utente = '10', telefono = '' at line 1"
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:2001)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:11 68)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java :1279)
    at com.mysql.jdbc.Connection.execSQL(Connection.java: 2281)
    at com.mysql.jdbc.PreparedStatement.execute(PreparedS tatement.java:1419)
    at org.schedule.db.SQLManager.esegui(SQLManager.java: 89)
    at org.schedule.oggetti.PannelloDocente.cancellaarchi vio(PannelloDocente.java:474)
    at org.schedule.oggetti.PannelloDocente$3.actionPerfo rmed(PannelloDocente.java:435)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    [/ECCEZIONE]

  2. #2
    Utente di HTML.it L'avatar di Metflar
    Registrato dal
    Apr 2007
    Messaggi
    790
    codice:
    DELETE
        FROM
            Nome_Tabella
    WHERE
        Nome_Colonna = Un_Valore
    non vanno indicati anche i campi ma solo la condizione
    conoscenze: C/C++, Java, PHP, Python
    [No PM tecnici o di qualsiasi genere]

  3. #3
    Utente di HTML.it L'avatar di 1sirena
    Registrato dal
    Mar 2008
    Messaggi
    163
    io nel database ho la tabella utenti, con tutti questi dati:
    Nome;
    Cognome;
    Password;
    id_Utente;
    Telefono;
    Fax;
    Tipo_Utente;
    Email;
    Dipartimento;
    Img;

    vorrei ke i dati ke visualizzo in delle textfield vengano cancellati dalla tabella, non so' se la sintassi ke hai postato tu sia utile per qll ke voglio fare io ... perche io voglio cancellare un intera riga ... tu forse dici di mettere poi tutti i nomi delle colonne ke voglio eliminare???

    [codice]
    "DELETE FROM Utente WHERE nome = '"+ TextNome.getText()+"', cognome= '"+ TextCognome.getText() +"', password ='"+ jPasswordField1.getText() +"', " +
    " id_utente = '"+ TextID_Utente.getText() +"', telefono = '"+ TextTelefono.getText() +"',fax = '"+ TextFax.getText() +"', tipo_utente = '"+ TextTipo_Utente.getText() +"', email = '"+ TextEmail.getText() +"'," +
    "dipartimento = '"+ TextDipartimento.getText() +"', img ='"+ TextImg.getText() +"' "


    [/codice]

  4. #4
    Utente di HTML.it L'avatar di Metflar
    Registrato dal
    Apr 2007
    Messaggi
    790
    con la sintassi postata prima cancella la riga che soddisfa quella condizione ...
    nell'esempio che hai riportato che è sbagliato perché dovresti legare tutti i campi con and..ma puoi fare tutto tramite un campo che identifica univocamente la riga che hai [cioè la chiave] in teoria è id_utente
    conoscenze: C/C++, Java, PHP, Python
    [No PM tecnici o di qualsiasi genere]

  5. #5
    Utente di HTML.it L'avatar di 1sirena
    Registrato dal
    Mar 2008
    Messaggi
    163
    grazie mille

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.