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

📄 transfer.java

📁 Java写的含有一个jdbc驱动的小型数据库数据库引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	cDelete.setEnabled(and && b);
	cInsert.setEnabled(and && b);
	bStart.setEnabled(and);
    }

    /**
     * Method declaration
     *
     *
     * @param ev
     */
    public void actionPerformed(ActionEvent ev) {
	String s = ev.getActionCommand();

	if (s == null) {
	    if (ev.getSource() instanceof MenuItem) {
		MenuItem i;

		s = ((MenuItem) ev.getSource()).getLabel();
	    }
	}

	if (s.equals("Start Transfer")) {
	    transfer();
	} else if (s.equals("Insert 10 rows only")) {
	    iMaxRows = 10;
	} else if (s.equals("Insert 1000 rows only")) {
	    iMaxRows = 1000;
	} else if (s.equals("Insert all rows")) {
	    iMaxRows = 0;
	}
    }

    /**
     * Method declaration
     *
     *
     * @param e
     */
    public void windowActivated(WindowEvent e) {}

    /**
     * Method declaration
     *
     *
     * @param e
     */
    public void windowDeactivated(WindowEvent e) {}

    /**
     * Method declaration
     *
     *
     * @param e
     */
    public void windowClosed(WindowEvent e) {}

    /**
     * Method declaration
     *
     *
     * @param ev
     */
    public void windowClosing(WindowEvent ev) {
	try {
	    if (cSource != null) {
		cSource.close();
	    }

	    if (cTarget != null) {
		cTarget.close();
	    }
	} catch (Exception e) {}

	fMain.dispose();

	if (bMustExit) {
	    System.exit(0);
	}
    }

    /**
     * Method declaration
     *
     *
     * @param e
     */
    public void windowDeiconified(WindowEvent e) {}

    /**
     * Method declaration
     *
     *
     * @param e
     */
    public void windowIconified(WindowEvent e) {}

    /**
     * Method declaration
     *
     *
     * @param e
     */
    public void windowOpened(WindowEvent e) {}

    /**
     * Class declaration
     *
     *
     * @author
     * @version %I%, %G%
     */
    class Table {
	int     iTableIndex;
	String  sSourceTable, sDestTable;
	String  sDestDrop, sDestCreate, sDestDelete;
	String  sSourceSelect, sDestInsert;
	boolean bTransfer, bDrop, bCreate, bDelete, bInsert;
	int     iColumnType[];
    }

    /**
     * Method declaration
     *
     */
    void initGUI() {
	Font fFont = new Font("Dialog", Font.PLAIN, 12);

	setLayout(new BorderLayout());

	Panel p = new Panel();

	p.setBackground(SystemColor.control);
	p.setLayout(new GridLayout(16, 1));

	tSourceTable = new TextField();

	tSourceTable.setEnabled(false);

	tDestTable = new TextField();
	tDestDrop = new TextField();
	tDestCreate = new TextField();
	tDestDelete = new TextField();
	tSourceSelect = new TextField();
	tDestInsert = new TextField();
	cTransfer = new Checkbox("Transfer to destination table", true);

	cTransfer.addItemListener(this);

	cDrop = new Checkbox("Drop destination table (ignore error)", true);

	cDrop.addItemListener(this);

	cCreate = new Checkbox("Create destination table", true);

	cCreate.addItemListener(this);

	cDelete = new Checkbox("Delete rows in destination table", true);

	cDelete.addItemListener(this);

	cInsert = new Checkbox("Insert into destination", true);

	cInsert.addItemListener(this);
	p.add(createLabel("Source table"));
	p.add(tSourceTable);
	p.add(cTransfer);
	p.add(tDestTable);
	p.add(cDrop);
	p.add(tDestDrop);
	p.add(cCreate);
	p.add(tDestCreate);
	p.add(cDelete);
	p.add(tDestDelete);
	p.add(createLabel("Select source records"));
	p.add(tSourceSelect);
	p.add(cInsert);
	p.add(tDestInsert);
	p.add(createLabel(""));

	bStart = new Button("Start Transfer");

	bStart.addActionListener(this);
	p.add(bStart);
	fMain.add("Center", createBorderPanel(p));

	lTable = new java.awt.List(10);

	lTable.addItemListener(this);
	fMain.add("West", createBorderPanel(lTable));

	tMessage = new TextField();

	Panel pMessage = createBorderPanel(tMessage);

	fMain.add("South", pMessage);
    }

    /**
     * Method declaration
     *
     *
     * @param center
     *
     * @return
     */
    Panel createBorderPanel(Component center) {
	Panel p = new Panel();

	p.setBackground(SystemColor.control);
	p.setLayout(new BorderLayout());
	p.add("Center", center);
	p.add("South", createLabel(""));
	p.add("East", createLabel(""));
	p.add("West", createLabel(""));
	p.setBackground(SystemColor.control);

	return p;
    }

    /**
     * Method declaration
     *
     *
     * @param s
     *
     * @return
     */
    Label createLabel(String s) {
	Label l = new Label(s);

	l.setBackground(SystemColor.control);

	return l;
    }

    /**
     * Method declaration
     *
     *
     * @param s
     */
    void trace(String s) {
	tMessage.setText(s);
	System.out.println(s);
    }

    /**
     * Method declaration
     *
     */
    void transfer() {
	saveTable();
	updateEnabled(false);
	trace("Start Transfer");

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

	    trace("Transfer finished successfully");
	} catch (SQLException e) {
	    String last = tMessage.getText();

	    trace("Transfer stopped - " + last + " / " + sLast + " / Error: "
		  + e.getMessage());
	    e.printStackTrace();
	} catch (Exception e) {
	    String last = tMessage.getText();

	    trace("Transfer stopped - " + last + " / " + sLast + " / Error: "
		  + e.getMessage());
	    e.printStackTrace();
	}

	updateEnabled(true);
    }

    /**
     * Method declaration
     *
     *
     * @param t
     *
     * @throws SQLException
     */
    void transfer(Table t) throws SQLException {
	lTable.select(t.iTableIndex);
	displayTable(t);

	if (t.bTransfer == false) {
	    trace("Table " + t.sSourceSelect + " not transfered");

	    return;
	}

	trace("Table " + t.sSourceSelect + ": start transfer");

	if (t.bDelete) {
	    try {
		trace("Executing " + t.sDestDrop);
		sTargetStatement.execute(t.sDestDrop);
	    } catch (SQLException e) {
		trace("Ignoring error " + e.getMessage());
	    }
	}

	if (t.bCreate) {
	    trace("Executing " + t.sDestCreate);
	    sTargetStatement.execute(t.sDestCreate);
	}

	if (t.bDelete) {
	    trace("Executing " + t.sDestDelete);
	    sTargetStatement.execute(t.sDestDelete);
	}

	if (t.bInsert) {
	    trace("Executing " + t.sDestInsert);

	    PreparedStatement p = cTarget.prepareStatement(t.sDestInsert);

	    trace("Executing " + t.sSourceSelect);

	    ResultSet	      r =
		sSourceStatement.executeQuery(t.sSourceSelect);
	    int		      i = 0;
	    ResultSetMetaData m = r.getMetaData();
	    int		      type[] = new int[m.getColumnCount()];

	    for (int j = 0; j < m.getColumnCount(); j++) {
		type[j] = m.getColumnType(j + 1);
	    }

	    trace("Start transfering data...");

	    while (r.next()) {
		transferRow(type, r, p);

		if (iMaxRows != 0 && i == iMaxRows) {
		    break;
		}

		i++;

		if (iMaxRows != 0 || i % 100 == 0) {
		    trace("Transfered " + i + " rows");
		}
	    }

	    trace("Finished");
	}
    }

    String sLast;

    /**
     * Method declaration
     *
     *
     * @param type
     * @param r
     * @param p
     *
     * @throws SQLException
     */
    void transferRow(int type[], ResultSet r,
		     PreparedStatement p) throws SQLException {
	sLast = null;

	p.clearParameters();

	int len = type.length;

	for (int i = 0; i < len; i++) {
	    int t = type[i];

	    sLast = "column=" + (i + 1) + " datatype=" + t;

	    Object o = r.getObject(i + 1);

	    if (o == null) {
		sLast += " value=" + o;

		p.setNull(i + 1, type[i]);
	    } else {

		// solves a problem for MS SQL 7
		if (t == Types.SMALLINT && o instanceof Integer) {
		    sLast += " SMALLINT: Converted Integer to Short";
		    o = new Short((short) ((Integer) o).intValue());
		} else if (t == Types.TINYINT && o instanceof Integer) {
		    sLast += " TINYINT: Converted Integer to Byte";
		    o = new Byte((byte) ((Integer) o).intValue());
		}

		sLast += " value=" + o;

		p.setObject(i + 1, o, type[i]);
	    }
	}

	p.execute();

	sLast = null;
    }

}

⌨️ 快捷键说明

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