⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 createdb.java

📁 Mandarax是一个规则引擎的纯Java实现。它支持多类型的事实和基于反映的规则
💻 JAVA
字号:
/*
 * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.mandarax.examples.db;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;

/**
 * Create the database table supporting the example. 
 * The example uses a <code>SimpleDataSource</code> to connect to the database. 
 * How to configure a particular database is described in the comment of this class.
 * @see SimpleDataSource
 * @since 2.2
 * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
 * @version 3.4 <7 March 05>
 */
public class CreateDB {
	public static final String[] SCRIPT = {
			"CREATE TABLE CUSTOMER_TRANSACTIONS (ID INT NOT NULL,CUSTOMER VARCHAR(20),AMOUNT DOUBLE PRECISION,TRANSACTION_DATE DATE,PRIMARY KEY (ID))",
			"INSERT INTO CUSTOMER_TRANSACTIONS VALUES (1,'John',99.90,'2000-06-01')",
			"INSERT INTO CUSTOMER_TRANSACTIONS VALUES (2,'John',20.00,'2000-01-05')",
			"INSERT INTO CUSTOMER_TRANSACTIONS VALUES (3,'John',15.00,'1999-01-05')",
			"INSERT INTO CUSTOMER_TRANSACTIONS VALUES (4,'Tom',55.00,'2001-12-08')",
			"INSERT INTO CUSTOMER_TRANSACTIONS VALUES (5,'Tom',99.90,'2000-01-05')",
			"INSERT INTO CUSTOMER_TRANSACTIONS VALUES (6,'Tom',45.00,'2000-04-05')",
			"INSERT INTO CUSTOMER_TRANSACTIONS VALUES (7,'Jim',5.00,'2000-01-05')",
			"INSERT INTO CUSTOMER_TRANSACTIONS VALUES (8,'Jim',12.50,'2001-12-05')",
			"INSERT INTO CUSTOMER_TRANSACTIONS VALUES (9,'Jim',4.50,'2001-12-22')",
			"INSERT INTO CUSTOMER_TRANSACTIONS VALUES (10,'Ralf',32.50,'2000-01-05')",
			"INSERT INTO CUSTOMER_TRANSACTIONS VALUES (11,'Bill',120.00,'2000-01-05')"			
	};
	/**
	 * Create the database.
	 */
	public static void main(String[] args) {
		
		final Properties properties = new Properties();
		try {
			FileInputStream in = new FileInputStream("exampledb.properties");
			properties.load(in);
			in.close();
		}
		catch (Exception x) {
			// no properties found
			System.out.println("File exampledb.properties not found, I will try to create it!");
		}
	
		// build ui
		final TextField txtDriver = new TextField(properties.getProperty("driver",""));
		final TextField txtUrl = new TextField(properties.getProperty("url",""));
		final TextField txtUser = new TextField(properties.getProperty("user",""));
		final TextField txtPassword = new TextField(properties.getProperty("password",""));
	
		Label label = new Label("",Label.RIGHT);
		final Dialog dlg = new Dialog(new Frame());
		dlg.setTitle("Edit database settings");		
		dlg.setLayout(new BorderLayout(5,5));
		Panel form = new Panel(new GridLayout(4,2,5,5));
		form.add(new Label("jdbc driver class:",Label.RIGHT),txtDriver);
		form.add(txtDriver);
		form.add(new Label("database url:",Label.RIGHT),txtUrl);
		form.add(txtUrl);
		form.add(new Label("database user (optional):",Label.RIGHT),txtUser);
		form.add(txtUser);
		form.add(new Label("database password (optional):",Label.RIGHT),txtPassword);
		form.add(txtPassword);
		Panel south = new Panel(new FlowLayout());
		Button okButton = new Button("save settings");
		okButton.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					properties.setProperty("driver",txtDriver.getText());
					properties.setProperty("url",txtUrl.getText());
					properties.setProperty("user",txtUser.getText());
					properties.setProperty("password",txtPassword.getText());
					try {
						FileOutputStream out = new FileOutputStream("exampledb.properties");
						properties.store(out,"mandarax database example properties");
						System.out.println("Settings saved");
					}
					catch (Exception x) {
						x.printStackTrace();
					}
				}
			}
		);
		south.add(okButton);
	
		Button testButton = new Button("test connection");
		testButton.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						Class.forName(txtDriver.getText());
						Connection con = DriverManager.getConnection(
							txtUrl.getText(),
							txtUser.getText(),
							txtPassword.getText()						
						);
						con.close();
						System.out.println("Connection ok");
					}
					catch (Exception x) {
						x.printStackTrace();
					}
				}
			}
		);
		south.add(testButton);
	
		Button createButton = new Button("create tables");
		createButton.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						System.out.println("Creating tables for mandarax db example");
						Class.forName(txtDriver.getText());
						Connection con = DriverManager.getConnection(
							txtUrl.getText(),
							txtUser.getText(),
							txtPassword.getText()						
						);
						Statement stmnt = con.createStatement();
						for (int i=0;i<SCRIPT.length;i++) {
							stmnt.executeUpdate(SCRIPT[i]);
						}
						System.out.println("Example table created and populated with test data.");
					}
					catch (Exception x) {
						System.out.println("Cannot create example db");
						x.printStackTrace();
					}
				}
			}
		);
		south.add(createButton);
	
		Button exitButton = new Button("exit");
		exitButton.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						dlg.dispose();
					}
					catch (Exception x) {
						x.printStackTrace();
					}
				}
			}
		);
		south.add(exitButton);
	
		dlg.add(form,BorderLayout.CENTER);
		dlg.add(south,BorderLayout.SOUTH);
	
		// pop up dialog
		dlg.pack();
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		dlg.setLocation(
			(screen.width-dlg.getWidth())/2,
			(screen.height-dlg.getHeight())/2	
		);
		dlg.show();
	
	
	}

	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -