Catch(ClassNotFoundException)

dmo83 Messages postés 79 Date d'inscription vendredi 19 août 2011 Statut Membre Dernière intervention 11 octobre 2011 - 12 sept. 2011 à 13:44
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 - 17 sept. 2011 à 10:33
bonjours! je sollicite votre aide sur ce ki ressort lors de la compilation de mon programme
voici le code:

private void afficher_actionPerformed(ActionEvent e) {
Connection conn = null;
ResultSet rs = null; Statement st=null;
try{
// Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql:/127.0.0.1/BD_PRODUIT?user=root" ;

//class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection(url);

String requete = "select* from PRODUIT";
st =conn.createStatement();
rs = st.executeQuery(requete);

while(rs.next()){

System.out.println(rs.getString("ref")+" "+rs.getString("designation")+" "+rs.getDouble("prix") );

}

}

catch(ClassNotFoundException ex){
//System.out.println("MAUVAIS PILOTE");
JOptionPane.showMessageDialog(null,"MAUVAIS PILOTE","Erreur",JOptionPane.ERROR_MESSAGE);
}



catch(SQLException se){
//System.out.println("SQL INCORRECTE");
JOptionPane.showMessageDialog(null,"sql incorrecte","Erreur",JOptionPane.ERROR_MESSAGE);

}

finally{
if(st!=null){
try{
st.close();
}
catch(SQLException el){
el.printStackTrace();
}
}

if(rs!=null){
try{
rs.close();
}
catch(SQLException se){
se.printStackTrace();
}
}

if(conn!=null){
try{
conn.close();
}
catch(SQLException se){
se.printStackTrace();
}
}
}


}


l'erreur afficher:
Error(201,9): exception java.lang.ClassNotFoundException is never thrown in body of corresponding try statement
bigboss

13 réponses

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
12 sept. 2011 à 13:50
Bonjour,

Enlèves le catch(ClassNotfoundException e) et ca marchera. En gros, tu catch une exception qui ne peut pas être levée.
0
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
12 sept. 2011 à 13:57
Si tu as d'autres soucis avec ce code n'ouvre pas de nouveau sujet, continue ici et je t'aiderai.
0
dmo83 Messages postés 79 Date d'inscription vendredi 19 août 2011 Statut Membre Dernière intervention 11 octobre 2011
12 sept. 2011 à 15:20
ca execute maintenant mais lorsque je click sur le bouton ajouter mes champs de saisi ne s'initialise mon code est pauvre u peu m'aidé
public void jajouter(String ref,String designation,double prix) throws Exception{
Connection conn = null;

Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql:/127.0.0.1/BD_PRODUIT?user=root" ;

//class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection(url);
if (conn == null)

return ;
String requete = "insert into PRODUIT values("+ref+","+designation+",prix)";
Statement st=conn.createStatement();
st.executeUpdate(requete);
st.close();
conn.close();
}










private void ajouter_actionPerformed(ActionEvent e) {

}
bigboss
0
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
12 sept. 2011 à 15:51
J'ai un peu arrangé ton code mais, si ton build path n'est pas configuré, tu n'as aucune chance de t'en sortir.

Evites les throws ou catch avec Exception, et n'oublie pas les ' devant les attributs VARCHAR de ta base de données

public void jajouter(String ref,String designation,double prix) {
   Connection conn = null;
   Class.forName("com.mysql.jdbc.Driver");
   String url = "jdbc:mysql:/127.0.0.1/BD_PRODUIT?user=root" ;
   //class.forName("org.gjt.mm.mysql.Driver");
   conn = DriverManager.getConnection(url);
   if (conn != null){
      try{
         String requete = "insert into PRODUIT values('"+ref+"','"+designation+"',prix)";
         Statement st=conn.createStatement();
         st.executeUpdate(requete);
      }
      catch(SQLException e){
         e.printStackTrace();
      }
      finally{
         try{
            st.close();
            conn.close();
         }
         catch(SQLException e){
            e.printStackTrace();
         }
      }
   }
}

