1 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 ; 38 import java.sql.*; 39 import javax.swing.*; 40 44 public class JDBCConnection extends it.businesslogic.ireport.IReportConnection { 45 46 private String JDBCDriver; 47 48 private String username; 49 50 private String password = null; 51 52 private String url; 53 54 private String database; 55 56 private boolean savePassword; 57 58 private String name; 59 60 63 private String serverAddress; 64 65 66 67 68 public JDBCConnection() { 69 } 70 71 75 public java.sql.Connection getConnection() { 76 77 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 ex) 85 { 86 DriverPool.registerDriver( this.getJDBCDriver(), this.getClass().getClassLoader() ); 87 } 88 89 java.sql.Driver driver = DriverPool.getDriver( url ); 90 91 java.util.Properties connectProps = new java.util.Properties (); 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 ex) 115 { 116 } 118 finally { 119 if (stmt != null) stmt.close(); 120 } 121 } 122 123 return conn; 124 125 }catch (NoClassDefFoundError ex) 126 { 127 showErrorMessage(I18n.getFormattedString("messages.connection.noClassDefFoundError", 128 "{0}\nNoClassDefFoundError!!\nCheck your classpath!\n{1}", 129 new Object []{""+ this.getName(), ""+ex.getMessage()}), 130 I18n.getString("message.title.exception","Exception")); 131 132 return null; 133 } 134 catch (ClassNotFoundException 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 []{""+ this.getName(), ""+ex.getMessage(), "" + this.getJDBCDriver()}), 139 I18n.getString("message.title.exception","Exception")); 140 return null; 141 } 142 catch (java.sql.SQLException ex) 143 { 144 if (!savePassword) password = null; 145 showErrorMessage(I18n.getFormattedString("messages.connection.sqlError", 146 "{0}\nSQL problems: {1}\n{2}", 147 new Object []{""+ this.getName(), ""+ex.getMessage(), "" + url}), 148 I18n.getString("message.title.exception","Exception")); 149 return null; 150 } 151 catch (Exception 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 []{""+ this.getName(), ""+ex.getMessage()}), 156 I18n.getString("message.title.exception","Exception")); 157 return null; 158 } 159 } 160 161 private void showErrorMessage(final String errorMsg, final String title) 162 { 163 Runnable r = new Runnable () { 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 ex) { 174 ex.printStackTrace(); 175 } catch (InterruptedException ex) { 176 ex.printStackTrace(); 177 } 178 } 179 else 180 { 181 r.run(); 182 } 183 } 184 185 186 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 202 public java.lang.String getDatabase() { 203 return database; 204 } 205 206 210 public void setDatabase(java.lang.String database) { 211 this.database = database; 212 } 213 214 218 public java.lang.String getJDBCDriver() { 219 return JDBCDriver; 220 } 221 222 226 public void setJDBCDriver(java.lang.String JDBCDriver) { 227 this.JDBCDriver = JDBCDriver; 228 } 229 230 234 public java.lang.String getPassword() { 235 236 if (isSavePassword()) return password; 237 else 238 { 239 try { 241 return it.businesslogic.ireport.gui.PasswordDialog.askPassword(); 242 } catch (Exception ex) { 243 ex.printStackTrace(); 244 } 245 } 246 return ""; 247 } 248 249 253 public void setPassword(java.lang.String password) { 254 this.password = password; 255 } 256 257 261 public boolean isSavePassword() { 262 return savePassword; 263 } 264 265 269 public void setSavePassword(boolean savePassword) { 270 this.savePassword = savePassword; 271 } 272 273 277 public java.lang.String getUrl() { 278 return url; 279 } 280 281 285 public void setUrl(java.lang.String url) { 286 this.url = url; 287 } 288 289 293 public java.lang.String getUsername() { 294 return username; 295 } 296 297 301 public void setUsername(java.lang.String username) { 302 this.username = username; 303 } 304 305 308 public java.util.HashMap getProperties() 309 { 310 java.util.HashMap map = new java.util.HashMap (); 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 map) 325 { 326 this.setJDBCDriver( (String )map.get("JDBCDriver")); 327 this.setUrl( (String )map.get("Url")); 328 this.setDatabase( (String )map.get("Database")); 329 this.setUsername( (String )map.get("Username")); 330 this.setSavePassword( (""+map.get("SavePassword")).equals("true") ); 331 if (this.isSavePassword()) 332 this.setPassword( Misc.nvl((String )map.get("Password"),"")); 333 this.setServerAddress( Misc.nvl((String )map.get("ServerAddress"),"") ); 334 335 } 336 337 public String getDescription(){ return "Database JDBC Connection ("+ this.getUrl() +")"; } 338 339 343 public String getServerAddress() { 344 return this.serverAddress; 345 } 346 347 351 public void setServerAddress(String serverAddress) { 352 this.serverAddress = serverAddress; 353 } 354 355 } 356 | Popular Tags |