Comment écrire dans ma base depuis mon écran de saisie

cs_redbar Messages postés 112 Date d'inscription mardi 26 janvier 2010 Statut Membre Dernière intervention 26 mai 2014 - 28 sept. 2010 à 12:20
cs_redbar Messages postés 112 Date d'inscription mardi 26 janvier 2010 Statut Membre Dernière intervention 26 mai 2014 - 29 sept. 2010 à 15:41
Bonjour tout le monde
Je suis en train d'écrire un logiciel de gestion de stock avec eclipse galileo et j'utilise le SGBD Postgrsql 8.
Mon problème est comment utiliser ma classe de connection pour me connecter et écrire dans ma base de données depuis les écran de saisie.
Aidez moi je vous en prie car je suis débutant et je veux apprendre JAVA pour plusieurs raisons.
Merci à tous.
je serai heureux de répondre à vos question pour informations complementaire

2 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
28 sept. 2010 à 18:52
Tu as quoi dans ta classe de connexion ?

Normalement, il faut créer une nouvelle connexion, puis l'ouvrir, executer une requete avec executeQuery ou executeUpdate et fermet la connexion.

Je serai plus précis si tu me ajoute ta classe connexion...
0
cs_redbar Messages postés 112 Date d'inscription mardi 26 janvier 2010 Statut Membre Dernière intervention 26 mai 2014
29 sept. 2010 à 15:41
Bonjour
voici ma classe connexion

package mti;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JOptionPane;

public class Connexion {
private static String url,bd="midas";
private static String user="postgres";
private static String password="postgres";
private static Connection connect;

public Connexion(String bd, String user, String password){
// TODO Auto-generated constructor stub
this.bd = bd;
this.user = user;
this.password = password;
}

/**
* return
*/
public static Connection getInstance(){

url = "jdbc:postgresql://localhost:5432/"+bd;
if(connect == null){
try{
connect= DriverManager.getConnection(url, user, password);

}catch(SQLException e){
JOptionPane.showMessageDialog(null, e.getMessage(), "ERREUR DE CONNEXION ! - MIDAS TOUCH INC. ",
JOptionPane.ERROR_MESSAGE);
}
}
return connect;
}

}


Aussi je vais ajouter un de mes ecran de saisi c'est la saisie des produits


package mti;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.Border;