private void ajouter_actionPerformed(ActionEvent e) {

}
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
dmo83 Messages postés 79 Date d'inscription vendredi 19 août 2011 Statut Membre Dernière intervention 11 octobre 2011
12 sept. 2011 à 16:47
le build path permet il d'initialisé mes zones de saisie ie que quand je clik sur le bouton ajouter mes zones de saisies se vide et je peux saisir un produit

bigboss
0
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
12 sept. 2011 à 17:08
Si tu configures ton build path, tu pourras accéder à la base de données, si tu ne le fais pas, tu n'as aucune chance de faire fonctionner ton programme.
0
dmo83 Messages postés 79 Date d'inscription vendredi 19 août 2011 Statut Membre Dernière intervention 11 octobre 2011
12 sept. 2011 à 17:21
donne mw le chemin qui me permetra de configurer le build path


bigboss
0
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
13 sept. 2011 à 08:27
Click droit sur ton projet -> Build path (ou chemin de génération en français) puis coufigure (ou configurer).

Puis tu choisis l'onglet Libraries (ou bibliothèques) et add externa jar (ajouter des fichiers jars externes) et tu choisis la librairie obdc (ou équivalent) que tu as préalablement téléchargé sur internet.

Je t'ai déjà expliqué ca sur un autre sujet
0
dmo83 Messages postés 79 Date d'inscription vendredi 19 août 2011 Statut Membre Dernière intervention 11 octobre 2011
14 sept. 2011 à 11:29
bonjours! le build path je les deja fè a moins ke ca été mal fè parcek quand j'execute et k j clic sur le bouton ajouter les zones des saisies ne se vide pas ie c k lè données precedantes demeur toujours hors j devrais avoir mes zones de texte vide pour saisir de nouveau .
pour le build path voici comment jè procedé:
projet->propriéte projet->librairie/classpath->add jar/directory...->mysql-connector-java-5.1.6-bin.jar
penses tu penses k ces ma configuration qui n'est pas bonne?
voici mon code:
//ajouter un nouveau produit
public void jajouter(String ref,String designation,double prix) {
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql:/127.0.0.1/BD_PRODUIT?user=root" ;
//class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection(url);
if (conn != null){
try{
String requete = "insert into PRODUIT values('"+ref+"','"+designation+"',prix)";
Statement st=conn.createStatement();
st.executeUpdate(requete);
}
catch(SQLException e){
e.printStackTrace();
}
finally{
try{
st.close();
conn.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
}
}

private void ajouter_actionPerformed(ActionEvent e) {

}
bigboss
0
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
14 sept. 2011 à 13:27
Alors si tu as ajouté la librairie dans le classpath, c'est bon de ce coté là.

Quelle est ton erreur maintenant ?
0
dmo83 Messages postés 79 Date d'inscription vendredi 19 août 2011 Statut Membre Dernière intervention 11 octobre 2011
14 sept. 2011 à 14:42
ya pas de bug l'execution marche sauf que le bouton spprimer est en commentaire.
j'arrive a afficher la fenêtre mè les actions sur mes boutons(ajouter,supprimer,afficher)qui ne fonctionnent pas
voici le code:

//ajouter un produit
public void jajouter(String ref,String designation,double prix) throws SQLException, ClassNotFoundException {
Connection conn=null;
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql:/127.0.0.1/BD_PRODUIT?user=root";
// Class.forName("org.gjt.mm.mysql.Driver");
conn=DriverManager.getConnection(url);

if(conn==null){
try{
String requete="insert into produit values('"+ref+"','"+designation+"',prix)";
Statement st=conn.createStatement();
st.executeUpdate(requete);
}
catch(SQLException ex){
ex.printStackTrace();
}
finally{
try{

conn.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
}


}






//affiche tous les produits enrégistrés dans la table produit
private void afficher_actionPerformed(ActionEvent e) {
Connection conn = null;
ResultSet rs = null; Statement st=null;
try{
// Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql:/127.0.0.1/BD_PRODUIT?user=root" ;

//class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection(url);

String requete = "select* from produit";
st =conn.createStatement();
rs = st.executeQuery(requete);

while(rs.next()){
String r=rs.getString("ref");
String d=rs.getString("designation");
double p=rs.getDouble("prix");



}
System.out.println(rs.getString("ref")+" "+rs.getString("designation")+" "+rs.getDouble("prix") );
}

/*catch(ClassNotFoundException ex){
//System.out.println("MAUVAIS PILOTE");
JOptionPane.showMessageDialog(null,"MAUVAIS PILOTE","Erreur",JOptionPane.ERROR_MESSAGE);
}*/



catch(SQLException se){
//System.out.println("SQL INCORRECTE");
JOptionPane.showMessageDialog(null,"sql incorrecte","Erreur",JOptionPane.ERROR_MESSAGE);

}

finally{
if(st!=null){
try{
st.close();
}
catch(SQLException el){
el.printStackTrace();
}
}

if(rs!=null){
try{
rs.close();
}
catch(SQLException se){
se.printStackTrace();
}
}

if(conn!=null){
try{
conn.close();
}
catch(SQLException se){
se.printStackTrace();
}
}
}


}
// supprimer le produit indésiré dans votre bd dans la table produit
/*private void supprimer_actionPerformed(ActionEvent e) {


//connection a la base de données
Connection conn = null;

Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql:/127.0.0.1/BD_PRODUIT?user=root" ;

//class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection(url);
if (conn == null){
if(JoptionPane.showConfirmDialog(this,"Voulez vous supprimer cet produit?","supprimer produit",0)==0)
try{
String requete = "delete from PRODUIT where ref=? and designation=? and prix=?";
Statement st=conn.createStatement(requete);
st.executeUpdate(requete);
}
catch(SQLException ex){
ex.PrintStrackTrace();
}
finally{
try{
st.close();
conn.close();
}
catch(SQLException ex){
ex.PrintStrackTrace();
}
}
}
}*/
bigboss
0
dmo83 Messages postés 79 Date d'inscription vendredi 19 août 2011 Statut Membre Dernière intervention 11 octobre 2011
16 sept. 2011 à 19:06
bonsoir, peux u m'aider pour mon code il ne m'affiche d'erreur mais il s'execute pas

package produit;

import java.awt.Dimension;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.List;
import java.awt.Rectangle;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//import java.io.*;
//import java.sql.*;
import java.sql.Array;
import java.sql.Connection;
import java.sql.DriverManager;
//import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.text.NumberFormat;

//import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
//import javax.swing.JOptionPane;



public class Frm_produit extends JFrame {
private JButton jajouter = new JButton();
private JButton jenregistrer = new JButton();
private JButton jsupprimer = new JButton();
private JButton jafficher = new JButton();
private JButton jfermer = new JButton();
private JTextField jref = new JTextField();
private JTextField jdesigne = new JTextField();
private JTextField jprix = new JTextField();
private JLabel jLabel1 = new JLabel();
private JLabel jLabel2 = new JLabel();
private JLabel jLabel3 = new JLabel();
private Object statement;
private Connection conn;
private JButton jprecedent = new JButton();
private JButton jsuivant = new JButton();
private JButton jfirst = new JButton();
private JButton jlast = new JButton();

public Frm_produit() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}

private void jbInit() throws Exception {
this.getContentPane().setLayout( null );
this.setSize(new Dimension(866, 381));
this.setTitle( "OPERATIONS SUR LES PRODUITS" );
jajouter.setText("AJOUTER");
jajouter.setBounds(new Rectangle(5, 270, 140, 25));
jajouter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ajouter_actionPerformed(e);
}
});
jenregistrer.setText("ENREGISTRER");
jenregistrer.setBounds(new Rectangle(150, 275, 125, 20));
jenregistrer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
enregistrer_actionPerformed(e);
}
});
jsupprimer.setText("SUPPRIMER");
jsupprimer.setBounds(new Rectangle(280, 275, 125, 20));
jsupprimer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
supprimer_actionPerformed(e);
}
});
jafficher.setText("AFFICHER");
jafficher.setBounds(new Rectangle(410, 275, 125, 20));
jafficher.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
afficher_actionPerformed(e);

}
});
jfermer.setText("FERMER");
jfermer.setBounds(new Rectangle(535, 275, 105, 25));
jfermer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fermer_actionPerformed(e);
}
});
jref.setBounds(new Rectangle(165, 20, 170, 20));
jref.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ref_actionPerformed(e);
}
});
jdesigne.setBounds(new Rectangle(160, 75, 350, 20));
jdesigne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
designation_actionPerformed(e);
}
});
jprix.setBounds(new Rectangle(160, 130, 180, 20));
jprix.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
prix_actionPerformed(e);

}
});
jLabel1.setText("REFERENCE");
jLabel1.setBounds(new Rectangle(5, 20, 140, 20));
jLabel1.setFont(new Font("Tahoma", 1, 18));
jLabel2.setText("DESIGNATION");
jLabel2.setBounds(new Rectangle(5, 75, 150, 20));
jLabel2.setFont(new Font("Tahoma", 1, 18));
jLabel3.setText("PRIX");
jLabel3.setBounds(new Rectangle(10, 135, 60, 15));
jLabel3.setFont(new Font("Tahoma", 1, 18));
jprecedent.setText("precedent");
jprecedent.setBounds(new Rectangle(110, 325, 115, 25));
jsuivant.setText("next");
jsuivant.setBounds(new Rectangle(230, 325, 60, 25));
jfirst.setText("FIRST");
jfirst.setBounds(new Rectangle(35, 330, 65, 20));
jlast.setText("LAST");
jlast.setBounds(new Rectangle(300, 330, 70, 20));
this.getContentPane().add(jlast, null);
this.getContentPane().add(jfirst, null);
this.getContentPane().add(jsuivant, null);
this.getContentPane().add(jprecedent, null);
this.getContentPane().add(jLabel3, null);
this.getContentPane().add(jLabel2, null);
this.getContentPane().add(jLabel1, null);
this.getContentPane().add(jprix, null);
this.getContentPane().add(jdesigne, null);
this.getContentPane().add(jref, null);
//connexion base de données

