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

📄 attrstablemodel.java

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

import com.prcomps.cahitarf.Db2Arff;

import javax.swing.table.AbstractTableModel;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.StringTokenizer;

/**
 */
public class AttrsTableModel
        extends AbstractTableModel
{
    public static final String TYPE_NUMERIC = "numeric";
    public static final String TYPE_STRING  = "string";
    public static final String TYPE_CLASS   = "class";

    private Connection connection;
    private ArrayList  attrs = new ArrayList();

    private ResourceBundle bundle = WizardFrame.getBundle();

    public AttrsTableModel()
    {
    }

    public void setConnection( Connection connection )
    {
        this.connection = connection;
    }

    public void loadAttrs()
    {
        attrs.clear();
        String def = null;
        for ( int i=0; (def=WizardFrame.properties.getProperty( Db2Arff.PROP_FIELD_ + i ) )!= null; i++ )
        {
            StringTokenizer st = new StringTokenizer( def, ":" );
            AttrDef attr = new AttrDef();
            if ( st.hasMoreTokens() )
                attr.name = st.nextToken();
            else
                continue;
            if ( st.hasMoreTokens() )
                attr.type = st.nextToken();
            else
                continue;
            if ( st.hasMoreTokens() )
            {
                String q = st.nextToken();
                if ( q.equals("q") )
                    attr.quote = "\"";
                else if ( q.equals( "qs" ) )
                    attr.quote = "\'";
            }
            attrs.add( attr );
        }
        fireTableChanged( null );
    }

    public void executeQuery(String query)
    {

        attrs.clear();
        Statement statement;
        ResultSet resultSet;
        ResultSetMetaData metaData;
        if (connection == null )
        {
            System.err.println("There is no database to execute the query.");
            return;
        }
        try
        {
            statement = connection.createStatement();
            statement.setMaxRows( 1 );
            resultSet = statement.executeQuery(query);
            metaData = resultSet.getMetaData();

            int numberOfColumns =  metaData.getColumnCount();
            // Get the column names and cache them.
            // Then we can close the connection.
            for(int column = 0; column < numberOfColumns; column++)
            {
                AttrDef attr = new AttrDef();
                attr.name = metaData.getColumnLabel(column + 1);
                switch ( metaData.getColumnType( column + 1 ) )
                {
                    case Types.BIGINT:
                    case Types.DECIMAL:
                    case Types.DOUBLE:
                    case Types.FLOAT:
                    case Types.INTEGER:
                    case Types.NUMERIC:
                    case Types.REAL:
                    case Types.SMALLINT:
                    case Types.TINYINT:
                        attr.type = TYPE_NUMERIC;
                        break;
                    default:
                        attr.type = TYPE_CLASS;
                }
                attr.quote = "";
                attrs.add( attr );
            }

            resultSet.close();
            statement.close();
            fireTableChanged(null); // Tell the listeners a new table has arrived.
        }
        catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }

    public void close()
            throws SQLException
    {
        System.out.println("Closing db connection");
        connection.close();
    }

    protected void finalize() throws Throwable
    {
        close();
        super.finalize();
    }

    public ArrayList getAttrs()
    {
        return attrs;
    }

    //////////////////////////////////////////////////////////////////////////
    //
    //             Implementation of the TableModel Interface
    //
    //////////////////////////////////////////////////////////////////////////

    // MetaData

    public String getColumnName(int column)
    {
        if ( column == 0 )
            return bundle.getString( "wizard.attrs.col.name" );
        if ( column == 1 )
            return bundle.getString( "wizard.attrs.col.type" );
        else
            return bundle.getString( "wizard.attrs.col.quote" );
    }

    public Class getColumnClass(int column)
    {
        return String.class;
    }

    public boolean isCellEditable(int row, int column)
    {
        return true;
    }

    public int getColumnCount()
    {
        return 3;
    }

    // Data methods

    public int getRowCount()
    {
        return attrs.size();
    }

    public Object getValueAt( int row, int column )
    {
        AttrDef attr = (AttrDef) attrs.get( row );
        if ( column == 0 )
            return attr.name;
        else if ( column == 1 )
            return attr.type;
        return attr.quote;
    }

    public void setValueAt(Object value, int row, int column)
    {
        AttrDef attr = (AttrDef)attrs.get( row );
        if ( column == 0 )
            attr.name = value.toString();
        else if ( column == 1 )
            attr.type = value.toString();
        else
            attr.quote = value.toString();
    }

    public class AttrDef
    {
        String name;
        String type;
        String quote = "";

        public String toString()
        {
            String def = name + ":" + type;
            if ( quote.equals( "\"") )
                def += ":q";
            else if ( quote.equals( "'") )
                def += ":qs";
            return def;
        }
    }
}

⌨️ 快捷键说明

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