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

📄 transfer.java

📁 Java写的含有一个jdbc驱动的小型数据库数据库引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Transfer.java
 *
 * Copyright (c) 2001, The HSQL Development Group
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 *
 * Neither the name of the HSQL Development Group nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This package is based on HypersonicSQL, originally developed by Thomas Mueller.
 *
 */
package org.hsqldb.util;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.applet.*;
import java.sql.*;
import java.util.*;

/**
 * Class declaration
 *
 *
 * @version 1.0.0.1
 */
public class Transfer extends Applet implements WindowListener,
	ActionListener, ItemListener {
    Connection       cSource, cTarget;
    DatabaseMetaData dSourceMeta, dTargetMeta;
    Statement	     sSourceStatement, sTargetStatement;
    Frame	     fMain;
    Image	     imgEmpty;
    Table	     tCurrent;
    int		     iMaxRows;
    Vector	     tTable;
    java.awt.List    lTable;
    TextField	     tSourceTable, tDestTable;
    TextField	     tDestDrop, tDestCreate, tDestDelete;
    TextField	     tSourceSelect, tDestInsert;
    Checkbox	     cTransfer, cDrop, cCreate, cDelete, cInsert;
    Button	     bStart;
    Hashtable	     hTypes;
    TextField	     tMessage;
    static boolean   bMustExit;

    /**
     * Method declaration
     *
     */
    public void init() {
	Transfer m = new Transfer();

	m.main();
    }

    /**
     * Method declaration
     *
     */
    public static void work() {
	Transfer m = new Transfer();

	m.main();
    }

    /**
     * Method declaration
     *
     *
     * @param arg
     */
    public static void main(String arg[]) {
	bMustExit = true;

	work();
    }

    /**
     * Method declaration
     *
     */
    void main() {
	fMain = new Frame("Hypersonic Transfer Tool");
	imgEmpty = createImage(new MemoryImageSource(2, 2, new int[4 * 4], 2,
		2));

	fMain.setIconImage(imgEmpty);
	fMain.addWindowListener(this);
	fMain.setSize(640, 480);
	fMain.add("Center", this);

	MenuBar bar = new MenuBar();
	String  extras[] = {
	    "Insert 10 rows only", "Insert 1000 rows only", "Insert all rows"
	};
	Menu    menu = new Menu("Options");

	addMenuItems(menu, extras);
	bar.add(menu);
	fMain.setMenuBar(bar);
	initGUI();

	Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
	Dimension size = fMain.getSize();

	fMain.setLocation((d.width - size.width) / 2,
			  (d.height - size.height) / 2);
	fMain.setVisible(true);

	cSource = ConnectionDialog.createConnection(fMain, "Source Database");

	if (cSource == null) {
	    return;
	}

	try {
	    dSourceMeta = cSource.getMetaData();
	    sSourceStatement = cSource.createStatement();
	} catch (Exception e) {
	    trace(e.toString());
	    e.printStackTrace();
	}

	cTarget = ConnectionDialog.createConnection(fMain, "Target Database");

	if (cTarget == null) {
	    return;
	}

	try {
	    dTargetMeta = cTarget.getMetaData();
	    sTargetStatement = cTarget.createStatement();
	} catch (Exception e) {
	    trace(e.toString());
	    e.printStackTrace();
	}

	String usertables[] = {
	    "TABLE"
	};

	lTable.removeAll();

	tTable = new Vector();

	try {
	    hTypes = new Hashtable();

	    trace("Building target type info");

	    ResultSet result = dTargetMeta.getTypeInfo();

	    while (result.next()) {
		hTypes.put(new Integer(result.getShort(2)),
			   result.getString(1));

		// todo: most database need parameters, for example VARCHAR(size)
	    }

	    result.close();
	    trace("Reading source tables");

	    result = dSourceMeta.getTables(null, null, null, usertables);

	    int index = 0;

	    while (result.next()) {
		Table t = new Table();

		t.iTableIndex = index++;

		String name = result.getString(3);

		t.bTransfer = true;
		t.sSourceTable = name;
		t.sDestTable = name;
		t.sDestDrop = "DROP TABLE " + name;
		t.sSourceSelect = "SELECT * FROM " + name;
		t.sDestDelete = "DELETE FROM " + name;
		t.bCreate = true;
		t.bDelete = true;
		t.bDrop = true;
		t.bInsert = true;

		tTable.addElement(t);
		lTable.add(name);
	    }

	    result.close();

	    for (int i = 0; i < tTable.size(); i++) {
		Table t = (Table) tTable.elementAt(i);

		trace("Reading source columns for table " + t.sSourceTable);

		ResultSet col = dSourceMeta.getColumns(null, null,
						       t.sSourceTable, null);
		String    create = "CREATE TABLE " + t.sSourceTable + "(";
		String    insert = "INSERT INTO " + t.sSourceTable
				   + " VALUES(";
		Vector    v = new Vector();

		while (col.next()) {
		    String name = col.getString(4);
		    int    type = col.getShort(5);
		    String source = col.getString(6);

		    // MS SQL 7 specific problems (Northwind database)
		    if (type == 11
			    && source.toUpperCase().equals("DATETIME")) {
			trace("Converted DATETIME (type 11) to TIMESTAMP");

			type = Types.TIMESTAMP;
		    } else if (type == -9
			       && source.toUpperCase().equals("NVARCHAR")) {
			trace("Converted NVARCHAR (type -9) to VARCHAR");

			type = Types.VARCHAR;
		    } else if (type == -8
			       && source.toUpperCase().equals("NCHAR")) {
			trace("Converted NCHAR (type -8) to VARCHAR");

			type = Types.VARCHAR;
		    } else if (type == -10
			       && source.toUpperCase().equals("NTEXT")) {
			trace("Converted NTEXT (type -10) to VARCHAR");

			type = Types.VARCHAR;
		    } else if (type == -1
			       && source.toUpperCase().equals("LONGTEXT")) {
			trace("Converted LONGTEXT (type -1) to LONGVARCHAR");

			type = Types.LONGVARCHAR;
		    }

		    Integer inttype = new Integer(type);
		    String  datatype = (String) hTypes.get(inttype);

		    if (datatype == null) {
			datatype = source;

			trace("No mapping for type: " + inttype
			      + " source type is " + datatype);
		    }

		    v.addElement(inttype);

		    create += name + " " + datatype + ",";
		    insert += "?,";
		}

		col.close();

		create = create.substring(0, create.length() - 1) + ")";
		insert = insert.substring(0, insert.length() - 1) + ")";
		t.sDestCreate = create;
		t.sDestInsert = insert;
		t.iColumnType = new int[v.size()];

		for (int j = 0; j < v.size(); j++) {
		    t.iColumnType[j] = ((Integer) v.elementAt(j)).intValue();
		}
	    }

	    trace("Edit definitions and press [Start Transfer]");
	} catch (SQLException e) {
	    trace("SQL Exception reading Metadata: " + e.getMessage());
	    e.printStackTrace();
	}

	fMain.show();
    }

    /**
     * Method declaration
     *
     *
     * @param f
     * @param m
     */
    void addMenuItems(Menu f, String m[]) {
	for (int i = 0; i < m.length; i++) {
	    MenuItem item = new MenuItem(m[i]);

	    item.addActionListener(this);
	    f.add(item);
	}
    }

    /**
     * Method declaration
     *
     *
     * @param e
     */
    public void itemStateChanged(ItemEvent e) {
	ItemSelectable item = e.getItemSelectable();

	if (item == lTable) {
	    String table = lTable.getSelectedItem();

	    for (int i = 0; i < tTable.size(); i++) {
		Table t = (Table) tTable.elementAt(i);

		if (t != null && t.sSourceTable.equals(table)) {
		    saveTable();
		    displayTable(t);
		    updateEnabled(true);
		}
	    }
	} else {

	    // it must be a checkbox
	    updateEnabled(true);
	}
    }

    /**
     * Method declaration
     *
     */
    void saveTable() {
	if (tCurrent == null) {
	    return;
	}

	Table t = tCurrent;

	t.sSourceTable = tSourceTable.getText();
	t.sDestTable = tDestTable.getText();
	t.sDestDrop = tDestDrop.getText();
	t.sDestCreate = tDestCreate.getText();
	t.sDestDelete = tDestDelete.getText();
	t.sSourceSelect = tSourceSelect.getText();
	t.sDestInsert = tDestInsert.getText();
	t.bTransfer = cTransfer.getState();
	t.bDrop = cDrop.getState();
	t.bCreate = cCreate.getState();
	t.bDelete = cDelete.getState();
	t.bInsert = cInsert.getState();
    }

    /**
     * Method declaration
     *
     *
     * @param t
     */
    void displayTable(Table t) {
	tCurrent = t;

	if (t == null) {
	    return;
	}

	tSourceTable.setText(t.sSourceTable);
	tDestTable.setText(t.sDestTable);
	tDestDrop.setText(t.sDestDrop);
	tDestCreate.setText(t.sDestCreate);
	tDestDelete.setText(t.sDestDelete);
	tSourceSelect.setText(t.sSourceSelect);
	tDestInsert.setText(t.sDestInsert);
	cTransfer.setState(t.bTransfer);
	cDrop.setState(t.bDrop);
	cCreate.setState(t.bCreate);
	cDelete.setState(t.bDelete);
	cInsert.setState(t.bInsert);
    }

    /**
     * Method declaration
     *
     *
     * @param and
     */
    void updateEnabled(boolean and) {
	boolean b = cTransfer.getState();

	tDestTable.setEnabled(and && b);
	tDestDrop.setEnabled(and && b && cDrop.getState());
	tDestCreate.setEnabled(and && b && cCreate.getState());
	tDestDelete.setEnabled(and && b && cDelete.getState());
	tSourceSelect.setEnabled(and && b);
	tDestInsert.setEnabled(and && b && cInsert.getState());
	cDrop.setEnabled(and && b);
	cCreate.setEnabled(and && b);

⌨️ 快捷键说明

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