this.getContentPane().add(jfermer, null);
this.getContentPane().add(jafficher, null);
this.getContentPane().add(jsupprimer, null);
this.getContentPane().add(jenregistrer, null);
this.getContentPane().add(jajouter, null);
Connection conn=null;
ResultSet rs=null;
Statement st=null;
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql:/127.0.0.1/BD_PRODUIT?user=root";
// Class.forName("org.gjt.mm.mysql.Driver");
conn=DriverManager.getConnection(url);


}




public void jajouter(String jref,String jdesigne,double jprix) throws SQLException, ClassNotFoundException {

if(conn==null){
try{

String requete="insert into produit(ref,designation,prix) values('"+jref+"','"+jdesigne+"',"+jprix+")";
st=conn.createStatement();
rs=st.executeUpdate(requete);
}
catch(SQLException ex){
st.close();
rs.close();
}
catch(Exception e){
e.printStackTrace();
}

}



}
affiche tous les enregistrements de la table produit
public void jafficher(){

if(conn==null)
try{
String requete = "select* from produit";
st =conn.createStatement();
rs = st.executeQuery(requete);

while(rs.next()){

System.out.println("ref:" +rs.getString("jref")+ "designation:"+rs.getString("jdesigne")+ "prix:"+rs.getDouble("jprix"));

}
rs.close();
//System.out.println(rs.getString("ref")+" "+rs.getString("designation")+" "+rs.getDouble("prix") );
}

catch(SQLException ex){
ex.printStackTrace();
}
} }

