KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > connection > JDBCConnection


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * JDBCConnection.java
28  *
29  * Created on 4 giugno 2003, 18.15
30  *
31  */

32
33 package it.businesslogic.ireport.connection;
34 import it.businesslogic.ireport.*;
35 import it.businesslogic.ireport.gui.MainFrame;
36 import it.businesslogic.ireport.util.*;
37 import java.lang.reflect.InvocationTargetException JavaDoc;
38 import java.sql.*;
39 import javax.swing.*;
40 /**
41  *
42  * @author Administrator
43  */

44 public class JDBCConnection extends it.businesslogic.ireport.IReportConnection {
45     
46     private String JavaDoc JDBCDriver;
47     
48     private String JavaDoc username;
49     
50     private String JavaDoc password = null;
51     
52     private String JavaDoc url;
53     
54     private String JavaDoc database;
55     
56     private boolean savePassword;
57     
58     private String JavaDoc name;
59     
60     /**
61      * Holds value of property serverAddress.
62      */

63     private String JavaDoc serverAddress;
64     
65     /** Creates a new instance of JDBCConnection */
66     
67     
68     public JDBCConnection() {
69     }
70     
71     /** This method return an instanced connection to the database.
72      * If isJDBCConnection() return false => getConnection() return null
73      *
74      */

75     public java.sql.Connection JavaDoc getConnection() {
76         
77             // Try the java connection...
78
try {
79                 
80                     it.businesslogic.ireport.gui.MainFrame.getMainInstance().getReportClassLoader().rescanLibDirectory();
81                     
82                     try {
83                         DriverPool.registerDriver( this.getJDBCDriver(), it.businesslogic.ireport.gui.MainFrame.getMainInstance().getReportClassLoader() );
84                     } catch (Exception JavaDoc ex)
85                     {
86                         DriverPool.registerDriver( this.getJDBCDriver(), this.getClass().getClassLoader() );
87                     }
88                     
89                     java.sql.Driver JavaDoc driver = DriverPool.getDriver( url );
90                     
91                     java.util.Properties JavaDoc connectProps = new java.util.Properties JavaDoc();
92                     
93                     if ((password == null || password.equals("") ) && !isSavePassword())
94                     {
95                         password = getPassword();
96                     }
97                     
98                     connectProps.setProperty("user", username);
99                     connectProps.setProperty("password", password);
100                     
101                     Connection conn = driver.connect( url, connectProps);
102                     
103                     if ( (this.getJDBCDriver().toLowerCase().indexOf("oracle") >= 0) && (it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties().getProperty("oracle_language","").trim().length() > 0 ||
104                         it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties().getProperty("oracle_territory","").trim().length() > 0) )
105                     {
106                         Statement stmt = null;
107                         try {
108                             stmt = conn.createStatement();
109                             if (it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties().getProperty("oracle_language","").trim().length() > 0)
110                     stmt.execute("ALTER SESSION SET NLS_LANGUAGE = '" + it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties().getProperty("oracle_language","").trim() + "'");
111                     if (it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties().getProperty("oracle_territory","").trim().length() > 0)
112                     stmt.execute("ALTER SESSION SET NLS_TERRITORY='" + it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties().getProperty("oracle_territory","").trim() + "'");
113                     
114                         } catch (Exception JavaDoc ex)
115                         {
116                             //ex.printStackTrace();
117
}
118                     finally {
119                         if (stmt != null) stmt.close();
120                     }
121                     }
122         
123                     return conn;
124             
125             }catch (NoClassDefFoundError JavaDoc ex)
126         {
127                     showErrorMessage(I18n.getFormattedString("messages.connection.noClassDefFoundError",
128                                 "{0}\nNoClassDefFoundError!!\nCheck your classpath!\n{1}",
129                                 new Object JavaDoc[]{""+ this.getName(), ""+ex.getMessage()}),
130                                 I18n.getString("message.title.exception","Exception"));
131                     
132             return null;
133         }
134         catch (ClassNotFoundException JavaDoc ex)
135         {
136             showErrorMessage(I18n.getFormattedString("messages.connection.classNotFoundError",
137                                 "{0}\nClassNotFoundError!\nMsg: {1}\nPossible not found class: {2}\nCheck your classpath!",
138                                 new Object JavaDoc[]{""+ this.getName(), ""+ex.getMessage(), "" + this.getJDBCDriver()}),
139                                 I18n.getString("message.title.exception","Exception"));
140             return null;
141         }
142         catch (java.sql.SQLException JavaDoc ex)
143         {
144             if (!savePassword) password = null;
145             showErrorMessage(I18n.getFormattedString("messages.connection.sqlError",
146                                 "{0}\nSQL problems: {1}\n{2}",
147                                 new Object JavaDoc[]{""+ this.getName(), ""+ex.getMessage(), "" + url}),
148                                 I18n.getString("message.title.exception","Exception"));
149             return null;
150         }
151         catch (Exception JavaDoc ex)
152         {
153             showErrorMessage(I18n.getFormattedString("messages.connection.generalError",
154                                 "{0}\nGeneral problem: {1}\nPlease check your username and password. The DBMS is running?!",
155                                 new Object JavaDoc[]{""+ this.getName(), ""+ex.getMessage()}),
156                                 I18n.getString("message.title.exception","Exception"));
157             return null;
158         }
159     }
160     
161     private void showErrorMessage(final String JavaDoc errorMsg, final String JavaDoc title)
162     {
163         Runnable JavaDoc r = new Runnable JavaDoc() {
164                 public void run() {
165                     JOptionPane.showMessageDialog(MainFrame.getMainInstance(),errorMsg,title,JOptionPane.ERROR_MESSAGE);
166                 }
167             };
168
169         if (!SwingUtilities.isEventDispatchThread())
170         {
171             try {
172                 SwingUtilities.invokeAndWait( r );
173             } catch (InvocationTargetException JavaDoc ex) {
174                 ex.printStackTrace();
175             } catch (InterruptedException JavaDoc ex) {
176                 ex.printStackTrace();
177             }
178         }
179         else
180         {
181                 r.run();
182         }
183     }
184     
185     
186     /* This method return an instanced JRDataDource to the database.
187      * If isJDBCConnection() return true => getJRDataSource() return false
188      *
189      */

190     public net.sf.jasperreports.engine.JRDataSource getJRDataSource() {
191         return new net.sf.jasperreports.engine.JREmptyDataSource();
192     }
193     
194     public boolean isJDBCConnection() {
195         return true;
196     }
197     
198     /** Getter for property database.
199      * @return Value of property database.
200      *
201      */

202     public java.lang.String JavaDoc getDatabase() {
203         return database;
204     }
205     
206     /** Setter for property database.
207      * @param database New value of property database.
208      *
209      */

210     public void setDatabase(java.lang.String JavaDoc database) {
211         this.database = database;
212     }
213     
214     /** Getter for property JDBCDriver.
215      * @return Value of property JDBCDriver.
216      *
217      */

218     public java.lang.String JavaDoc getJDBCDriver() {
219         return JDBCDriver;
220     }
221     
222     /** Setter for property JDBCDriver.
223      * @param JDBCDriver New value of property JDBCDriver.
224      *
225      */

226     public void setJDBCDriver(java.lang.String JavaDoc JDBCDriver) {
227         this.JDBCDriver = JDBCDriver;
228     }
229     
230     /** Getter for property password.
231      * @return Value of property password.
232      *
233      */

234     public java.lang.String JavaDoc getPassword() {
235         
236         if (isSavePassword()) return password;
237         else
238         {
239             // Ask for password...
240
try {
241                 return it.businesslogic.ireport.gui.PasswordDialog.askPassword();
242             } catch (Exception JavaDoc ex) {
243                 ex.printStackTrace();
244             }
245         }
246         return "";
247     }
248     
249     /** Setter for property password.
250      * @param password New value of property password.
251      *
252      */

253     public void setPassword(java.lang.String JavaDoc password) {
254         this.password = password;
255     }
256     
257     /** Getter for property savePassword.
258      * @return Value of property savePassword.
259      *
260      */

261     public boolean isSavePassword() {
262         return savePassword;
263     }
264     
265     /** Setter for property savePassword.
266      * @param savePassword New value of property savePassword.
267      *
268      */

269     public void setSavePassword(boolean savePassword) {
270         this.savePassword = savePassword;
271     }
272     
273     /** Getter for property url.
274      * @return Value of property url.
275      *
276      */

277     public java.lang.String JavaDoc getUrl() {
278         return url;
279     }
280     
281     /** Setter for property url.
282      * @param url New value of property url.
283      *
284      */

285     public void setUrl(java.lang.String JavaDoc url) {
286         this.url = url;
287     }
288     
289     /** Getter for property username.
290      * @return Value of property username.
291      *
292      */

293     public java.lang.String JavaDoc getUsername() {
294         return username;
295     }
296     
297     /** Setter for property username.
298      * @param username New value of property username.
299      *
300      */

301     public void setUsername(java.lang.String JavaDoc username) {
302         this.username = username;
303     }
304     
305     /*
306      * This method return all properties used by this connection
307      */

308     public java.util.HashMap JavaDoc getProperties()
309     {
310         java.util.HashMap JavaDoc map = new java.util.HashMap JavaDoc();
311         map.put("JDBCDriver", Misc.nvl(this.getJDBCDriver(),"") );
312         map.put("Url", Misc.nvl(this.getUrl(),""));
313         map.put("Database", Misc.nvl(this.getDatabase(),""));
314         map.put("Username", Misc.nvl(this.getUsername(),""));
315         if (this.isSavePassword())
316             map.put("Password", Misc.nvl(this.getPassword(),""));
317         else map.put("Password","");
318         map.put("SavePassword", ""+this.isSavePassword());
319         map.put("ServerAddress", Misc.nvl(this.getServerAddress(),"") );
320         
321         return map;
322     }
323     
324     public void loadProperties(java.util.HashMap JavaDoc map)
325     {
326         this.setJDBCDriver( (String JavaDoc)map.get("JDBCDriver"));
327         this.setUrl( (String JavaDoc)map.get("Url"));
328         this.setDatabase( (String JavaDoc)map.get("Database"));
329         this.setUsername( (String JavaDoc)map.get("Username"));
330         this.setSavePassword( (""+map.get("SavePassword")).equals("true") );
331         if (this.isSavePassword())
332             this.setPassword( Misc.nvl((String JavaDoc)map.get("Password"),""));
333         this.setServerAddress( Misc.nvl((String JavaDoc)map.get("ServerAddress"),"") );
334         
335     }
336     
337     public String JavaDoc getDescription(){ return "Database JDBC Connection ("+ this.getUrl() +")"; }
338     
339     /**
340      * Getter for property serverAddress.
341      * @return Value of property serverAddress.
342      */

343     public String JavaDoc getServerAddress() {
344         return this.serverAddress;
345     }
346     
347     /**
348      * Setter for property serverAddress.
349      * @param serverAddress New value of property serverAddress.
350      */

351     public void setServerAddress(String JavaDoc serverAddress) {
352         this.serverAddress = serverAddress;
353     }
354     
355 }
356
Popular Tags