Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it
    Registrato dal
    Aug 2009
    Messaggi
    37

    Java e MySQL

    Salve, ho un problema con l'aggiornamento di un database MySQL attraverso java.
    In pratica ho creato un sistema che controlla vari elementi ambientali, come temperatura, umidità, ecc. ed inserisce ogni volta l'ultima rilevazione in un database.
    I campi della tabella (si chiama "env_sensors_curr_val") sono:
    ID_ESV, intero, chiave primaria, identificativo del dispositivo di rilevamento
    ESCV_NAME, char(20) nome del dispositivo (temperatura, ecc.)
    ESCV_VAL, char(20) rilevazione del dispositivo
    ESCV_DATE, char(20) data della rilevazione

    Se la tabella è vuota, non ho problemi, in quanto dopo aver eseguito queste linee

    codice:
    SQL = "SELECT ID_ESV FROM env_sensors_curr_val";
    SQLPreparedStatement = con.prepareStatement(SQL);
    rsQuery = SQLPreparedStatement.executeQuery();
    faccio un controllo su rsQuery per vedere se ha elementi oppure no. Se non ce li ha eseguo una INSERT e tutto funziona a meraviglia. Il problema c'è quando cerco di fare una UPDATE. Il codice che ho usato è questo:

    codice:
    while (rsQuery.next()){
    	sem = 1;
    	int id = rsQuery.getInt("ID_ESV");
    	if (id == 1 && array[0].equals("temperatura")){
    		SQL = "UPDATE env_sensors_curr_val SET ESCV_VAL = ?, ESCV_DATE = ?, WHERE ESCV_NAME = ?";
    		SQLPreparedStatement = con.prepareStatement(SQL);
    		SQLPreparedStatement.setString(1, array[1]); // contiene il valore rilevato
    		SQLPreparedStatement.setString(2, array[2]); // contiene la data del rilevamento
    		SQLPreparedStatement.setString(3, array[0]); // contiene il nome del dispositivo
    		SQLPreparedStatement.executeUpdate();
    		SQLPreparedStatement.close();
    		System.out.println("Temperatura aggiornata!");
    	}
    	else if (id == 2 && array[0].equals("umidita")){
    		SQL = "UPDATE env_sensors_curr_val SET ESCV_VAL = ?, ESCV_DATE = ?, WHERE ESCV_NAME = ?";
    		SQLPreparedStatement = con.prepareStatement(SQL);
    		SQLPreparedStatement.setString(1, array[1]);
    		SQLPreparedStatement.setString(2, array[2]);
    		SQLPreparedStatement.setString(3, array[0]);
    		SQLPreparedStatement.executeUpdate();
    		SQLPreparedStatement.close();
    		System.out.println("Umidità aggiornata!");
    	}
    }
    Il compilatore mi restituisce questo errore:

    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorEx ception: 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 'WHERE ESCV_NAME = 'temperatura'' at line 1

    Dove sto sbagliando???
    www.ariadifan.it - Ariadineve Official Fan Club -

    God save the Queen

    Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: Java e MySQL

    Originariamente inviato da dvoran
    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorEx ception: 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 'WHERE ESCV_NAME = 'temperatura'' at line 1

    Dove sto sbagliando???
    Devi togliere la virgola "," prima del WHERE. Non ci vuole!
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    Utente di HTML.it
    Registrato dal
    Aug 2009
    Messaggi
    37

    Immaginavo che fosse un errore così stupido!

    Grazie mille, ora funziona!
    Olè!
    www.ariadifan.it - Ariadineve Official Fan Club -

    God save the Queen

    Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something

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.