public void jsupprimer(String jref){

if(JOptionPane.showConfirmDialog(this, "voulez vous supprimer cet enregistrement", "Supprimer", 0)==0){
if(conn==null){
try{
String requete="delete from produit where ref='"+jref+"'";
st =conn.createStatement();
rs = st.executeUpdate(requete);
}
catch(SQLException ex){
ex.printStackTrace();
}

}

}
}
affiche l'enregistrement precedent
public void jprecedent(){

if(conn==null){
try{
String requete="select * from produit";
st= conn.createStatement();
rs=st.executeQuery();
//rs.afterLast();

while(rs.previous()){

System.out.println(rs.getString("jref") +""+rs.getString("jdesigne")+""+rs.getString("jprix"));
}
rs.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
}

affiche le l'enregistrement suivant
public void jsuivant(){

if(conn==null){
try{
String requete="select* from produit";
st=conn.createStatement();
rs=st.executeQuery(requete);
while(rs.next()){

System.out.println(rs.getString("jref") +""+rs.getString("jdesigne")+""+rs.getString("jprix"));

}
rs.close();
}
catch(SQLException ex){
ex.printStrackTrace();
}

}
}

////affiche le premier enregistrement
public void jfirst(){

if(conn==null){
try{
String requete="select* from produit";
st=conn.createStatement();
rs=st.executeQuery(requete);

while(rs.first()){
System.out.println(rs.getString("jref") +""+rs.getString("jdesigne")+""+rs.getString("jprix"));
}

}
catch(SQLException ex){
ex.printStrackTrace();
}

}
}
//affiche le dernier enregistrement
public void jlast(){

if(conn==null){
try{
String requete="select* from produit";
st=conn.createStatement();
rs=st.executeQuery(requete);

while(rs.last()){
System.out.println(rs.getString("jref") +""+rs.getString("jdesigne")+""+rs.getString("jprix"));
}

}
catch(SQLException ex){
ex.printStrackTrace();
}

}

}

private void ajouter_actionPerformed(ActionEvent e) {

}

private void enregistrer_actionPerformed(ActionEvent e) {
if(JOptionPane.showConfirmDialog(this,"voulez-vous enregistrer cet produit?","Enrégistrement du produit",0)==0)
try{
jajouter(jref.getText(),jdesigne.getText(),Double.parseDouble(jprix.getText()));
}

catch(Exception ex){
Logger.getLogger(Frm_produit.class.getName()).log(Level.SEVERE,null,ex);

}
}
private void afficher_actionPerformed(ActionEvent e) {


}

private void fermer_actionPerformed(ActionEvent e) {
System.exit(0);
}

private void ref_actionPerformed(ActionEvent e) {
}

private void designation_actionPerformed(ActionEvent e) {
}

//saisir des chiffre obligatoire
private void prix_actionPerformed(ActionEvent e) {
try{
double prix=Double.parseDouble(jprix.getText().trim());
System.out.println(jprix);
}
catch(NumberFormatException ne){
JOptionPane.showMessageDialog(null, "Veuillez saisir des chiffres", "saisir chiffre", JOptionPane.ERROR_MESSAGE);
}
}


public static void main(String[] arg){
EventQueue.invokeLater(new Runnable(){
public void run(){
new Frm_produit().setVisible(true);

}
});

}

//variable de déclarion

private void supprimer_actionPerformed(ActionEvent e) {
}

}



bigboss
0
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
17 sept. 2011 à 10:33
Si ca concerne un nouveau problème, ouvres un nouveau sujet, et explique ton problème plus précisément, sache que la plupart des personnes qui t'aideront (j'en fais partie) détecteront les erreur en lisant ton code et n'essayeront pas de l'exécuter. Donc, il faut que tu nous oriente vers la solution.

Essaye de mettre quelques sysout pour vérifier si ton code est bien exécuté.
0
Rejoignez-nous