bonsoir j'ai essaié de réutiliser en une seule foi l'etablissement d'une connexion dans dans ma class Frm_produit mè ca beug aide moi au niveau de la variable conn introuvable
voici mon code:
package produit;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//import java.sql.Connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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;
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 JButton jlast = new JButton();
private JButton jprecedent = new JButton();
private JButton jsuivant = new JButton();
private JButton jfirst = new JButton();
private JLabel jLabel1 = new JLabel();
private JLabel jLabel2 = new JLabel();
private JLabel jLabel3 = new JLabel();
private JTextField jref = new JTextField();
private JTextField jdesigne = new JTextField();
private JTextField jprix = new JTextField();
public Frm_produit() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.getContentPane().setLayout( null );
this.setSize(new Dimension(765, 406));
this.setTitle( "GESTION DES PRODUIT" );
this.setFont(new Font("Tahoma", 1, 11));
jajouter.setText("AJOUTER");
jajouter.setBounds(new Rectangle(10, 345, 115, 30));
jajouter.setFont(new Font("Tahoma", 1, 11));
jajouter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jajouter_actionPerformed(e);
}
});
jenregistrer.setText("ENREGISTRER");
jenregistrer.setBounds(new Rectangle(135, 345, 105, 30));
jenregistrer.setFont(new Font("Tahoma", 1, 11));
jenregistrer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jenregistrer_actionPerformed(e);
}
});
jsupprimer.setText("SUPPRIMER");
jsupprimer.setBounds(new Rectangle(245, 345, 115, 30));
jsupprimer.setFont(new Font("Tahoma", 1, 11));
jsupprimer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jsupprimer_actionPerformed(e);
}
});
jafficher.setText("AFFICHER");
jafficher.setBounds(new Rectangle(370, 345, 100, 30));
jafficher.setFont(new Font("Tahoma", 1, 11));
jafficher.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jafficher_actionPerformed(e);
}
});
jfermer.setText("FERMER");
jfermer.setBounds(new Rectangle(675, 350, 85, 25));
jfermer.setFont(new Font("Tahoma", 1, 11));
jlast.setText("");
jfirst.setBounds(new Rectangle(605, 345, 55, 30));
jfirst.setFont(new Font("Tahoma", 1, 11));
jLabel1.setText("REF");
jLabel1.setBounds(new Rectangle(15, 10, 40, 35));
jLabel1.setFont(new Font("Tahoma", 1, 18));
jLabel2.setText("DESIGNATION");
jLabel2.setBounds(new Rectangle(5, 80, 155, 40));
jLabel2.setFont(new Font("Tahoma", 1, 18));
jLabel3.setText("PRIX");
jLabel3.setBounds(new Rectangle(5, 150, 150, 40));
jLabel3.setFont(new Font("Tahoma", 1, 18));
jref.setBounds(new Rectangle(140, 10, 135, 25));
jdesigne.setBounds(new Rectangle(140, 85, 310, 30));
jprix.setBounds(new Rectangle(140, 150, 135, 25));
jprix.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jprix_actionPerformed(e);
}
});
this.getContentPane().add(jprix, null);
this.getContentPane().add(jdesigne, null);
this.getContentPane().add(jref, null);
this.getContentPane().add(jLabel3, null);
this.getContentPane().add(jLabel2, null);
this.getContentPane().add(jLabel1, null);
this.getContentPane().add(jfirst, null);
this.getContentPane().add(jsuivant, null);
this.getContentPane().add(jprecedent, null);
this.getContentPane().add(jlast, null);
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;
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql:/localhost/bd_produit?";
Class.forName("org.gjt.mm.mysql.Driver");
conn=DriverManager.getConnection(url);
}
public void jajouter(String jref,String jdesigne,double jprix) throws SQLException, ClassNotFoundException {
//ResultSet rs=null;
int resultat;
Statement st=null;
if(conn==null){
try{
String requete="insert into produit(ref,designation,prix) values('"+jref+"','"+jdesigne+"',"+jprix+")";
st=conn.createStatement();
resultat=st.executeUpdate(requete);
}
catch(SQLException ex){
JOptionPane.showInternalConfirmDialog(null,"sql incorrecte","Erreur",JOptionPane.ERROR_MESSAGE);
}
finally{
if(st!=null){
try{
st.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
/*if(rs!=null){
try{
rs.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}*/
if(conn!=null){
try{
conn.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
}
}
}
public void jafficher()throws Exception{
//String requete = "select* from produit";
ResultSet rs=null;
Statement st=null;
if(conn==null){
try{
String requete = "select* from produit";
st =conn.createStatement();
rs = st.executeQuery(requete);
rs.first();
while(rs.next()){
System.out.println(rs.getString("jref") +" "+rs.getString("designation")+" "+rs.getString("jprix"));
}
//rs.close();
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null,"anomalie lors de l'execution de la requete","Erreur",JOptionPane.ERROR_MESSAGE);
}
finally{
if(st!=null){
try{
st.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
if(rs!=null){
try{
rs.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
if(conn!=null){
try{
conn.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
}
}
}
public void jsupprimer(String jref)throws Exception{
//if(JOptionPane.showConfirmDialog(this, "voulez vous supprimer cet enregistrement", "Supprimer", 0)==0){
//ResultSet rs=null;
int resultat;
Statement st=null;
if(conn==null){
try{
String requete="delete from produit where ref='"+jref+"' ";
st =conn.createStatement();
resultat = st.executeUpdate(requete);
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null,"anomalie lors de l'execution de la requete","Erreur",JOptionPane.ERROR_MESSAGE);
}
finally{
if(st!=null){
try{
st.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
/*if(rs!=null){
try{
rs.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}*/
if(conn!=null){
try{
conn.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
}
}
//}
}
public void jprecedent()throws Exception{
ResultSet rs=null;
Statement st=null;
if(conn==null){
try{
String requete="select * from produit";
st= conn.createStatement();
rs=st.executeQuery(requete);
rs.afterLast();
while(rs.previous()){
System.out.println(rs.getString("ref") +""+rs.getString("designation")+""+rs.getString("prix"));
}
rs.close();
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null,"anomalie lors de l'execution de la requete","Erreur",JOptionPane.ERROR_MESSAGE);
}
finally{
if(st!=null){
try{
st.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
if(rs!=null){
try{
rs.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
if(conn!=null){
try{
conn.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
}
}
}
public void jsuivant()throws Exception{
ResultSet rs=null;
Statement st=null;
if(conn==null){
try{
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.getString("prix"));
}
//rs.close();
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null,"anomalie lors de l'execution de la requete","Erreur",JOptionPane.ERROR_MESSAGE);
}
finally{
if(st!=null){
try{
st.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
if(rs!=null){
try{
rs.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
if(conn!=null){
try{
conn.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
}
}
}
public void jfirst()throws Exception{
ResultSet rs=null;
Statement st=null;
if(conn==null){
try{
String requete="select* from produit";
st=conn.createStatement();
rs=st.executeQuery(requete);
while(rs.first()){
System.out.println(rs.getString("ref") +""+rs.getString("designation")+""+rs.getString("prix"));
}
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null,"anomalie lors de l'execution de la requete","Erreur",JOptionPane.ERROR_MESSAGE);
}
finally{
if(st!=null){
try{
st.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
if(rs!=null){
try{
rs.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
if(conn!=null){
try{
conn.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
}
}
}
public void jlast()throws Exception{
ResultSet rs=null;
Statement st=null;
if(conn==null){
try{
String requete="select* from produit";
st=conn.createStatement();
rs=st.executeQuery(requete);
while(rs.last()){
System.out.println(rs.getString("ref") +""+rs.getString("designation")+""+rs.getString("prix"));
}
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null,"anomalie lors de l'execution de la requete","Erreur",JOptionPane.ERROR_MESSAGE);
}
finally{
if(st!=null){
try{
st.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
if(rs!=null){
try{
rs.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
if(conn!=null){
try{
conn.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
}
}
}
/*public jdesigne(String d){
d=jdesigne.getText();
}
public(String r){
r=jref.getText();
}*/
public static void main(String[] arg){
EventQueue.invokeLater(new Runnable(){
public void run(){
new Frm_produit().setVisible(true);
}
});
}
private void jlast_actionPerformed(ActionEvent e) {
}
private void jenregistrer_actionPerformed(ActionEvent e) {
if(JOptionPane.showConfirmDialog(this,"voulez-vous enregistrer cet produit?","Enrégistrement du produit",0)==0)
try{
jajouter(jref.getText(),jref.getText(),Double.parseDouble(jprix.getText()));
}
catch(Exception ex){
Logger.getLogger(Frm_produit.class.getName()).log(Level.SEVERE,null,ex);
}
}
private void jprix_actionPerformed(ActionEvent e) {
try{
Double.parseDouble(jprix.getText());
// System.out.println();
}
catch(NumberFormatException ne){
JOptionPane.showMessageDialog(null, "Veuillez saisir des chiffres", "saisir chiffre", JOptionPane.ERROR_MESSAGE);
}
}
private void jajouter_actionPerformed(ActionEvent e) {
}
private void jsupprimer_actionPerformed(ActionEvent e) {
}
private void jafficher_actionPerformed(ActionEvent e) {
}
}
bigboss
Afficher la suite