illustration pattern singleton pour se connecter à une base de données
Bonjour La communauté,
je tente d'illustrer le pattern singleton.
je veux utiliser 1 singleton pr me connecter à une base de données de la sorte
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import java.sql.*;
import java.util.*;
public class Singleton{
private static String url = "jdbc:mysql://localhost:3306/aigemedb";
private static String user = "root";
private static String passwd = "monpass";
private static Connection conn;
public static Connection getInstance(){
if (conn==null){
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("url","user","passwd");
} catch (Exception e){
e.printStackTrace(); }}
return conn;
} |
la compilation de cela me ramène aucune erreur.
Lorsque je compile le code ci dessous. j'ai un message d'erreur
Code:
1 2
| Symbol: variable Singleton
location:class ConnectSingleton |
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
| import java.sql.*;
import java.util.*;
public class ConnectSingleton{
Statement state;
public void afficher(){
try{
ResultSet result = state.executeQuery("SELECT id_etudiant,sexe_etudiant,nom_etudiant FROM etudiant limit 5");
ResultSetMetaData resultMeta = result.getMetaData();
System.out.println("Il y'a " +resultMeta.getColumnCount()+ " Colonnes Dans cette Table");
for(int i = 1; i <= resultMeta.getColumnCount(); i++)
{System.out.println("* " + resultMeta.getColumnName(i));}
System.out.println("\n******************************************************************************");
//On affiche le nom des colonnes
for(int i = 1; i <= resultMeta.getColumnCount(); i++)
{System.out.print("\t" + resultMeta.getColumnName(i).toUpperCase() + "\t *");}
System.out.println("\n*************************************************");
while(result.next()){
for(int i = 1; i <= resultMeta.getColumnCount(); i++)
System.out.print("\t" + result.getObject(i).toString() + "\t |");
System.out.println("\n--------------------------------------------------------------------------"); }
state.close();
result.close();
}catch (Exception e) {
e.printStackTrace();
} }
public static void main(String args[]) {
Singleton.getInstance().afficher();
}
} |
merci de bien vouloir m'indiquer ce qui cloche dans mon code.