Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    Errore query con valore autoincrement

    Salve ragazzi ho un nuovo quesito da porvi

    Ho un database con alcune tabelle.

    Sto creando una pagina in java in cui il sistema prende da alcune text box, dei valori e li utilizza per lanciare una query di inserimento.

    Purtroppo una tabella ha come chiave primaria un indice che si autoincrementa da solo.

    La query che lancio è la seguente:

    codice:
    db.eseguiQuery("INSERT INTO archiviolocalita(`localita`, `indirizzo`) VALUES ( '"+localita+"', '"+indirizzo+"') ;");
    dove localita e indirizzo sono due valori string che ho preso dal form.

    l'eccezione che mi viene lanciata è la seguente

    Can not issue data manipulation statements with executeQuery().
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:1075)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:989)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:984)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:929)
    at com.mysql.jdbc.StatementImpl.checkForDml(Statement Impl.java:438)
    at com.mysql.jdbc.StatementImpl.executeQuery(Statemen tImpl.java:1379)
    at Pullman_versione_1_0.Database.eseguiQuery(Database .java:82)
    at Pullman_versione_1_0.PanelArchivioLocalita$1.actio nPerformed(PanelArchivioLocalita.java:234)
    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)



    ho provato anche a lanciare questa query nella consolle di mysql e funziona correttamente. dove sta l'errore???

    posto anche lo statement della classe database che ho implementato:

    codice:
    public Vector eseguiQuery(String query) {
    	      Vector v = null;      
    	      int colonne = 0;
    	      try {
    	         Statement stmt = db.createStatement();     // Creo lo Statement per l'esecuzione della query
    	         ResultSet rs = stmt.executeQuery(query);   // Ottengo il ResultSet dell'esecuzione della query
    	         v = new Vector();
    	         ResultSetMetaData rsmd = rs.getMetaData();
    	         //rsmd = rs.getMetaData();
    	         colonne = rsmd.getColumnCount();
    	//qualche modifica qui, in modo che si lavori su Vector piuttosto
    	//che su String[]
    	         while(rs.next()) {   // Creo il vettore risultato scorrendo tutto il ResultSet
    	            Vector record = new Vector();
    	            for (int i=0; i<colonne; i++) {
    	                record.add(rs.getString(i+1));
    	            }
    	            v.add(record);
    	         }
    	         rs.close();     // Chiudo il ResultSet
    	         stmt.close();   // Chiudo lo Statement
    	      } catch (Exception e) { e.printStackTrace(); errore = e.getMessage(); }
    
    	      return v;
    	   }

  2. #2
    Utente di HTML.it L'avatar di Lomio
    Registrato dal
    Jul 2010
    Messaggi
    160
    Per le query di INSERT, UPDATE E DELETE devi usare il metoto eseguiAggiornamento( ) della classe Database del buon LeleFT, eseguiQuery( ) lo devi utilizzare esclusivamente per le SELECT!
    La legge e l'ordine saranno rispettati solo quando si concederà la giustizia a tutti indistintamente.

  3. #3
    Utente di HTML.it L'avatar di desa
    Registrato dal
    Oct 2008
    Messaggi
    569
    Sì, be'... ma alla radice la questione è che devi utilizzare executeUpdate(), non executeQuery()

  4. #4
    scusate ragazzi, non so come ho fatto a non accorgermeneeee

    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 © 2025 vBulletin Solutions, Inc. All rights reserved.