System.err.println(e);n'est pas suffisant pour obtenir des informations sur l'erreur, il vaudrait mieux utiliser
e.printStackTrace();ou des Logger.
try { st=conn.obtenirConnexion().createStatement(); Rs=st.executeQuery("SELECT * FROM ..."); } catch(SQLException e) { System.err.println(e); } Rs.next();
public int getQtePd(String codePd) throws SQLException { try (Connection conn = obtenirConnexion()) { String sql = "SELECT QtePd FROM produits where CodePd = ?"; try (PreparedStatement st = conn.prepareStatement(sql)) { st.setString(1, codePd); try (ResultSet rs = st.executeQuery()) { if (!rs.next()) { throw new IllegalStateException("getQtePd has no row for " + codePd); } int qte = rs.getInt("QtePd"); if (rs.next()) { throw new IllegalStateException("getQtePd has more than 1 row for " + codePd); } return qte; } } } } public void setQtePd(String codePd, int qtePd) throws SQLException { try (Connection conn = obtenirConnexion()) { String sql = "UPDATE produits SET QtePd = ? WHERE CodePd = ?"; try (PreparedStatement st = conn.prepareStatement(sql)) { st.setInt(1, qtePd); st.setString(2, codePd); int counts = st.executeUpdate(); if (counts != 1) { throw new IllegalStateException("setQtePd has changed " + counts + " rows for " + codePd + " (1 was expected)"); } } } } private void enrFactActionPerformed(ActionEvent evt) { for (int i = 0; i < model2.getRowCount(); i++) { try { String codePd = model2.getValueAt(i, 0).toString(); txtCodePd.setText(codePd); String qteV = model2.getValueAt(i, 1).toString(); txtQteV.setText(qteV); int oldQte = getQtePd(codePd); int newQte = oldQte - Integer.parseInt(qteV); setQtePd(codePd, newQte); txtQteVTable.setText(Integer.toString(newQte)); } catch (SQLException | RuntimeException e) { Logger.getLogger(getClass().toString()).log(Level.SEVERE, "Can't enrFactActionPerformed: " + i, e); } } }
obtenirConnexion()c'était une méthode dans ton code...
try { st=conn.obtenirConnexion().createStatement(); Rs=st.executeQuery("SELECT * FROM produits where CodePd='"+txtCodePd.getText()+"'"); }
package MyFrames;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Connecter {
//creation de la variable con de type Connection
Connection con;
public Connecter() throws InstantiationException, IllegalAccessException, SQLException {
try{
// chargement de la classe par son nom
Class c = Class.forName("com.mysql.jdbc.Driver") ;
Driver pilote = (Driver)c.newInstance() ;
// enregistrement du pilote auprès du DriverManager
DriverManager.registerDriver(pilote);
System.out.println("Driver jdbc chargée");
}catch(ClassNotFoundException e){System.err.println(e);}
try{
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/gestcom","root","");
System.out.println("Connection établie");
}catch(SQLException e){System.err.println(e);
System.out.println("SQLException: " + e.getMessage());
System.out.println("SQLState: " + e.getSQLState());
System.out.println("VendorError: " + e.getErrorCode());}
}
Connection obtenirConnexion(){return con;}
}
public class GestComDatasource { static { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { throw new IllegalStateException("Can't load MySQL Driver", e); } } public static Connection getConnection() throws SQLException { return DriverManager.getConnection("jdbc:mysql://localhost:3306/gestcom", "root", ""); } }
public int getQtePd(String codePd) throws SQLException { try (Connection conn = GestComDatasource.getConnection()) {
GRAVE: Can't enrFactActionPerformed: 3 java.lang.IllegalStateException: getQtePd has no row for