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: }
|