Source Code Cross Referenced for JdbcCar.java in  » Testing » PolePosition-0.20 » org » polepos » teams » jdbc » 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 » Testing » PolePosition 0.20 » org.polepos.teams.jdbc 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* 
002:        This file is part of the PolePosition database benchmark
003:        http://www.polepos.org
004:
005:        This program is free software; you can redistribute it and/or
006:        modify it under the terms of the GNU General Public License
007:        as published by the Free Software Foundation; either version 2
008:        of the License, or (at your option) any later version.
009:
010:        This program is distributed in the hope that it will be useful,
011:        but WITHOUT ANY WARRANTY; without even the implied warranty of
012:        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013:        GNU General Public License for more details.
014:
015:        You should have received a copy of the GNU General Public
016:        License along with this program; if not, write to the Free
017:        Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
018:        MA  02111-1307, USA. */
019:
020:        package org.polepos.teams.jdbc;
021:
022:        import java.sql.*;
023:        import java.util.*;
024:
025:        import org.polepos.framework.*;
026:
027:        /**
028:         * @author Herkules
029:         */
030:        public class JdbcCar extends Car {
031:
032:            private static Connection mConnection;
033:            private static Statement mStatement;
034:
035:            private final String mDBType;
036:            private String mName;
037:
038:            private final static Map<Class, String> mColTypesMap = new HashMap<Class, String>();
039:
040:            static {
041:                mColTypesMap.put(String.class, "VARCHAR(100)");
042:                mColTypesMap.put(Integer.TYPE, "INTEGER");
043:            }
044:
045:            public JdbcCar(String dbtype) throws CarMotorFailureException {
046:
047:                mDBType = dbtype;
048:                mWebsite = Jdbc.settings().getWebsite(mDBType);
049:                mDescription = Jdbc.settings().getDescription(mDBType);
050:                mName = Jdbc.settings().getName(mDBType);
051:
052:                try {
053:                    Class.forName(Jdbc.settings().getDriverClass(mDBType))
054:                            .newInstance();
055:                } catch (Exception e) {
056:                    e.printStackTrace();
057:                    throw new CarMotorFailureException();
058:                }
059:            }
060:
061:            public String name() {
062:                if (mName != null) {
063:                    return mName;
064:                }
065:                return mDBType;
066:            }
067:
068:            public void openConnection() throws CarMotorFailureException {
069:
070:                try {
071:                    assert null == mConnection : "database has to be closed before opening";
072:                    mConnection = DriverManager.getConnection(Jdbc.settings()
073:                            .getConnectUrl(mDBType), Jdbc.settings()
074:                            .getUsername(mDBType), Jdbc.settings().getPassword(
075:                            mDBType));
076:                    mConnection.setAutoCommit(false);
077:                    mStatement = mConnection.createStatement();
078:                } catch (SQLException e) {
079:                    e.printStackTrace();
080:                    throw new CarMotorFailureException();
081:                }
082:            }
083:
084:            /**
085:             *
086:             */
087:            public void closeConnection() {
088:
089:                if (mStatement != null) {
090:                    try {
091:                        mStatement.close();
092:                    } catch (SQLException e) {
093:                        e.printStackTrace();
094:                    }
095:                }
096:
097:                try {
098:                    mConnection.commit();
099:                    mConnection.close();
100:                } catch (SQLException sqlex) {
101:                    sqlex.printStackTrace();
102:                }
103:                mConnection = null;
104:            }
105:
106:            /**
107:             * Commit changes.
108:             */
109:            public void commit() {
110:                try {
111:                    mConnection.commit();
112:                } catch (SQLException ex) {
113:                    ex.printStackTrace();
114:                }
115:            }
116:
117:            /**
118:             * Declarative statement executor
119:             */
120:            public void executeSQL(String sql) {
121:                //      Log.logger.fine( sql );
122:
123:                Statement stmt = null;
124:                try {
125:                    stmt = mConnection.createStatement();
126:                    stmt.execute(sql);
127:                } catch (SQLException ex) {
128:                    ex.printStackTrace();
129:                } finally {
130:                    if (stmt != null) {
131:                        try {
132:                            stmt.close();
133:                        } catch (SQLException sqlEx) {
134:                            stmt = null;
135:                        }
136:                    }
137:                }
138:            }
139:
140:            /**
141:             * Declarative statement executor
142:             */
143:            public ResultSet executeQuery(String sql) {
144:                Log.logger.fine(sql);
145:
146:                Statement stmt = null;
147:                ResultSet rs = null;
148:                try {
149:                    stmt = mConnection.createStatement();
150:                    rs = stmt.executeQuery(sql);
151:                } catch (SQLException ex) {
152:                    ex.printStackTrace();
153:                }
154:                return rs;
155:            }
156:
157:            /**
158:             * Declarative statement executor
159:             */
160:            public ResultSet executeQueryForUpdate(String sql) {
161:                Log.logger.fine(sql);
162:
163:                Statement stmt = null;
164:                ResultSet rs = null;
165:                try {
166:                    stmt = mConnection.createStatement(
167:                            ResultSet.TYPE_FORWARD_ONLY,
168:                            ResultSet.CONCUR_UPDATABLE);
169:                    rs = stmt.executeQuery(sql);
170:                } catch (SQLException ex) {
171:                    ex.printStackTrace();
172:                }
173:                return rs;
174:            }
175:
176:            public void executeUpdate(String sql) {
177:
178:                Log.logger.fine(sql);
179:
180:                try {
181:                    mStatement.executeUpdate(sql);
182:                } catch (SQLException e) {
183:                    e.printStackTrace();
184:                }
185:            }
186:
187:            /**
188:             * Drop a certain table.
189:             */
190:            public void dropTable(String tablename) {
191:                Statement stmt = null;
192:                try {
193:                    stmt = mConnection.createStatement();
194:                    stmt.execute("drop table " + tablename);
195:                } catch (SQLException ex) {
196:                    // intentionally empty
197:                    // don't bother about 'table does not exist'
198:                } finally {
199:                    if (stmt != null) {
200:                        try {
201:                            stmt.close();
202:                        } catch (SQLException sqlEx) {
203:                            stmt = null;
204:                        }
205:                    }
206:                }
207:            }
208:
209:            /**
210:             * Create a new table, use the first column name as the primary key
211:             */
212:            public void createTable(String tablename, String[] colnames,
213:                    Class[] coltypes) {
214:                String sql = "create table " + tablename + " (" + colnames[0]
215:                        + "  INTEGER NOT NULL";
216:
217:                for (int i = 1; i < colnames.length; i++) {
218:                    sql += ", " + colnames[i] + " "
219:                            + mColTypesMap.get(coltypes[i]);
220:                }
221:                sql += ", PRIMARY KEY(" + colnames[0] + "))";
222:
223:                executeSQL(sql);
224:            }
225:
226:            public void createIndex(String tablename, String colname) {
227:                // The maximum length for index names is 18 for Derby.        
228:                String sql = "CREATE INDEX X" + tablename + "_" + colname
229:                        + " ON " + tablename + " (" + colname + ")";
230:                executeSQL(sql);
231:            }
232:
233:            /**
234:             * Retrieve a prepared statement.
235:             */
236:            public PreparedStatement prepareStatement(String sql) {
237:                PreparedStatement stmt = null;
238:                try {
239:                    stmt = mConnection.prepareStatement(sql);
240:                } catch (SQLException ex) {
241:                    ex.printStackTrace();
242:                }
243:                return stmt;
244:            }
245:
246:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.