comment récuperer ma variable
Bonjour tout le monde!
j' ai cree une fenetre avec un bouton. Le clic sur le bouton bouton appelle un module de connexion à une base de données " ConnectionBase"
Tout cela fonctionne bien.
Pour apprendre, je voudrais récuperer une variable "URL" de ConnectionBase et l'afficher quand je clique sur le bouton
......je n'y arrive pas, je ne comprends pas
Pouvez vous m'aider?
voici les codes
Fenetre
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
package pack;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Fenetre extends javax.swing.JFrame {
private JButton bouton1;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Fenetre inst = new Fenetre();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public Fenetre() {
super();
initGUI();
}
private void initGUI() {
try {
BorderLayout thisLayout = new BorderLayout();
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(thisLayout);
{
bouton1 = new JButton();
getContentPane().add(bouton1, BorderLayout.NORTH);
bouton1.setText("bouton1");
bouton1.setPreferredSize(new java.awt.Dimension(392, 52));
bouton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.out.println("bouton1.actionPerformed, event="+evt);
//TODO add your code for bouton1.actionPerformed
ConnectionBase connect = new ConnectionBase();
connect.connection();
// System.out.println(URL)...NE FONCTIONNE PAS URL NON RECONNU
}
});
}
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
} |
code ConnectionBase:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
package pack;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
class ConnectionBase {
public void connection(){
String URL = "jdbc:mysql://localhost:3306/base1";
String nomutilisateur = "root";
String motdepasse = "";
// chargement du pilote
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (Exception e){
System.err.println("Le pilote ne peut etre chargé");
}
// connexion à la base de données
Statement commande = null;
Connection connexion = null;
try {
connexion = DriverManager.getConnection (URL, nomutilisateur, motdepasse);
commande = connexion.createStatement();
}
catch (Exception e){
System.err.println("la connexion ne peut etre etablie");
}
}
} |