Source Code Cross Referenced for ShowProgress.java in  » Database-DBMS » h2database » org » h2 » samples » 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.samples 
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.samples;
007:
008:        import java.sql.Connection;
009:        import java.sql.DriverManager;
010:        import java.sql.PreparedStatement;
011:        import java.sql.SQLException;
012:        import java.sql.Statement;
013:
014:        import org.h2.api.DatabaseEventListener;
015:        import org.h2.jdbc.JdbcConnection;
016:
017:        /**
018:         * This example application implements a database event listener.
019:         * This is useful to display progress information while opening a large database,
020:         * or to log database exceptions.
021:         */
022:        public class ShowProgress implements  DatabaseEventListener {
023:
024:            private long last, start;
025:
026:            /**
027:             * Create a new instance of this class, and start the timer.
028:             */
029:            public ShowProgress() {
030:                start = last = System.currentTimeMillis();
031:            }
032:
033:            public static void main(String[] args) throws Exception {
034:                new ShowProgress().test();
035:            }
036:
037:            /**
038:             * Run the progress test.
039:             */
040:            void test() throws Exception {
041:                Class.forName("org.h2.Driver");
042:                Connection conn = DriverManager.getConnection(
043:                        "jdbc:h2:test;LOG=2", "sa", "");
044:                Statement stat = conn.createStatement();
045:                stat.execute("DROP TABLE IF EXISTS TEST");
046:                stat
047:                        .execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)");
048:                PreparedStatement prep = conn
049:                        .prepareStatement("INSERT INTO TEST VALUES(?, 'Test' || SPACE(100))");
050:                long time;
051:                time = System.currentTimeMillis();
052:                int len = 1000;
053:                for (int i = 0; i < len; i++) {
054:                    long last = System.currentTimeMillis();
055:                    if (last > time + 1000) {
056:                        time = last;
057:                        System.out.println("Inserting " + (100L * i / len)
058:                                + "%");
059:                    }
060:                    prep.setInt(1, i);
061:                    prep.execute();
062:                }
063:                boolean abnormalTermination = true;
064:                if (abnormalTermination) {
065:                    ((JdbcConnection) conn).setPowerOffCount(1);
066:                    try {
067:                        stat
068:                                .execute("INSERT INTO TEST VALUES(-1, 'Test' || SPACE(100))");
069:                    } catch (SQLException e) {
070:                    }
071:                } else {
072:                    conn.close();
073:                }
074:
075:                System.out.println("Open connection...");
076:                time = System.currentTimeMillis();
077:                conn = DriverManager.getConnection(
078:                        "jdbc:h2:test;LOG=2;DATABASE_EVENT_LISTENER='"
079:                                + getClass().getName() + "'", "sa", "");
080:                time = System.currentTimeMillis() - time;
081:                System.out.println("Done after " + time + " ms");
082:                conn.close();
083:
084:            }
085:
086:            /**
087:             * This method is called by the database if disk space is low.
088:             *
089:             * @param stillAvailable the number of bytes still available
090:             */
091:            public void diskSpaceIsLow(long stillAvailable) throws SQLException {
092:                System.out.println("diskSpaceIsLow stillAvailable="
093:                        + stillAvailable);
094:            }
095:
096:            /**
097:             * This method is called if an exception occurs in the database.
098:             *
099:             * @param e the exception
100:             * @param sql the SQL statement
101:             */
102:            public void exceptionThrown(SQLException e, String sql) {
103:                System.out.println("Error executing " + sql);
104:                e.printStackTrace();
105:            }
106:
107:            /**
108:             * This method is called when opening the database to notify about the progress.
109:             *
110:             * @param state the current state
111:             * @param name the object name (depends on the state)
112:             * @param current the current progress
113:             * @param max the 100% mark
114:             */
115:            public void setProgress(int state, String name, int current, int max) {
116:                long time = System.currentTimeMillis();
117:                if (time < last + 5000) {
118:                    return;
119:                }
120:                last = time;
121:                String stateName = "?";
122:                switch (state) {
123:                case STATE_SCAN_FILE:
124:                    stateName = "Scan " + name;
125:                    break;
126:                case STATE_CREATE_INDEX:
127:                    stateName = "Create Index " + name;
128:                    break;
129:                case STATE_RECOVER:
130:                    stateName = "Recover";
131:                    break;
132:                }
133:                try {
134:                    Thread.sleep(1);
135:                } catch (InterruptedException e) {
136:                }
137:                System.out.println("State: " + stateName + " "
138:                        + (100 * current / max) + "% (" + current + " of "
139:                        + max + ") " + (time - start) + " ms");
140:            }
141:
142:            /**
143:             * This method is called when the database is closed.
144:             */
145:            public void closingDatabase() {
146:                System.out.println("Closing the database");
147:            }
148:
149:            /**
150:             * This method is called just after creating the instance.
151:             *
152:             * @param url the database URL
153:             */
154:            public void init(String url) {
155:                System.out
156:                        .println("Initializing the event listener for database "
157:                                + url);
158:            }
159:
160:            /**
161:             * This method is called when the database is open.
162:             */
163:            public void opened() {
164:            }
165:
166:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.