public class Produit extends JDialog {
private JLabel labelRef,nomLab, prixLab, qteLab,catLab,vid1,vid2,vid3, titrelab;
private JTextField txtRef, txtNom, txtPrix, txtQte;

private JComboBox catBox = new JComboBox();
private JButton btnNouveau = new JButton("Nouveau");
private JButton btnModifier = new JButton("Modifier");
private JButton btnActualiser = new JButton("Actualiser");
private JButton btnSupprimer = new JButton("Supprimer");

private JSplitPane split;
//exemple pour lister les produits et permettre une vue sur la table produit
//pas encore fonctionnel
String title[] = {"Référence ", "Nom du Produit", "Prix Unitaire","Quantité en stock"};
private Object[][] data = { {"MT-OHP01", "Odinateur HP P4","450000","3"},
{"MT-CMMSD1", "Carte mémoire microSD 1G","5000","120"},
{"MT-CM915V1","Carte mère PGA 915GV","33000","35"},
{"MT-CIMP1.5","Cable imprimante 1,5m","1500","34"}
};
private JTable tablep = new JTable(data, title);



public Produit(){
this.setTitle("Produits");
this.setModal(true);
this.setSize(750,450);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setResizable(false);
initComponent();
this.setVisible(true);
}

private void initComponent() {
// TODO Auto-generated method stub
JPanel pane1 = new JPanel();
pane1.setPreferredSize(new Dimension(600,70));
pane1.setBackground(Color.WHITE);

//Titre de la fenêtre du produit
titrelab = new JLabel("PRODUITS");
titrelab.setPreferredSize(new Dimension(250,70));
titrelab.setFont(new Font("comic sans MS",30,38));
titrelab.setForeground(Color.BLUE);
titrelab.setAlignmentX(JLabel.CENTER);
pane1.add(titrelab);

JPanel pane2 = new JPanel();
pane2.setPreferredSize(new Dimension(380,80));
pane2.setBackground(Color.WHITE);

Border[] listBorder = {
BorderFactory.createEtchedBorder(Color.green, Color.GRAY),
BorderFactory.createLineBorder(Color.green),
BorderFactory.createLineBorder(Color.orange),
BorderFactory.createLineBorder(Color.white),
BorderFactory.createMatteBorder(5, 2, 5, 2, Color.black),
BorderFactory.createRaisedBevelBorder(),
BorderFactory.createTitledBorder("Titre")


};

//Reference du produit
JPanel pane2_1 = new JPanel(new BorderLayout());
pane2_1.setPreferredSize(new Dimension(350,25));
pane2_1.setBackground(Color.WHITE);
pane2_1.setBorder(listBorder[3]);
labelRef = new JLabel("Référence : ");
labelRef.setFont(new Font("comic sans MS",10,15));
labelRef.setForeground(Color.BLUE);
labelRef.setPreferredSize(new Dimension(100,25));
txtRef = new JTextField();
txtRef.setPreferredSize(new Dimension(60,20));
txtRef.setFont(new Font("",1,14));
JLabel vide1= new JLabel(" ");
vide1.setPreferredSize(new Dimension(100,25));
pane2_1.add(labelRef,BorderLayout.WEST);
pane2_1.add(txtRef,BorderLayout.CENTER);
pane2_1.add(vide1,BorderLayout.EAST);
pane2.add(pane2_1);

//Nom du produit
JPanel pane2_2 = new JPanel(new BorderLayout());
pane2_2.setPreferredSize(new Dimension(350,25));
pane2_2.setBackground(Color.WHITE);
pane2_2.setBorder(listBorder[3]);
nomLab = new JLabel("Nom : ");
nomLab.setFont(new Font("comic sans MS",10,15));
nomLab.setForeground(Color.BLUE);
nomLab.setPreferredSize(new Dimension(100,25));
txtNom = new JTextField();
txtNom.setPreferredSize(new Dimension(210,25));
txtNom.setFont(new Font("",1,13));
pane2_2.add(nomLab,BorderLayout.WEST);
pane2_2.add(txtNom,BorderLayout.CENTER);
pane2.add(pane2_2);

//Prix du produit
JPanel pane2_3 = new JPanel(new BorderLayout());
pane2_3.setPreferredSize(new Dimension(350,25));
pane2_3.setBackground(Color.WHITE);
pane2_3.setBorder(listBorder[3]);
prixLab =new JLabel("Prix Unitaire :");
prixLab.setFont(new Font("comic sans MS",10,15));
prixLab.setForeground(Color.BLUE);
prixLab.setPreferredSize(new Dimension(100,25));
txtPrix = new JTextField();
txtPrix.setPreferredSize(new Dimension(60,25));
txtPrix.setFont(new Font("",1,14));
JLabel vide3 = new JLabel(" F CFA ");
vide3.setFont(new Font("comic sans MS",10,15));
vide3.setForeground(Color.BLUE);
vide3.setPreferredSize(new Dimension(120,25));
pane2_3.add(prixLab,BorderLayout.WEST);
pane2_3.add(txtPrix,BorderLayout.CENTER);
pane2_3.add(vide3,BorderLayout.EAST);
pane2.add(pane2_3);


JPanel pane3 = new JPanel();
pane3.setPreferredSize(new Dimension(380,80));
pane3.setBackground(Color.WHITE);

//Quantité en stock
JPanel pane31 = new JPanel(new BorderLayout());
pane31.setPreferredSize(new Dimension(350,25));
pane31.setBackground(Color.WHITE);
pane31.setBorder(listBorder[3]);
qteLab = new JLabel("Quantité en stock : ");
qteLab.setFont( new Font("comic sans MS",10,15));
qteLab.setForeground(Color.BLUE);
txtQte = new JTextField();
txtQte.setPreferredSize(new Dimension(70,25));
txtQte.setFont(new Font("",1,14));
JLabel vide5 = new JLabel(" ");
vide5.setPreferredSize(new Dimension(110,25));
pane31.add(qteLab, BorderLayout.WEST);
pane31.add(txtQte,BorderLayout.CENTER);
pane31.add(vide5, BorderLayout.EAST);
pane3.add(pane31);


//Categorie du produit
JPanel pane33= new JPanel(new BorderLayout());
pane33.setPreferredSize(new Dimension(350,25));
pane33.setBackground(Color.WHITE);
pane33.setBorder(listBorder[3]);
catLab = new JLabel("Catégorie du produit : ");
catLab.setFont( new Font("comic sans MS",10,15));
catLab.setForeground(Color.BLUE);
catBox.setPreferredSize(new Dimension(150,25));
catBox.setFont(new Font("",1,13));
pane33.add(catLab,BorderLayout.WEST);
pane33.add(catBox,BorderLayout.CENTER);
pane3.add(pane33);

JPanel pane4 = new JPanel();
pane4.setPreferredSize(new Dimension(700,240));
pane4.setBackground(Color.WHITE);

//Premier séparateur
JPanel separ1 = new JPanel();
separ1.setPreferredSize(new Dimension(750,10));
separ1.setBackground(Color.WHITE);
JSeparator sep1 = new JSeparator();
sep1.setPreferredSize(new Dimension(750,2));
sep1.setBorder(BorderFactory.createRaisedBevelBorder());
separ1.add(sep1);


JPanel panebouton = new JPanel(new FlowLayout());
panebouton.setPreferredSize(new Dimension(700,40));
panebouton.setBackground(Color.WHITE);
//panebouton.setBorder(BorderFactory.createLineBorder(Color.BLUE));


btnNouveau.setPreferredSize(new Dimension(130,35));
btnNouveau.setFont(new Font("comic sans MS",0,13));
btnNouveau.setForeground(Color.BLUE);
btnNouveau.setIcon(new ImageIcon("imag/ajout.JPG"));
btnModifier.setPreferredSize(new Dimension(130,35));
btnModifier.setFont(new Font("comic sans MS",0,13));
btnModifier.setForeground(Color.BLUE);
btnActualiser.setPreferredSize(new Dimension(130,35));
btnActualiser.setFont(new Font("comic sans MS",0,13));
btnActualiser.setForeground(Color.BLUE);
btnSupprimer.setPreferredSize(new Dimension(130,35));
btnSupprimer.setFont(new Font("comic sans MS",0,13));
btnSupprimer.setForeground(Color.BLUE);
vid1 = new JLabel(" ");
vid2 = new JLabel(" ");
vid3 = new JLabel(" ");

panebouton.add(btnNouveau);
panebouton.add(vid1);
panebouton.add(btnModifier);
panebouton.add(vid2);
panebouton.add(btnActualiser);
panebouton.add(vid3);
panebouton.add(btnSupprimer);

btnSupprimer.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showConfirmDialog(null, "Voulez-vous supprimer ce produit définitivement du stock?",
"Gestion de stock - MIDAS TOUCH INC.",JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);

}
});

