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

📄 formdb.java

📁 weka机器学习系统(本站可下载)的拓展
💻 JAVA
字号:
package com.prcomps.cahitarf.gui;

import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;

import com.sohlman.easylayout.EasyLayout;
import com.sohlman.easylayout.Constraint;
import com.prcomps.cahitarf.Db2Arff;

import java.util.ResourceBundle;
import java.util.Iterator;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.SQLException;

/**
 */
public class FormDb
        extends JPanel
        implements IWizardPanel
{
    private JTextField txtRelation   = new JTextField();
    private JComboBox  txtJdbcDriver = new JComboBox();
    private JComboBox  txtJdbcUrl    = new JComboBox();
    private JTextField txtJdbcUser   = new JTextField();
    private JPasswordField txtJdbcPassword = new JPasswordField(  );

    private ResourceBundle bundle = WizardFrame.getBundle();

    private boolean tested = false;

    public FormDb()
    {
        createForm();
        setName( WizardFrame.FORM_DB );
    }

    private void createForm()
    {
        txtJdbcDriver.setEditable( true );
        txtJdbcUrl.setEditable( true );
        loadPrefs();

        setLayout( new EasyLayout( new int[]{ 10, 20, 60, 10 }, new int[] { 80, 0, 0, 0, 0, 10, 0, 10 }, 0, 8 ) );

        add( new JLabel( bundle.getString( "wizard.db.relation") ), new Constraint( 1,0 ) );
        add( txtRelation, new Constraint( 2, 0, Constraint.FULL, Constraint.CENTER, 0, 0  ) );

        add( new JLabel( bundle.getString( "wizard.db.driver" ) ), new Constraint( 1, 1 ) );
        add( txtJdbcDriver, new Constraint( 2, 1, Constraint.FULL, Constraint.CENTER, 0, 0 ) );

        add( new JLabel( bundle.getString( "wizard.db.url" ) ), new Constraint( 1, 2 ) );
        add( txtJdbcUrl, new Constraint( 2, 2, Constraint.FULL, Constraint.CENTER, 0, 0  ) );

        add( new JLabel( bundle.getString( "wizard.db.user" ) ), new Constraint( 1, 3 ) );
        add( txtJdbcUser, new Constraint( 2, 3, Constraint.FULL, Constraint.CENTER, 0, 0 ) );

        add( new JLabel( bundle.getString( "wizard.db.password") ), new Constraint( 1, 4 ) );
        add( txtJdbcPassword, new Constraint( 2, 4, Constraint.FULL, Constraint.CENTER, 0, 0 ) );

        JButton btnTest = new JButton( bundle.getString( "wizard.db.btntest" ) );
        add( btnTest, new Constraint( 2, 6, Constraint.RIGHT, Constraint.CENTER, 0, 0 ) );
        btnTest.addActionListener( new ActionListener()
        {
            public void actionPerformed( ActionEvent e )
            {
                testConnection();
            }
        });
    }

    private void testConnection()
    {
        Db2Arff db2Arff = new Db2Arff();
        saveProperties();
        db2Arff.setProperties( WizardFrame.properties );
        try
        {
            db2Arff.loadDrivers();
            Connection conn = db2Arff.connect();
            tested = true;
            JOptionPane.showMessageDialog( this, bundle.getString("wizard.db.conn.ok" ), "", JOptionPane.DEFAULT_OPTION );
        }
        catch ( FileNotFoundException e )
        {
            JOptionPane.showMessageDialog( this, e.getMessage(),
                    bundle.getString("wizard.db.conn.fail"), JOptionPane.DEFAULT_OPTION );
        }
        catch ( SQLException e )
        {
            JOptionPane.showMessageDialog( this, e.getMessage(),
                    bundle.getString("wizard.db.conn.fail"), JOptionPane.DEFAULT_OPTION );
        }
        catch ( ClassNotFoundException e )
        {
            JOptionPane.showMessageDialog( this, e.getMessage(),
                    bundle.getString("wizard.db.conn.fail"), JOptionPane.DEFAULT_OPTION );
        }
    }

    public String getNext()
            throws NotReadyException
    {
        String next = getName();
        if ( ! tested )
        {
            int answer = JOptionPane.showConfirmDialog( this, bundle.getString("wizard.db.conn.continue" ),
                    "", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE  );
            if ( answer == JOptionPane.YES_OPTION )
                next = WizardFrame.FORM_SELECT;
        }
        else
            next = WizardFrame.FORM_SELECT;
        saveProperties();
        return next;
    }

    public String getPrev()
            throws NotReadyException
    {
        saveProperties();
        return null;
    }

    public void checkStatus()
    {
        txtRelation.setText( WizardFrame.properties.getProperty( Db2Arff.PROP_RELATION ) );
        txtJdbcDriver.setSelectedItem( WizardFrame.properties.getProperty( Db2Arff.PROP_JDBC_DRIVER ) );
        txtJdbcUrl.setSelectedItem( WizardFrame.properties.getProperty( Db2Arff.PROP_JDBC_URL ) );
        txtJdbcUser.setText( WizardFrame.properties.getProperty( Db2Arff.PROP_JDBC_USER ) );
        txtJdbcPassword.setText( WizardFrame.properties.getProperty( Db2Arff.PROP_JDBC_PASSWORD ) );
    }

    private void saveProperties()
    {
        WizardFrame.properties.setProperty( Db2Arff.PROP_RELATION, txtRelation.getText() );
        WizardFrame.properties.setProperty( Db2Arff.PROP_JDBC_DRIVER, txtJdbcDriver.getSelectedItem().toString() );
        WizardFrame.properties.setProperty( Db2Arff.PROP_JDBC_URL, txtJdbcUrl.getSelectedItem().toString() );
        WizardFrame.properties.setProperty( Db2Arff.PROP_JDBC_USER, txtJdbcUser.getText() );
        WizardFrame.properties.setProperty( Db2Arff.PROP_JDBC_PASSWORD, new String( txtJdbcPassword.getPassword() ) );
        WizardFrame.prefsJdbcDrivers.add( txtJdbcDriver.getSelectedItem() );
        WizardFrame.prefsJdbcUrls.add( txtJdbcUrl.getSelectedItem() );
        tested = false;
    }

    private void loadPrefs()
    {
        for ( Iterator i = WizardFrame.prefsJdbcDrivers.iterator(); i.hasNext(); )
            txtJdbcDriver.addItem( i.next() );
        for ( Iterator i = WizardFrame.prefsJdbcUrls.iterator(); i.hasNext(); )
            txtJdbcUrl.addItem( i.next() );

    }
}

⌨️ 快捷键说明

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