Bonjour tout le monde,
Avant de commencer je tiens à signaler que je débute en java.
J'ai essayé d'établir une connexion entre eclipse et mysql.
Ci dessous vous trouver le code que j'ai fait pour se connecter à MysqlAprès l'exécution du code, je me suis rendu compte qu'il y a un problème de driver vu qu'il m'affiche pas le message (driver chargé, le message que je l'affiche juste après Class.forName(driver);
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171 import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.SQLWarning; public class Test { // les chaines utiles a la connexion avec valeurs par defaut private String driver = "com.mysql.jdbc.Driver"; private String url = "jdbc:mysql://localhost:3306/test"; private String login = "root"; private String password = ""; private Connection conn = null; /* Afficher quelques informations à propos the database connection */ public void printInfo(Connection c) throws Exception { // Get meta-data about the database DatabaseMetaData info = c.getMetaData(); System.out.println("\nConnected to :\t" + info.getURL()); System.out.println("Driver :\t" + info.getDriverName()); System.out.println("Version :\t" + info.getDriverVersion()); } /* Afficher tous les SQLWarning */ private boolean checkForSQLWarnings(SQLWarning w) throws SQLException { boolean warning = false; if(w != null) { warning = true; System.out.println("\n**** Warning ****\n"); while(w != null) { System.out.println("SQLState: " + w.getSQLState()); System.out.println("Message: " + w.getMessage()); System.out.println("Erreur: " + w.getErrorCode()); System.out.println(""); w = w.getNextWarning(); } } return warning; } public void setDriver(String newDriver) { driver = newDriver; } public void setLogin(String newLogin, String newPassword) { login = newLogin; password = newPassword; } public void setUrl(String newUrl) { url = newUrl; } public Connection getConnection() { return conn; } /* Afficher tous les SQLException */ private void printSQLErrors(SQLException e) { while(e != null) { System.err.println("SQLState: " + e.getSQLState()); System.err.println("Message: " + e.getMessage()); System.err.println("Erreur: " + e.getErrorCode()); System.err.println(""); e = e.getNextException(); } } public void loadDriverAndConnect() throws Exception { try { // Si la connexion existe deja : on ferme d'abord, puis on re-ouvre if (conn!=null && !conn.isClosed() ) { conn.close(); } // Load the jdbc driver for MySQL /** * Le nom de la machine ou s'execute le SGBD MySQL : localhost (en local) * Le nuero de port sur lequel le SGBD est à l'écoute : 3306 * le nom de la base de données à accéder : test * le login : root * le mot de passe : [aucun] * */ System.out.println("driver pas encore chargé"); Class.forName(driver); System.out.println("driver chargé"); // Etablir une connexion to the database source using Thin JDBC driver conn = DriverManager.getConnection(url, login, password); // Afficher un warning messages si nécessaire checkForSQLWarnings(conn.getWarnings()); // Print info messages printInfo(conn); } catch(SQLException e) { System.err.println("\n*** SQLException caught in LoadDriver()"); printSQLErrors(e); throw e; } catch(Exception e) { // Ce type d'erreur est affiché si JDBC driver used ne peut pas se charger System.err.println("\n*** Exception caught in LoadDriver()"); throw e; } } /** * Fermer la connexion to the data base source */ public void close() throws Exception { try { conn.close(); System.out.println("Test : Disconnecting ..."); } catch(Exception e) { System.err.println("\n*** Exception caught in close()"); throw e; } } } class TestMain { public static void main(String[] args) { Test t = new Test(); try{ t.loadDriverAndConnect(); // t.createTablesAndInit(); t.close(); } catch(Exception e) { System.err.println("\n*** SQLException caught in main()"); } } }
Voilà, j'espère que j'étais clair.
Avant d'oublier:
Comme logiciel , j'ai
Partager