Source Code Cross Referenced for JDBCConnection.java in  » Report » iReport-2.0.5 » it » businesslogic » ireport » connection » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Report » iReport 2.0.5 » it.businesslogic.ireport.connection 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (C) 2005 - 2008 JasperSoft Corporation.  All rights reserved. 
003:         * http://www.jaspersoft.com.
004:         *
005:         * Unless you have purchased a commercial license agreement from JasperSoft,
006:         * the following license terms apply:
007:         *
008:         * This program is free software; you can redistribute it and/or modify
009:         * it under the terms of the GNU General Public License version 2 as published by
010:         * the Free Software Foundation.
011:         *
012:         * This program is distributed WITHOUT ANY WARRANTY; and without the
013:         * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014:         * See the GNU General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU General Public License
017:         * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018:         * or write to:
019:         *
020:         * Free Software Foundation, Inc.,
021:         * 59 Temple Place - Suite 330,
022:         * Boston, MA  USA  02111-1307
023:         *
024:         *
025:         *
026:         *
027:         * JDBCConnection.java
028:         * 
029:         * Created on 4 giugno 2003, 18.15
030:         *
031:         */
032:
033:        package it.businesslogic.ireport.connection;
034:
035:        import it.businesslogic.ireport.*;
036:        import it.businesslogic.ireport.connection.gui.JDBCConnectionEditor;
037:        import it.businesslogic.ireport.IReportConnectionEditor;
038:        import it.businesslogic.ireport.gui.MainFrame;
039:        import it.businesslogic.ireport.util.*;
040:        import java.lang.reflect.InvocationTargetException;
041:        import java.sql.*;
042:        import javax.swing.*;
043:        import org.jdesktop.swingx.JXErrorPane;
044:        import org.jdesktop.swingx.error.ErrorInfo;
045:
046:        /**
047:         *
048:         * @author  Administrator
049:         */
050:        public class JDBCConnection extends
051:                it.businesslogic.ireport.IReportConnection {
052:
053:            private String JDBCDriver;
054:
055:            private String username;
056:
057:            private String password = null;
058:
059:            private String url;
060:
061:            private String database;
062:
063:            private boolean savePassword;
064:
065:            private String name;
066:
067:            /**
068:             * Holds value of property serverAddress.
069:             */
070:            private String serverAddress;
071:
072:            /** Creates a new instance of JDBCConnection */
073:
074:            public JDBCConnection() {
075:            }
076:
077:            /**  This method return an instanced connection to the database.
078:             *  If isJDBCConnection() return false => getConnection() return null
079:             *
080:             */
081:            public java.sql.Connection getConnection() {
082:
083:                // Try the java connection...
084:                try {
085:
086:                    it.businesslogic.ireport.gui.MainFrame.getMainInstance()
087:                            .getReportClassLoader().rescanLibDirectory();
088:
089:                    try {
090:                        DriverPool.registerDriver(this .getJDBCDriver(),
091:                                it.businesslogic.ireport.gui.MainFrame
092:                                        .getMainInstance()
093:                                        .getReportClassLoader());
094:                    } catch (Exception ex) {
095:                        DriverPool.registerDriver(this .getJDBCDriver(), this 
096:                                .getClass().getClassLoader());
097:                    }
098:
099:                    java.sql.Driver driver = DriverPool.getDriver(url);
100:
101:                    java.util.Properties connectProps = new java.util.Properties();
102:
103:                    if ((password == null || password.equals(""))
104:                            && !isSavePassword()) {
105:                        password = getPassword();
106:                    }
107:
108:                    connectProps.setProperty("user", username);
109:                    connectProps.setProperty("password", password);
110:
111:                    Connection conn = driver.connect(url, connectProps);
112:
113:                    if ((this .getJDBCDriver().toLowerCase().indexOf("oracle") >= 0)
114:                            && (it.businesslogic.ireport.gui.MainFrame
115:                                    .getMainInstance().getProperties()
116:                                    .getProperty("oracle_language", "").trim()
117:                                    .length() > 0 || it.businesslogic.ireport.gui.MainFrame
118:                                    .getMainInstance().getProperties()
119:                                    .getProperty("oracle_territory", "").trim()
120:                                    .length() > 0)) {
121:                        Statement stmt = null;
122:                        try {
123:                            stmt = conn.createStatement();
124:                            if (it.businesslogic.ireport.gui.MainFrame
125:                                    .getMainInstance().getProperties()
126:                                    .getProperty("oracle_language", "").trim()
127:                                    .length() > 0)
128:                                stmt
129:                                        .execute("ALTER SESSION SET NLS_LANGUAGE = '"
130:                                                + it.businesslogic.ireport.gui.MainFrame
131:                                                        .getMainInstance()
132:                                                        .getProperties()
133:                                                        .getProperty(
134:                                                                "oracle_language",
135:                                                                "").trim()
136:                                                + "'");
137:                            if (it.businesslogic.ireport.gui.MainFrame
138:                                    .getMainInstance().getProperties()
139:                                    .getProperty("oracle_territory", "").trim()
140:                                    .length() > 0)
141:                                stmt
142:                                        .execute("ALTER SESSION SET NLS_TERRITORY='"
143:                                                + it.businesslogic.ireport.gui.MainFrame
144:                                                        .getMainInstance()
145:                                                        .getProperties()
146:                                                        .getProperty(
147:                                                                "oracle_territory",
148:                                                                "").trim()
149:                                                + "'");
150:
151:                        } catch (Exception ex) {
152:                            //ex.printStackTrace();
153:                        } finally {
154:                            if (stmt != null)
155:                                stmt.close();
156:                        }
157:                    }
158:
159:                    return conn;
160:
161:                } catch (NoClassDefFoundError ex) {
162:                    showErrorMessage(
163:                            I18n
164:                                    .getFormattedString(
165:                                            "messages.connection.noClassDefFoundError",
166:                                            "{0}\nNoClassDefFoundError!!\nCheck your classpath!\n{1}",
167:                                            new Object[] { "" + this .getName(),
168:                                                    "" + ex.getMessage() }),
169:                            I18n.getString("message.title.exception",
170:                                    "Exception"), ex);
171:
172:                    return null;
173:                } catch (ClassNotFoundException ex) {
174:                    showErrorMessage(
175:                            I18n
176:                                    .getFormattedString(
177:                                            "messages.connection.classNotFoundError",
178:                                            "{0}\nClassNotFoundError!\nMsg: {1}\nPossible not found class: {2}\nCheck your classpath!",
179:                                            new Object[] { "" + this .getName(),
180:                                                    "" + ex.getMessage(),
181:                                                    "" + this .getJDBCDriver() }),
182:                            I18n.getString("message.title.exception",
183:                                    "Exception"), ex);
184:                    return null;
185:                } catch (java.sql.SQLException ex) {
186:                    if (!savePassword)
187:                        password = null;
188:                    showErrorMessage(I18n.getFormattedString(
189:                            "messages.connection.sqlError",
190:                            "{0}\nSQL problems: {1}\n{2}", new Object[] {
191:                                    "" + this .getName(), "" + ex.getMessage(),
192:                                    "" + url }), I18n.getString(
193:                            "message.title.exception", "Exception"), ex);
194:                    return null;
195:                } catch (Exception ex) {
196:                    showErrorMessage(
197:                            I18n
198:                                    .getFormattedString(
199:                                            "messages.connection.generalError",
200:                                            "{0}\nGeneral problem: {1}\nPlease check your username and password. The DBMS is running?!",
201:                                            new Object[] { "" + this .getName(),
202:                                                    "" + ex.getMessage() }),
203:                            I18n.getString("message.title.exception",
204:                                    "Exception"), ex);
205:                    return null;
206:                }
207:            }
208:
209:            private void showErrorMessage(String errorMsg, String title,
210:                    Throwable theException) {
211:
212:                final JXErrorPane pane = new JXErrorPane();
213:                pane.setLocale(I18n.getCurrentLocale());
214:
215:                String[] lines = errorMsg.split("\r\n|\n|\r");
216:
217:                String shortMessage = errorMsg;
218:                if (lines.length > 4) {
219:                    shortMessage = "";
220:                    for (int i = 0; i < 4; ++i) {
221:                        shortMessage += lines[i] + "\n";
222:                    }
223:                    shortMessage = shortMessage.trim() + "\n...";
224:                }
225:
226:                ErrorInfo ei = new ErrorInfo(title, shortMessage, null, //"<html><pre>" + errorMsg + "</pre>"
227:                        null, theException, null, null);
228:                pane.setErrorInfo(ei);
229:
230:                /*
231:                
232:                
233:                final String fErrorMsg = errorMsg;
234:                 */
235:                Runnable r = new Runnable() {
236:                    public void run() {
237:                        // JOptionPane.showMessageDialog(MainFrame.getMainInstance(),fErrorMsg,title,JOptionPane.ERROR_MESSAGE);
238:
239:                        JXErrorPane.showDialog(MainFrame.getMainInstance(),
240:                                pane);
241:                    }
242:                };
243:
244:                if (!SwingUtilities.isEventDispatchThread()) {
245:                    try {
246:                        SwingUtilities.invokeAndWait(r);
247:                    } catch (InvocationTargetException ex) {
248:                        ex.printStackTrace();
249:                    } catch (InterruptedException ex) {
250:                        ex.printStackTrace();
251:                    }
252:                } else {
253:                    r.run();
254:                }
255:            }
256:
257:            /*  This method return an instanced JRDataDource to the database.
258:             *  If isJDBCConnection() return true => getJRDataSource() return false
259:             *
260:             */
261:            public net.sf.jasperreports.engine.JRDataSource getJRDataSource() {
262:                return new net.sf.jasperreports.engine.JREmptyDataSource();
263:            }
264:
265:            public boolean isJDBCConnection() {
266:                return true;
267:            }
268:
269:            /** Getter for property database.
270:             * @return Value of property database.
271:             *
272:             */
273:            public java.lang.String getDatabase() {
274:                return database;
275:            }
276:
277:            /** Setter for property database.
278:             * @param database New value of property database.
279:             *
280:             */
281:            public void setDatabase(java.lang.String database) {
282:                this .database = database;
283:            }
284:
285:            /** Getter for property JDBCDriver.
286:             * @return Value of property JDBCDriver.
287:             *
288:             */
289:            public java.lang.String getJDBCDriver() {
290:                return JDBCDriver;
291:            }
292:
293:            /** Setter for property JDBCDriver.
294:             * @param JDBCDriver New value of property JDBCDriver.
295:             *
296:             */
297:            public void setJDBCDriver(java.lang.String JDBCDriver) {
298:                this .JDBCDriver = JDBCDriver;
299:            }
300:
301:            /** Getter for property password.
302:             * @return Value of property password.
303:             *
304:             */
305:            public java.lang.String getPassword() {
306:
307:                if (isSavePassword())
308:                    return password;
309:                else {
310:                    // Ask for password...
311:                    try {
312:                        return it.businesslogic.ireport.gui.PasswordDialog
313:                                .askPassword();
314:                    } catch (Exception ex) {
315:                        ex.printStackTrace();
316:                    }
317:                }
318:                return "";
319:            }
320:
321:            /** Setter for property password.
322:             * @param password New value of property password.
323:             *
324:             */
325:            public void setPassword(java.lang.String password) {
326:                this .password = password;
327:            }
328:
329:            /** Getter for property savePassword.
330:             * @return Value of property savePassword.
331:             *
332:             */
333:            public boolean isSavePassword() {
334:                return savePassword;
335:            }
336:
337:            /** Setter for property savePassword.
338:             * @param savePassword New value of property savePassword.
339:             *
340:             */
341:            public void setSavePassword(boolean savePassword) {
342:                this .savePassword = savePassword;
343:            }
344:
345:            /** Getter for property url.
346:             * @return Value of property url.
347:             *
348:             */
349:            public java.lang.String getUrl() {
350:                return url;
351:            }
352:
353:            /** Setter for property url.
354:             * @param url New value of property url.
355:             *
356:             */
357:            public void setUrl(java.lang.String url) {
358:                this .url = url;
359:            }
360:
361:            /** Getter for property username.
362:             * @return Value of property username.
363:             *
364:             */
365:            public java.lang.String getUsername() {
366:                return username;
367:            }
368:
369:            /** Setter for property username.
370:             * @param username New value of property username.
371:             *
372:             */
373:            public void setUsername(java.lang.String username) {
374:                this .username = username;
375:            }
376:
377:            /*
378:             *  This method return all properties used by this connection
379:             */
380:            public java.util.HashMap getProperties() {
381:                java.util.HashMap map = new java.util.HashMap();
382:                map.put("JDBCDriver", Misc.nvl(this .getJDBCDriver(), ""));
383:                map.put("Url", Misc.nvl(this .getUrl(), ""));
384:                map.put("Database", Misc.nvl(this .getDatabase(), ""));
385:                map.put("Username", Misc.nvl(this .getUsername(), ""));
386:                if (this .isSavePassword())
387:                    map.put("Password", Misc.nvl(this .getPassword(), ""));
388:                else
389:                    map.put("Password", "");
390:                map.put("SavePassword", "" + this .isSavePassword());
391:                map.put("ServerAddress", Misc.nvl(this .getServerAddress(), ""));
392:
393:                return map;
394:            }
395:
396:            public void loadProperties(java.util.HashMap map) {
397:                this .setJDBCDriver((String) map.get("JDBCDriver"));
398:                this .setUrl((String) map.get("Url"));
399:                this .setDatabase((String) map.get("Database"));
400:                this .setUsername((String) map.get("Username"));
401:                this .setSavePassword(("" + map.get("SavePassword"))
402:                        .equals("true"));
403:                if (this .isSavePassword())
404:                    this 
405:                            .setPassword(Misc.nvl((String) map.get("Password"),
406:                                    ""));
407:                this .setServerAddress(Misc.nvl((String) map
408:                        .get("ServerAddress"), ""));
409:
410:            }
411:
412:            /**
413:             * Getter for property serverAddress.
414:             * @return Value of property serverAddress.
415:             */
416:            public String getServerAddress() {
417:                return this .serverAddress;
418:            }
419:
420:            /**
421:             * Setter for property serverAddress.
422:             * @param serverAddress New value of property serverAddress.
423:             */
424:            public void setServerAddress(String serverAddress) {
425:                this .serverAddress = serverAddress;
426:            }
427:
428:            public String getDescription() {
429:                return I18n.getString("connectionType.jdbc",
430:                        "Database JDBC connection");
431:            }
432:
433:            public IReportConnectionEditor getIReportConnectionEditor() {
434:                return new JDBCConnectionEditor();
435:            }
436:
437:            public void test() throws Exception {
438:                // Try the java connection...
439:                Connection conn = null;
440:                try {
441:                    conn = getConnection();
442:                    if (conn == null)
443:                        throw new Exception("");
444:                } finally {
445:                    // Clean up
446:                    if (conn != null)
447:                        try {
448:                            conn.close();
449:                        } catch (Exception e) { /* anyone really care? */
450:                        }
451:                }
452:                JOptionPane
453:                        .showMessageDialog(
454:                                MainFrame.getMainInstance(),
455:                                I18n
456:                                        .getString(
457:                                                "messages.connectionDialog.connectionTestSuccessful",
458:                                                "Connection test successful!"),
459:                                "", JOptionPane.INFORMATION_MESSAGE);
460:                return;
461:            }
462:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.