Source Code Cross Referenced for ConnectionProfile.java in  » Database-Client » JSqlTool » org » jsqltool » conn » 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 » Database Client » JSqlTool » org.jsqltool.conn 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.jsqltool.conn;
002:
003:        import java.io.*;
004:        import java.util.*;
005:        import javax.swing.*;
006:        import java.sql.*;
007:        import org.jsqltool.gui.tableplugins.datatable.filter.FilterModel;
008:        import org.jsqltool.utils.Options;
009:
010:        /**
011:         * <p>Title: JSqlTool Project</p>
012:         * <p>Description: Class for load/save a connection profile.
013:         * </p>
014:         * <p>Copyright: Copyright (C) 2006 Mauro Carniel</p>
015:         *
016:         * <p> This file is part of JSqlTool project.
017:         * This library is free software; you can redistribute it and/or
018:         * modify it under the terms of the (LGPL) Lesser General Public
019:         * License as published by the Free Software Foundation;
020:         *
021:         *                GNU LESSER GENERAL PUBLIC LICENSE
022:         *                 Version 2.1, February 1999
023:         *
024:         * This library is distributed in the hope that it will be useful,
025:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
026:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
027:         * Library General Public License for more details.
028:         *
029:         * You should have received a copy of the GNU Library General Public
030:         * License along with this library; if not, write to the Free
031:         * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
032:         *
033:         *       The author may be contacted at:
034:         *           maurocarniel@tin.it</p>
035:         *
036:         * @author Mauro Carniel
037:         * @version 1.0
038:         */
039:        public class ConnectionProfile {
040:
041:            /** max mumber of old queries to store in the connection profile file */
042:            private static final int MAX_OLD_QUERIES = 20;
043:
044:            /**
045:             * Load connection profile file (profile/filename.ini)
046:             */
047:            public void loadProfile(JInternalFrame parent, File file,
048:                    ArrayList conns, Vector connNames) {
049:                try {
050:                    BufferedReader br = new BufferedReader(
051:                            new InputStreamReader(new FileInputStream(file)));
052:                    String line = br.readLine();
053:                    if (line.equals("VERSION 1.0.7")) {
054:                        loadProfileV107(parent, file, conns, connNames);
055:                    } else if (line.equals("VERSION 1.0.3")) {
056:                        loadProfileV103(parent, file, conns, connNames);
057:                    } else
058:                        loadProfileV102(parent, file, conns, connNames);
059:                    br.close();
060:                } catch (Exception ex) {
061:                    ex.printStackTrace();
062:                    JOptionPane
063:                            .showMessageDialog(
064:                                    parent,
065:                                    Options
066:                                            .getInstance()
067:                                            .getResource(
068:                                                    "error on loading connections profile files.")
069:                                            + ":\n" + ex.getMessage(),
070:                                    Options.getInstance().getResource("error"),
071:                                    JOptionPane.ERROR_MESSAGE);
072:                }
073:
074:            }
075:
076:            /**
077:             * Load connection profile file (profile/filename.ini) in JSQLTool <=1.0.2 version.
078:             */
079:            private void loadProfileV102(JInternalFrame parent, File file,
080:                    ArrayList conns, Vector connNames) {
081:                try {
082:                    // load .ini file...
083:                    String line = null;
084:                    int dbType;
085:                    String name = null;
086:                    String driver = null;
087:                    String url = null;
088:                    String username = null;
089:                    String password = null;
090:                    boolean autoCommit;
091:                    int isolationLevel;
092:                    boolean readOnly;
093:                    String catalog = null;
094:                    String tableName = null;
095:                    FilterModel fm = null;
096:                    String whereC = null;
097:                    Hashtable filters = null;
098:                    ArrayList oldQueries = null;
099:                    BufferedReader br = new BufferedReader(
100:                            new InputStreamReader(new FileInputStream(file)));
101:
102:                    // read connection properties...
103:                    dbType = Integer.parseInt(br.readLine());
104:                    name = br.readLine();
105:                    driver = br.readLine();
106:                    url = br.readLine();
107:                    username = br.readLine();
108:                    password = Options.getInstance().decode(br.readLine());
109:                    autoCommit = br.readLine().toLowerCase().equals("true");
110:                    isolationLevel = Integer.parseInt(br.readLine());
111:                    readOnly = br.readLine().toLowerCase().equals("true");
112:                    catalog = br.readLine();
113:
114:                    // skip one row...
115:                    br.readLine();
116:
117:                    // read filters/orderers...
118:                    filters = new Hashtable();
119:                    while (!(line = br.readLine()).equals("")) {
120:                        tableName = line;
121:                        fm = new FilterModel();
122:                        fm.setOrderClause(br.readLine());
123:                        whereC = br.readLine();
124:                        if (whereC.length() > 0)
125:                            whereC = whereC.substring(7);
126:                        fm.setWhereClause(whereC);
127:                        filters.put(tableName, fm);
128:                    }
129:
130:                    // read old queries...
131:                    oldQueries = new ArrayList();
132:                    while ((line = br.readLine()) != null) {
133:                        oldQueries.add(line);
134:                    }
135:                    br.close();
136:
137:                    conns.add(new DbConnection(dbType, name, driver, url,
138:                            username, password, autoCommit, isolationLevel,
139:                            readOnly, catalog, filters, oldQueries, false));
140:                    connNames.add(name);
141:
142:                } catch (Exception ex) {
143:                    ex.printStackTrace();
144:                    JOptionPane
145:                            .showMessageDialog(
146:                                    parent,
147:                                    Options
148:                                            .getInstance()
149:                                            .getResource(
150:                                                    "error on loading connections profile files.")
151:                                            + ":\n" + ex.getMessage(),
152:                                    Options.getInstance().getResource("error"),
153:                                    JOptionPane.ERROR_MESSAGE);
154:                }
155:            }
156:
157:            /**
158:             * Load connection profile file (profile/filename.ini) in JSQLTool >=1.0.3 version.
159:             */
160:            private void loadProfileV103(JInternalFrame parent, File file,
161:                    ArrayList conns, Vector connNames) {
162:                try {
163:                    // load .ini file...
164:                    String line = null;
165:                    int dbType;
166:                    String name = null;
167:                    String driver = null;
168:                    String url = null;
169:                    String username = null;
170:                    boolean autoCommit;
171:                    int isolationLevel;
172:                    boolean readOnly;
173:                    String catalog = null;
174:                    String tableName = null;
175:                    FilterModel fm = null;
176:                    String whereC = null;
177:                    Hashtable filters = null;
178:                    ArrayList oldQueries = null;
179:                    BufferedReader br = new BufferedReader(
180:                            new InputStreamReader(new FileInputStream(file)));
181:
182:                    // read connection properties...
183:                    String iniVersion = br.readLine();
184:                    dbType = Integer.parseInt(br.readLine());
185:                    name = br.readLine();
186:                    driver = br.readLine();
187:                    url = br.readLine();
188:                    username = br.readLine();
189:                    autoCommit = br.readLine().toLowerCase().equals("true");
190:                    isolationLevel = Integer.parseInt(br.readLine());
191:                    readOnly = br.readLine().toLowerCase().equals("true");
192:                    catalog = br.readLine();
193:
194:                    // skip one row...
195:                    br.readLine();
196:
197:                    // read filters/orderers...
198:                    filters = new Hashtable();
199:                    while (!(line = br.readLine()).equals("")) {
200:                        tableName = line;
201:                        fm = new FilterModel();
202:                        fm.setOrderClause(br.readLine());
203:                        whereC = br.readLine();
204:                        if (whereC.length() > 0)
205:                            whereC = whereC.substring(7);
206:                        fm.setWhereClause(whereC);
207:                        filters.put(tableName, fm);
208:                    }
209:
210:                    // read old queries...
211:                    oldQueries = new ArrayList();
212:                    while ((line = br.readLine()) != null) {
213:                        oldQueries.add(line);
214:                    }
215:                    br.close();
216:
217:                    File passwdFile = new File(file.getAbsolutePath()
218:                            .substring(0, file.getAbsolutePath().length() - 4)
219:                            + ".pwd");
220:                    FileInputStream in = new FileInputStream(passwdFile);
221:                    byte[] bb = new byte[(int) passwdFile.length()];
222:                    in.read(bb);
223:                    String password = Options.getInstance().decodeFromBytes(bb);
224:                    in.close();
225:
226:                    conns.add(new DbConnection(dbType, name, driver, url,
227:                            username, password, autoCommit, isolationLevel,
228:                            readOnly, catalog, filters, oldQueries, false));
229:                    connNames.add(name);
230:
231:                } catch (Exception ex) {
232:                    ex.printStackTrace();
233:                    JOptionPane
234:                            .showMessageDialog(
235:                                    parent,
236:                                    Options
237:                                            .getInstance()
238:                                            .getResource(
239:                                                    "error on loading connections profile files.")
240:                                            + ":\n" + ex.getMessage(),
241:                                    Options.getInstance().getResource("error"),
242:                                    JOptionPane.ERROR_MESSAGE);
243:                }
244:            }
245:
246:            /**
247:             * Load connection profile file (profile/filename.ini) in JSQLTool >=1.0.7 version.
248:             */
249:            private void loadProfileV107(JInternalFrame parent, File file,
250:                    ArrayList conns, Vector connNames) {
251:                try {
252:                    // load .ini file...
253:                    String line = null;
254:                    int dbType;
255:                    String name = null;
256:                    String driver = null;
257:                    String url = null;
258:                    String username = null;
259:                    boolean autoCommit;
260:                    int isolationLevel;
261:                    boolean readOnly;
262:                    boolean quotes;
263:                    String catalog = null;
264:                    String tableName = null;
265:                    FilterModel fm = null;
266:                    String whereC = null;
267:                    Hashtable filters = null;
268:                    ArrayList oldQueries = null;
269:                    BufferedReader br = new BufferedReader(
270:                            new InputStreamReader(new FileInputStream(file)));
271:
272:                    // read connection properties...
273:                    String iniVersion = br.readLine();
274:                    dbType = Integer.parseInt(br.readLine());
275:                    name = br.readLine();
276:                    driver = br.readLine();
277:                    url = br.readLine();
278:                    username = br.readLine();
279:                    autoCommit = br.readLine().toLowerCase().equals("true");
280:                    isolationLevel = Integer.parseInt(br.readLine());
281:                    readOnly = br.readLine().toLowerCase().equals("true");
282:                    catalog = br.readLine();
283:                    quotes = br.readLine().toLowerCase().equals("true");
284:
285:                    // skip one row...
286:                    br.readLine();
287:
288:                    // read filters/orderers...
289:                    filters = new Hashtable();
290:                    while (!(line = br.readLine()).equals("")) {
291:                        tableName = line;
292:                        fm = new FilterModel();
293:                        fm.setOrderClause(br.readLine());
294:                        whereC = br.readLine();
295:                        if (whereC.length() > 0)
296:                            whereC = whereC.substring(7);
297:                        fm.setWhereClause(whereC);
298:                        filters.put(tableName, fm);
299:                    }
300:
301:                    // read old queries...
302:                    oldQueries = new ArrayList();
303:                    while ((line = br.readLine()) != null) {
304:                        oldQueries.add(line);
305:                    }
306:                    br.close();
307:
308:                    File passwdFile = new File(file.getAbsolutePath()
309:                            .substring(0, file.getAbsolutePath().length() - 4)
310:                            + ".pwd");
311:                    FileInputStream in = new FileInputStream(passwdFile);
312:                    byte[] bb = new byte[(int) passwdFile.length()];
313:                    in.read(bb);
314:                    String password = Options.getInstance().decodeFromBytes(bb);
315:                    in.close();
316:
317:                    conns.add(new DbConnection(dbType, name, driver, url,
318:                            username, password, autoCommit, isolationLevel,
319:                            readOnly, catalog, filters, oldQueries, quotes));
320:                    connNames.add(name);
321:
322:                } catch (Exception ex) {
323:                    ex.printStackTrace();
324:                    JOptionPane
325:                            .showMessageDialog(
326:                                    parent,
327:                                    Options
328:                                            .getInstance()
329:                                            .getResource(
330:                                                    "error on loading connections profile files.")
331:                                            + ":\n" + ex.getMessage(),
332:                                    Options.getInstance().getResource("error"),
333:                                    JOptionPane.ERROR_MESSAGE);
334:                }
335:            }
336:
337:            public void saveProfile(JFrame parent, DbConnection c,
338:                    boolean isEdit) {
339:                try {
340:                    PrintWriter pw = new PrintWriter(new FileOutputStream(
341:                            new File("profile/" + c.getName().replace(' ', '_')
342:                                    + ".ini")));
343:
344:                    // save connection properties...
345:                    pw.println("VERSION 1.0.7");
346:                    pw.println(c.getDbType());
347:                    pw.println(c.getName());
348:                    pw.println(c.getClassName());
349:                    pw.println(c.getUrl());
350:                    pw.println(c.getUsername());
351:                    pw.println(c.isAutoCommit());
352:                    pw.println(c.getIsolationLevel());
353:                    pw.println(c.isReadOnly());
354:                    pw.println(c.getCatalog());
355:                    pw.println(c.isQuotes());
356:
357:                    // save one empty row...
358:                    pw.println("");
359:
360:                    // save filters/orderers...
361:                    Enumeration en = c.getFilters().keys();
362:                    FilterModel fm = null;
363:                    String tableName = null;
364:                    while (en.hasMoreElements()) {
365:                        tableName = en.nextElement().toString();
366:                        pw.println(tableName);
367:                        fm = (FilterModel) c.getFilters().get(tableName);
368:                        pw.println(fm.getOrderClause());
369:                        pw.println(fm.getWhereClause());
370:                    }
371:
372:                    // save one empty row...
373:                    pw.println("");
374:
375:                    // save old queries...
376:                    while (c.getOldQueries().size() > MAX_OLD_QUERIES)
377:                        c.getOldQueries().remove(0);
378:                    for (int i = 0; i < c.getOldQueries().size(); i++)
379:                        pw.println(c.getOldQueries().get(i));
380:
381:                    pw.close();
382:
383:                    File passwdFile = new File("profile/"
384:                            + c.getName().replace(' ', '_') + ".pwd");
385:
386:                    FileOutputStream out = new FileOutputStream(passwdFile);
387:                    out.write(Options.getInstance().encodeToBytes(
388:                            c.getPassword()));
389:                    out.close();
390:
391:                } catch (Exception ex) {
392:                    ex.printStackTrace();
393:                    JOptionPane
394:                            .showMessageDialog(
395:                                    parent,
396:                                    Options
397:                                            .getInstance()
398:                                            .getResource(
399:                                                    "error on saving connections profile files.")
400:                                            + ":\n" + ex.getMessage(),
401:                                    Options.getInstance().getResource("error"),
402:                                    JOptionPane.ERROR_MESSAGE);
403:                }
404:            }
405:
406:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.