Source Code Cross Referenced for DbConnection.java in  » Database-DBMS » h2database » org » h2 » test » synth » sql » 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 DBMS » h2database » org.h2.test.synth.sql 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003:         * (license2)
004:         * Initial Developer: H2 Group
005:         */
006:        package org.h2.test.synth.sql;
007:
008:        import java.sql.Connection;
009:        import java.sql.DatabaseMetaData;
010:        import java.sql.DriverManager;
011:        import java.sql.ResultSet;
012:        import java.sql.SQLException;
013:        import java.sql.Statement;
014:        import java.util.ArrayList;
015:
016:        /**
017:         * Represents a connection to a real database.
018:         */
019:        class DbConnection implements  DbInterface {
020:            private TestSynth config;
021:            private int id;
022:            private String driver;
023:            private String url;
024:            private String user;
025:            private String password;
026:            private Connection conn;
027:            private Connection sentinel;
028:            private boolean useSentinel;
029:
030:            DbConnection(TestSynth config, String driver, String url,
031:                    String user, String password, int id, boolean useSentinel) {
032:                this .config = config;
033:                this .driver = driver;
034:                this .url = url;
035:                this .user = user;
036:                this .password = password;
037:                this .id = id;
038:                this .useSentinel = useSentinel;
039:                log("url=" + url);
040:            }
041:
042:            public void reset() throws SQLException {
043:                log("reset;");
044:                DatabaseMetaData meta = conn.getMetaData();
045:                Statement stat = conn.createStatement();
046:                ArrayList tables = new ArrayList();
047:                ResultSet rs = meta.getTables(null, null, null,
048:                        new String[] { "TABLE" });
049:                while (rs.next()) {
050:                    String schemaName = rs.getString("TABLE_SCHEM");
051:                    if (!"INFORMATION_SCHEMA".equals(schemaName)) {
052:                        tables.add(rs.getString("TABLE_NAME"));
053:                    }
054:                }
055:                while (tables.size() > 0) {
056:                    int dropped = 0;
057:                    for (int i = 0; i < tables.size(); i++) {
058:                        try {
059:                            String table = (String) tables.get(i);
060:                            stat.execute("DROP TABLE " + table);
061:                            dropped++;
062:                            tables.remove(i);
063:                            i--;
064:                        } catch (SQLException e) {
065:                            // maybe a referential integrity
066:                        }
067:                    }
068:                    // could not drop any table and still tables to drop
069:                    if (dropped == 0 && tables.size() > 0) {
070:                        throw new Error("Cannot drop " + tables);
071:                    }
072:                }
073:            }
074:
075:            public void connect() throws Exception {
076:                if (useSentinel && sentinel == null) {
077:                    sentinel = getConnection();
078:                }
079:                log("connect to " + url + ";");
080:                conn = getConnection();
081:            }
082:
083:            private Connection getConnection() throws Exception {
084:                log("(getConnection to " + url + ");");
085:                if (driver == null) {
086:                    return config.getConnection("synth");
087:                } else {
088:                    Class.forName(driver);
089:                    return DriverManager.getConnection(url, user, password);
090:                }
091:            }
092:
093:            public void disconnect() throws SQLException {
094:                log("disconnect " + url + ";");
095:                conn.close();
096:            }
097:
098:            public void end() throws SQLException {
099:                log("end " + url + ";");
100:                if (sentinel != null) {
101:                    sentinel.close();
102:                    sentinel = null;
103:                }
104:            }
105:
106:            public void createTable(Table table) throws SQLException {
107:                execute(table.getCreateSQL());
108:            }
109:
110:            public void dropTable(Table table) throws SQLException {
111:                execute(table.getDropSQL());
112:            }
113:
114:            public void createIndex(Index index) throws SQLException {
115:                execute(index.getCreateSQL());
116:                index.getTable().addIndex(index);
117:            }
118:
119:            public void dropIndex(Index index) throws SQLException {
120:                execute(index.getDropSQL());
121:                index.getTable().removeIndex(index);
122:            }
123:
124:            public Result insert(Table table, Column[] c, Value[] v)
125:                    throws SQLException {
126:                String sql = table.getInsertSQL(c, v);
127:                execute(sql);
128:                return new Result(sql, 1);
129:            }
130:
131:            private void execute(String sql) throws SQLException {
132:                log(sql + ";");
133:                conn.createStatement().execute(sql);
134:            }
135:
136:            public Result select(String sql) throws SQLException {
137:                log(sql + ";");
138:                Statement stat = conn.createStatement();
139:                Result result = new Result(config, sql, stat.executeQuery(sql));
140:                return result;
141:            }
142:
143:            public Result delete(Table table, String condition)
144:                    throws SQLException {
145:                String sql = "DELETE FROM " + table.getName();
146:                if (condition != null) {
147:                    sql += "  WHERE " + condition;
148:                }
149:                log(sql + ";");
150:                Statement stat = conn.createStatement();
151:                Result result = new Result(sql, stat.executeUpdate(sql));
152:                return result;
153:            }
154:
155:            public Result update(Table table, Column[] columns, Value[] values,
156:                    String condition) throws SQLException {
157:                String sql = "UPDATE " + table.getName() + " SET ";
158:                for (int i = 0; i < columns.length; i++) {
159:                    if (i > 0) {
160:                        sql += ", ";
161:                    }
162:                    sql += columns[i].getName() + "=" + values[i].getSQL();
163:                }
164:                if (condition != null) {
165:                    sql += "  WHERE " + condition;
166:                }
167:                log(sql + ";");
168:                Statement stat = conn.createStatement();
169:                Result result = new Result(sql, stat.executeUpdate(sql));
170:                return result;
171:            }
172:
173:            public void setAutoCommit(boolean b) throws SQLException {
174:                log("set autoCommit " + b + ";");
175:                conn.setAutoCommit(b);
176:            }
177:
178:            public void commit() throws SQLException {
179:                log("commit;");
180:                conn.commit();
181:            }
182:
183:            public void rollback() throws SQLException {
184:                log("rollback;");
185:                conn.rollback();
186:            }
187:
188:            private void log(String s) {
189:                config.log(id, s);
190:            }
191:
192:            public String toString() {
193:                return url;
194:            }
195:
196:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.