Question sur JDBC classe mysql
Bonjour a tous :).
Je me posais une petite question, j'ai donc recuperer une classe mysql pour se connecter a ma base mysql. Bref tout marche très bien.
J'ai une petite question concernant cette classe :
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
| package pressingAlpha;
import java.sql.*;
import javax.swing.JOptionPane;
public class mysql {
public mysql() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
//-----------------------------------------------------------------------------
private String host = "192.168.0.2";
private String username = "root";
private String password = "****";
private String numPort = "3306";
private String base = "pressing";
public static Connection connexion;
//-----------------------------------------------------------------------------
/**
* connexionDb -> Connexion a la base de données avec les valeurs
* initialisées au dessus(host,username...).
* La connection se fait au moyen de JDBC.
*
* @throws SQLException en cas d'erreur dans la base de données.
*/
public void connexionDb() {
// Connexion à la base
try{
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
String connectString = "jdbc:mysql://" + host + ":" + numPort + "/" + base;
connexion = DriverManager.getConnection(connectString, username, password);
System.out.println("Connexion a " + base + " sur " + host + " --> OK. Identifiant : "+username);
}
private void jbInit() throws Exception {
}
}
} |
Pk suis-je obligé de declarer Connection en public? Puis-je y remedier?
J'ai un oral bientot et apparement les examinateurs n'aiment pas trop les variables déclarées en public.
Merci a tous pour votre aide :)