Bonjour,
J'utilise Eclipse Juno + quantum DB + PGaDMIN III.
Je n'arrive pas à me connecter à ma base de données Postgres, via le "main" de ma classe java. (le "main" se trouve à la fin du code)
Alors qu'avec les mêmes informations Quantum DB se connecte.
Message de la console :
Exception in thread "main" java.lang.NullPointerException
at DataBase.getNbColonne(DataBase.java:84)
at DataBase.main(DataBase.java:205)
Erreur lors du chargement du pilote jdbc:odbc : org.postgresql.Driver
echec d'ouverture:No suitable driver found for jdbc:postgresql://localhost:5432/com
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 import java.io.*; import java.sql.*; import java.util.*; public class DataBase { private Connection con; private ResultSet results; private ResultSetMetaData rsmd; private Statement stmt; //Constructeur public DataBase(String driver) { // TODO Auto-generated constructor stub try{ this.results=null; Class.forName(driver); } catch (ClassNotFoundException e){ System.out.println("Erreur lors du chargement du pilote jdbc:odbc : "+ e.getMessage()); } } //Méthodes public boolean ouvre(String url,String user,String mp) { boolean ok = false; try { this.con = DriverManager.getConnection(url,user,mp); ok = true; } catch (SQLException e) { System.out.println("echec d'ouverture:" + e.getMessage()); ok = false; } return ok; } //------------- public boolean ferme(){ boolean ok=false; try{ this.con.close(); ok=true; } catch(SQLException e){ System.out.println("echec lors de la fermeture : "+e.getMessage()); ok=false; } return ok; } //--------- public void executeRequete(String requete){ try{ this.stmt=con.createStatement(); this.results=this.stmt.executeQuery(requete); } catch(SQLException e){ System.out.println("Erreur requête : "+e.getMessage()); } } //-------- public ResultSet getResult(){ return this.results; } //-------- public void recupDonnees(){ try{ this.rsmd=this.results.getMetaData(); } catch (SQLException e){ System.out.println("Erreur données : "+e.getMessage()); } } //------- public int getNbColonne(){ int nbCols=0; try{ nbCols=this.rsmd.getColumnCount(); } catch(SQLException e){ System.out.println("Erreur données : "+e.getMessage()); } return nbCols; } //------- public boolean ligneSuivante(){ boolean encore = false; try{ encore=this.results.next(); } catch(SQLException e){ System.out.println("Erreur nombre colonnes : "+e.getMessage()); } return encore; } //---- public String getValeur(int i){ String valeur=""; try{ valeur=this.results.getString(i); } catch(SQLException e){ } return valeur; } //------- public ResultSetMetaData getDonnees(){ return this.rsmd; } //------- public Vector<String> getNomColonne(){ Vector<String> vec=new Vector<String>(); for(int i=1; i<=this.getNbColonne();i++){ try{ vec.add(this.rsmd.getColumnName(i)); } catch(SQLException e){ } } return vec; } //extrait les données de la requête dans un fichier csv public void write_csv(String nomFichier){ Vector<String> vec=this.getNomColonne(); try{ BufferedWriter out=new BufferedWriter(new FileWriter(nomFichier)); for(int i=1;i<=this.getNbColonne();i++){ out.write((String) vec.elementAt(i-1)); if(i!=this.getNbColonne()){ out.write(";"); } else{ out.newLine(); } } boolean encore=this.ligneSuivante(); while(encore){ for(int i=1;i<=this.getNbColonne();i++){ out.write(this.getValeur(i)); if(i!=this.getNbColonne()){ out.write(";"); } else{ out.newLine(); } encore=this.ligneSuivante(); } out.close(); } }//try catch(FileNotFoundException e){ //TODO Auto-generated catch block e.printStackTrace(); } catch(IOException e){ //TODO Auto-generated catch block e.printStackTrace(); } }//fin write_csv /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String url="jdbc:postgresql://localhost:5432/com"; String user="postgres"; String mp="123456"; String driver ="org.postgresql.Driver"; DataBase db=new DataBase(driver); db.ouvre(url,user,mp); db.getNbColonne(); } }
Partager