JPanel panesepare = new JPanel();
panesepare.setPreferredSize(new Dimension(750,10));
panesepare.setBackground(Color.WHITE);

JSeparator sep2 = new JSeparator();
sep2.setPreferredSize(new Dimension(750,2));
sep2.setBorder(BorderFactory.createRaisedBevelBorder());
panesepare.add(sep2);

JPanel panetable = new JPanel(new BorderLayout());
panetable.setPreferredSize(new Dimension(720,200));
panetable.add(new JScrollPane(tablep),BorderLayout.CENTER);
tablep.setRowHeight(20);
tablep.setFont(new Font("comic sans ms",0,13));


JPanel paneinfo = new JPanel();
paneinfo.setBackground(Color.WHITE);
paneinfo.setPreferredSize(new Dimension(700,80));
JLabel labinfo = new JLabel("Il y a xx produits en stock.");
labinfo.setPreferredSize(new Dimension(350,40));
labinfo.setFont(new Font("comic sans ms",0,15));
labinfo.setForeground(Color.BLUE);
paneinfo.add(labinfo);
panetable.add(paneinfo,BorderLayout.SOUTH);

pane4.add(separ1);
pane4.add(panebouton);
pane4.add(panesepare);
pane4.add(panetable);


this.getContentPane().add(pane1, BorderLayout.NORTH);
this.getContentPane().add(pane2, BorderLayout.CENTER);
this.getContentPane().add(pane3, BorderLayout.EAST);
this.getContentPane().add(pane4, BorderLayout.SOUTH);
}



}
0
Rejoignez-nous