Source Code Cross Referenced for CreateDB.java in  » Rule-Engine » Mandarax » org » mandarax » examples » db » 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 » Rule Engine » Mandarax » org.mandarax.examples.db 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003:         *
004:         * This library is free software; you can redistribute it and/or
005:         * modify it under the terms of the GNU Lesser General Public
006:         * License as published by the Free Software Foundation; either
007:         * version 2 of the License, or (at your option) any later version.
008:         *
009:         * This library is distributed in the hope that it will be useful,
010:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
011:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012:         * Lesser General Public License for more details.
013:         *
014:         * You should have received a copy of the GNU Lesser General Public
015:         * License along with this library; if not, write to the Free Software
016:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
017:         */
018:        package org.mandarax.examples.db;
019:
020:        import java.io.*;
021:        import java.awt.*;
022:        import java.awt.event.*;
023:        import java.sql.*;
024:        import java.util.*;
025:
026:        /**
027:         * Create the database table supporting the example. 
028:         * The example uses a <code>SimpleDataSource</code> to connect to the database. 
029:         * How to configure a particular database is described in the comment of this class.
030:         * @see SimpleDataSource
031:         * @since 2.2
032:         * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
033:         * @version 3.4 <7 March 05>
034:         */
035:        public class CreateDB {
036:            public static final String[] SCRIPT = {
037:                    "CREATE TABLE CUSTOMER_TRANSACTIONS (ID INT NOT NULL,CUSTOMER VARCHAR(20),AMOUNT DOUBLE PRECISION,TRANSACTION_DATE DATE,PRIMARY KEY (ID))",
038:                    "INSERT INTO CUSTOMER_TRANSACTIONS VALUES (1,'John',99.90,'2000-06-01')",
039:                    "INSERT INTO CUSTOMER_TRANSACTIONS VALUES (2,'John',20.00,'2000-01-05')",
040:                    "INSERT INTO CUSTOMER_TRANSACTIONS VALUES (3,'John',15.00,'1999-01-05')",
041:                    "INSERT INTO CUSTOMER_TRANSACTIONS VALUES (4,'Tom',55.00,'2001-12-08')",
042:                    "INSERT INTO CUSTOMER_TRANSACTIONS VALUES (5,'Tom',99.90,'2000-01-05')",
043:                    "INSERT INTO CUSTOMER_TRANSACTIONS VALUES (6,'Tom',45.00,'2000-04-05')",
044:                    "INSERT INTO CUSTOMER_TRANSACTIONS VALUES (7,'Jim',5.00,'2000-01-05')",
045:                    "INSERT INTO CUSTOMER_TRANSACTIONS VALUES (8,'Jim',12.50,'2001-12-05')",
046:                    "INSERT INTO CUSTOMER_TRANSACTIONS VALUES (9,'Jim',4.50,'2001-12-22')",
047:                    "INSERT INTO CUSTOMER_TRANSACTIONS VALUES (10,'Ralf',32.50,'2000-01-05')",
048:                    "INSERT INTO CUSTOMER_TRANSACTIONS VALUES (11,'Bill',120.00,'2000-01-05')" };
049:
050:            /**
051:             * Create the database.
052:             */
053:            public static void main(String[] args) {
054:
055:                final Properties properties = new Properties();
056:                try {
057:                    FileInputStream in = new FileInputStream(
058:                            "exampledb.properties");
059:                    properties.load(in);
060:                    in.close();
061:                } catch (Exception x) {
062:                    // no properties found
063:                    System.out
064:                            .println("File exampledb.properties not found, I will try to create it!");
065:                }
066:
067:                // build ui
068:                final TextField txtDriver = new TextField(properties
069:                        .getProperty("driver", ""));
070:                final TextField txtUrl = new TextField(properties.getProperty(
071:                        "url", ""));
072:                final TextField txtUser = new TextField(properties.getProperty(
073:                        "user", ""));
074:                final TextField txtPassword = new TextField(properties
075:                        .getProperty("password", ""));
076:
077:                Label label = new Label("", Label.RIGHT);
078:                final Dialog dlg = new Dialog(new Frame());
079:                dlg.setTitle("Edit database settings");
080:                dlg.setLayout(new BorderLayout(5, 5));
081:                Panel form = new Panel(new GridLayout(4, 2, 5, 5));
082:                form.add(new Label("jdbc driver class:", Label.RIGHT),
083:                        txtDriver);
084:                form.add(txtDriver);
085:                form.add(new Label("database url:", Label.RIGHT), txtUrl);
086:                form.add(txtUrl);
087:                form.add(new Label("database user (optional):", Label.RIGHT),
088:                        txtUser);
089:                form.add(txtUser);
090:                form
091:                        .add(new Label("database password (optional):",
092:                                Label.RIGHT), txtPassword);
093:                form.add(txtPassword);
094:                Panel south = new Panel(new FlowLayout());
095:                Button okButton = new Button("save settings");
096:                okButton.addActionListener(new ActionListener() {
097:                    public void actionPerformed(ActionEvent e) {
098:                        properties.setProperty("driver", txtDriver.getText());
099:                        properties.setProperty("url", txtUrl.getText());
100:                        properties.setProperty("user", txtUser.getText());
101:                        properties.setProperty("password", txtPassword
102:                                .getText());
103:                        try {
104:                            FileOutputStream out = new FileOutputStream(
105:                                    "exampledb.properties");
106:                            properties.store(out,
107:                                    "mandarax database example properties");
108:                            System.out.println("Settings saved");
109:                        } catch (Exception x) {
110:                            x.printStackTrace();
111:                        }
112:                    }
113:                });
114:                south.add(okButton);
115:
116:                Button testButton = new Button("test connection");
117:                testButton.addActionListener(new ActionListener() {
118:                    public void actionPerformed(ActionEvent e) {
119:                        try {
120:                            Class.forName(txtDriver.getText());
121:                            Connection con = DriverManager.getConnection(txtUrl
122:                                    .getText(), txtUser.getText(), txtPassword
123:                                    .getText());
124:                            con.close();
125:                            System.out.println("Connection ok");
126:                        } catch (Exception x) {
127:                            x.printStackTrace();
128:                        }
129:                    }
130:                });
131:                south.add(testButton);
132:
133:                Button createButton = new Button("create tables");
134:                createButton.addActionListener(new ActionListener() {
135:                    public void actionPerformed(ActionEvent e) {
136:                        try {
137:                            System.out
138:                                    .println("Creating tables for mandarax db example");
139:                            Class.forName(txtDriver.getText());
140:                            Connection con = DriverManager.getConnection(txtUrl
141:                                    .getText(), txtUser.getText(), txtPassword
142:                                    .getText());
143:                            Statement stmnt = con.createStatement();
144:                            for (int i = 0; i < SCRIPT.length; i++) {
145:                                stmnt.executeUpdate(SCRIPT[i]);
146:                            }
147:                            System.out
148:                                    .println("Example table created and populated with test data.");
149:                        } catch (Exception x) {
150:                            System.out.println("Cannot create example db");
151:                            x.printStackTrace();
152:                        }
153:                    }
154:                });
155:                south.add(createButton);
156:
157:                Button exitButton = new Button("exit");
158:                exitButton.addActionListener(new ActionListener() {
159:                    public void actionPerformed(ActionEvent e) {
160:                        try {
161:                            dlg.dispose();
162:                        } catch (Exception x) {
163:                            x.printStackTrace();
164:                        }
165:                    }
166:                });
167:                south.add(exitButton);
168:
169:                dlg.add(form, BorderLayout.CENTER);
170:                dlg.add(south, BorderLayout.SOUTH);
171:
172:                // pop up dialog
173:                dlg.pack();
174:                Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
175:                dlg.setLocation((screen.width - dlg.getWidth()) / 2,
176:                        (screen.height - dlg.getHeight()) / 2);
177:                dlg.show();
178:
179:            }
180:
181:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.