java - How can I do to update a row like a pro? -
package gestionoutilbricolage; import java.sql.*; import javax.swing.table.defaulttablemodel; import javax.swing.jtable; public class fenetrelisteoutil extends javax.swing.jframe { string outil; statement stmta; statement stmt; statement stmtlisteoutil; connexion maconnexion = new connexion(); public fenetrelisteoutil() { initcomponents(); defaulttablemodel model = new defaulttablemodel(); model.addcolumn("nom de l' outil"); model.addcolumn("fabricant"); model.addcolumn("annee de fabrication"); model.addcolumn("prix"); string requetelisteoutil = "select *from outil"; try{ stmtlisteoutil=maconnexion.obtenirconnexion().createstatement(); resultset resultat = stmtlisteoutil.executequery(requetelisteoutil); while(resultat.next()){ model.addrow (new object[] {resultat.getstring("nom_outil"),resultat.getstring("fabricant_outil"),resultat.getstring("annee_fabrication"),resultat.getstring("prix_outil")}); } } catch(sqlexception ex){ system.out.println(ex); } tableoutil.setmodel(model); } private void jbutton1actionperformed(java.awt.event.actionevent evt) { string sql = "delete `outil` nom_outil='outil b'"; try{ stmt=maconnexion.obtenirconnexion().createstatement(); stmt.executeupdate(sql); } catch(sqlexception ex){ system.err.println(ex); } } private void jbutton2actionperformed(java.awt.event.actionevent evt) { int ligneselectionne = jtable.getselectedrow(); string outil = jtable.getvalueat(ligneselectionne, 0); stmt=maconnexion.obtenirconnexion().createstatement(); string wql = "update outil set nom_outil=outil a"; stmt.setstring(1, outil); stmt.executeupdate(wql); } public static void main(string args[]) { try { (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) { if ("nimbus".equals(info.getname())) { javax.swing.uimanager.setlookandfeel(info.getclassname()); break; } } } catch (classnotfoundexception ex) { java.util.logging.logger.getlogger(fenetrelisteoutil.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (instantiationexception ex) { java.util.logging.logger.getlogger(fenetrelisteoutil.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (illegalaccessexception ex) { java.util.logging.logger.getlogger(fenetrelisteoutil.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(fenetrelisteoutil.class.getname()).log(java.util.logging.level.severe, null, ex); } java.awt.eventqueue.invokelater(new runnable() { public void run() { new fenetrelisteoutil().setvisible(true); } }); } // variables declaration - not modify private javax.swing.jtable tableoutil; private javax.swing.jbutton jbutton1; private javax.swing.jbutton jbutton2; private javax.swing.jlabel jlabel1; private javax.swing.jscrollpane jscrollpane1;
i want know if there way without being obliged every time go rêquête change parameter. how make professionals in computing in application select line of table mouse press on button update , update right now.
my update code has errors. errors are:
setstring cannot find symbol, non static method getselectedrow() can not referenced static context, non static method getvalue() can not referenced static context.
my new code after tips:
private void jbutton2actionperformed(java.awt.event.actionevent evt) { // todo add handling code here: int ligneselectionne = tableoutil.getselectedrow(); string outil = tableoutil.getvalueat(ligneselectionne, 0); preparedstatement stmt = null; string wql = "update outil set nom_outil = ? nom_outil = ?"; try { maconnexion.setautocommit(false); stmt = maconnexion.preparestatement(wql); stmt.setstring(1, "outil a"); stmt.setstring(2, "outil avant"); stmt.executeupdate(); maconnexion.commit(); } catch (sqlexception e ) { jdbctutorialutilities.printsqlexception(e); if (maconnexion != null) { try { system.err.print("transaction being rolled back"); maconnexion.rollback(); } catch(sqlexception excep) { jdbctutorialutilities.printsqlexception(excep); } } } { if (stmt != null) { stmt.close(); } maconnexion.setautocommit(true); } }
setautocommit, commit, jdbctutorialutilities, rollback cannot find symbol.
i have difficult apply answer because novice in sql , java use video make application https://www.youtube.com/watch?v=fs7dikroacg
variable names should not start upper case character. "tableoutil" should "tableoutil". notice how variable highlighted differently in posted code. because upper case characters used "class names".
int ligneselectionne = jtable.getselectedrow(); string outil = jtable.getvalueat(ligneselectionne, 0);
you referencing "jtable" class. want reference instance of class created:
int ligneselectionne = tableoutil.getselectedrow(); string outil = tableoutil.getvalueat(ligneselectionne, 0);
Comments
Post a Comment