Pagina 1 di 3 1 2 3 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 22
  1. #1
    Utente di HTML.it L'avatar di 1sirena
    Registrato dal
    Mar 2008
    Messaggi
    163

    [java]modifica database da jTextField

    Salve a tutti avrei un problema!!! ho creato un Jpanel con delle JtextField all'interno delle quali riesco a visualizzare i dati presenti in un database al click di un bottone, ma non ho la più pallida idea d come fare per modificarli; vi posto il codice:

    codice:
    private JButton getJButVis() {
    	if(jButVis == null) {
    		jButVis = new JButton();
    		jButVis.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    		jButVis.setText("Visualizza dati");
    		
    		jButVis.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e){ 
    				try {
    					//System.out.println("ciao....");
    					visualizzaarchivio(e);
    				} catch (SQLException ex) {
    					ex.printStackTrace();
    				} catch (ClassNotFoundException ex) {
    					ex.printStackTrace();
    				}
    			} 
    		});	
    	}
    	return jButVis;
    }
    
    public void visualizzaarchivio(ActionEvent e)throws SQLException, ClassNotFoundException{
    	
    	SQLManager sqlm = new SQLManager("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/schedule","root","root");
    	String query = "select * from Utente where cognome = '" + this.getCognomeDocente() + "'";
    	//System.out.println("**********: " + query);
    	ResultSet rs = sqlm.execute(query);
    	while (rs.next()) {
    		//System.out.println("nome dalla query: " + rs.getString("nome"));
    		TextNome.setText(rs.getString("nome"));
    		TextCognome.setText(rs.getString("cognome"));
    		jPasswordField1.setText(rs.getString("password"));  
    		TextID_Utente.setText(rs.getString("id_utente"));
    		TextTelefono.setText(rs.getString("telefono"));
    		TextFax.setText(rs.getString("fax"));
    		TextTipo_Utente.setText(rs.getString("tipo_utente"));
    		TextEmail.setText(rs.getString("email"));
    		TextDipartimento.setText(rs.getString("dipartimento"));
    		TextImg.setText(rs.getString("img"));
    	}
    	//eseguire la query 
    	//settare i parametri
    	
    	@SuppressWarnings("unused")
    	boolean  inserisci = sqlm.esegui(query); 
    	 
    sqlm.close();
    AIUTATEMI!!!!

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

    Re: [java]modifica database da jTextField

    Originariamente inviato da 1sirena
    ho creato un Jpanel con delle JtextField all'interno delle quali riesco a visualizzare i dati presenti in un database al click di un bottone, ma non ho la più pallida idea d come fare per modificarli
    Una volta che hai confermato il tutto (tramite un pulsante o altro), crei una query di update "UPDATE Utente ....." ci metti dentro i dati presi dai vari componenti e poi la esegui.
    Così però dovresti preoccuparti tu di quotare le stringhe e fare l'escape dei caratteri speciali. Altrimenti usi un PreparedStatement. Oppure se il ResultSet da cui avevi preso i dati lo fai creare di tipo "updatable" e non l'hai "buttato via", puoi fare l'update sui vari campi e poi aggiornare la riga.


    Poi, scusa, 2 cose: sei sicuro che sia una buona idea specificare i parametri di connessione nel sorgente?
    SQLManager sqlm = new SQLManager("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/schedule","root","root");

    E poi se sai già che avrai solo 1 riga (immagino io, vedendo il codice), perché fai un while (rs.next()) ??
    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 L'avatar di 1sirena
    Registrato dal
    Mar 2008
    Messaggi
    163
    Questo è quello che ho provato a fare ... ora come ora al click del bottone non fa altro che azzerarmi le jtextfield generando un'eccezione... Perche'??



    [ECCEZIONE]
    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 '('Enrichetta', 'Gentile', '00001', '1', '080-5443271','080-5443271', 'Dottoressa' 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.modificaarchi vio(PannelloDocente.java:399)
    at org.schedule.oggetti.PannelloDocente$2.actionPerfo rmed(PannelloDocente.java:365)
    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]

    il codice che ho utilizzato è questo:
    codice:
    
    private JButton getJButton1() {
    	if(jButton1 == null) {
    		jButton1 = new JButton();
    		jButton1.setText("Modifica dati");
    		jButton1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    		jButton1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) { 
    				try {
    					//System.out.println("ciao....");
    					modificaarchivio(e);
    				} catch (SQLException ex) {
    					ex.printStackTrace();
    				} catch (ClassNotFoundException ex) {
    					ex.printStackTrace();
    				}
    			}
    		});
    	}
    	return jButton1;
    }
    
    @SuppressWarnings("deprecation")
    public void modificaarchivio(ActionEvent e)throws SQLException, ClassNotFoundException{
    	
    	SQLManager sqlm = new SQLManager("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/schedule","root","root");
    	String query = "update Utente set ('"+ TextNome.getText() +"', '"+ TextCognome.getText() +"', '"+ jPasswordField1.getText() +"', " +
    		"'"+ TextID_Utente.getText() +"', '"+ TextTelefono.getText() +"','"+ TextFax.getText() +"', '"+ TextTipo_Utente.getText() +"', '"+ TextEmail.getText() +"'," +
    				"'"+ TextDipartimento.getText() +"', '"+ TextImg.getText() +"')where cognome = '" + this.getCognomeDocente() + ")";
    	//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")
    	boolean  inserisci = 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
    	
    }

  4. #4
    Utente di HTML.it L'avatar di Alex'87
    Registrato dal
    Aug 2001
    residenza
    Verona
    Messaggi
    5,802
    Originariamente inviato da 1sirena
    [ECCEZIONE]
    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 '('Enrichetta', 'Gentile', '00001', '1', '080-5443271','080-5443271', 'Dottoressa' at line 1"
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:2001)
    Te lo dice cosa c'è che non va, hai sbagliato qualcosa nella sintassi ^^'
    SpringSource Certified Spring Professional | Pivotal Certified Enterprise Integration Specialist
    Di questo libro e degli altri (blog personale di recensioni libri) | ​NO M.P. TECNICI

  5. #5
    Utente di HTML.it L'avatar di 1sirena
    Registrato dal
    Mar 2008
    Messaggi
    163
    grazie ... lo avevo capito ... non ho capito cosa ... ;P

  6. #6
    Utente di HTML.it L'avatar di Metflar
    Registrato dal
    Apr 2007
    Messaggi
    790
    scusa ma il 4° campo è un id e presumo sia un intero perché lo metti tra singolo apice?

  7. #7
    Utente di HTML.it L'avatar di 1sirena
    Registrato dal
    Mar 2008
    Messaggi
    163
    se ti riferisci a questo: ' "+ TextID_Utente.getText() +" '

    L'ho già utilizzato per l'inserimento nel database e funziona!
    Quindi non perso sia quello l'errore di sintassi a cui si riferisce l'eccezione.

  8. #8
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da 1sirena
    Quindi non perso sia quello l'errore di sintassi a cui si riferisce l'eccezione.
    Osservando bene:

    where cognome = '" + this.getCognomeDocente() + ")";

    manca l'apice di chiusura di quotazione della stringa.

    E comunque tutto questo ha un "piccolo" problema: e se nei text field l'utente inserisce proprio un apice (es. un cognome D'Alessi)?? La tua query smette di funzionare ....

    Poi altra cosa: lungi da me dal ritenermi esperto di MySQL, ma non mi sembra che preveda una sintassi:

    update nometabella set (xxxx,yyyy ......)
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  9. #9
    Utente di HTML.it L'avatar di 1sirena
    Registrato dal
    Mar 2008
    Messaggi
    163
    ho inserito l'apice cm m hai detto,... ma continua a darmi errore:

    [eccezione]
    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 '('Giovanni', 'Gentile', '00001', '1', '080-5443271','080-5443271', 'Dottoressa'' 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.modificaarchi vio(PannelloDocente.java:399)
    at org.schedule.oggetti.PannelloDocente$2.actionPerfo rmed(PannelloDocente.java:365)
    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]



    cmq per qnt riguarda la sintassi dell'update:

    nel nstr caso dobbiamo passargli i valori ke inseriamo nella jtextfield, quindi nn possiamo insere i valori nella query ... tutto qui!

    Aiutooooooooooooooooooooooooooooooooo!!!!!!

  10. #10
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da 1sirena
    cmq per qnt riguarda la sintassi dell'update:

    nel nstr caso dobbiamo passargli i valori ke inseriamo nella jtextfield, quindi nn possiamo insere i valori nella query ... tutto qui!
    Ripeto che pur non sapendone molto di MySQL, se vado a leggere sul manuale vedo che la sintassi è:

    UPDATE [LOW_PRIORITY] [IGNORE] tbl_name
    SET col_name1=expr1 [, col_name2=expr2 ...]
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]

    Quindi non vedo una sintassi del tipo: ... set (....)
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

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.