Example usage for weka.experiment InstanceQuery disconnectFromDatabase

List of usage examples for weka.experiment InstanceQuery disconnectFromDatabase

Introduction

In this page you can find the example usage for weka.experiment InstanceQuery disconnectFromDatabase.

Prototype

public void disconnectFromDatabase() throws Exception 

Source Link

Document

Closes the connection to the database.

Usage

From source file:adams.flow.transformer.WekaExperimentEvaluation.java

License:Open Source License

/**
 * Loads the experimental results.//  w  w  w.j  a va  2s  . c o  m
 *
 * @param exp      the experiment to evaluate
 * @return      the results
 * @throws Exception    If reading fails.
 */
protected Instances getData(weka.experiment.Experiment exp) throws Exception {
    Instances result;
    String tmpStr;
    DatabaseResultListener dbListener;
    InstanceQuery query;

    // load experiment data
    if (exp.getResultListener() instanceof CSVResultListener) {
        try {
            result = DataSource
                    .read(((CSVResultListener) exp.getResultListener()).getOutputFile().getAbsolutePath());
        } catch (Exception e) {
            getLogger().log(Level.SEVERE, "Failed to get instances from CSV:", e);
            throw new Exception("Error reading experiment from file:\n" + e);
        }
    } else if (exp.getResultListener() instanceof DatabaseResultListener) {
        dbListener = (DatabaseResultListener) exp.getResultListener();
        try {
            tmpStr = dbListener.getResultsTableName(exp.getResultProducer());
            query = new InstanceQuery();
            query.setDatabaseURL(dbListener.getDatabaseURL());
            query.setUsername(dbListener.getUsername());
            query.setPassword(dbListener.getPassword());
            query.setDebug(dbListener.getDebug());
            query.connectToDatabase();
            result = query.retrieveInstances("SELECT * FROM " + tmpStr);
            query.disconnectFromDatabase();
        } catch (Exception e) {
            getLogger().log(Level.SEVERE, "Failed to get instances from database:", e);
            throw new Exception("Error reading experiment from database:\n" + e);
        }
    } else {
        throw new Exception(
                "Unsupported ResultListener '" + exp.getResultListener().getClass().getName() + "'!");
    }

    return result;
}

From source file:adams.gui.visualization.instance.InstanceExplorer.java

License:Open Source License

/**
 * pops up SQL Viewer for SQL statement.
 */// w w  w. j a  v a2s . co  m
public void loadDataFromDatabase() {
    InstanceQuery query;
    List<InstanceContainer> data;
    Instances dataset;
    Instance inst;
    int i;

    if (m_LoadFromDatabaseDialog == null) {
        if ((getParentFrame() != null) && (getParentFrame() instanceof JFrame))
            m_LoadFromDatabaseDialog = new SqlViewerDialog((JFrame) getParentFrame());
        else
            m_LoadFromDatabaseDialog = new SqlViewerDialog(null);
    }

    m_LoadFromDatabaseDialog.setVisible(true);
    if (m_LoadFromDatabaseDialog.getReturnValue() != JOptionPane.OK_OPTION)
        return;

    try {
        showStatus("Executing query: " + m_LoadFromDatabaseDialog.getQuery());
        query = new InstanceQuery();
        query.setDatabaseURL(m_LoadFromDatabaseDialog.getURL());
        query.setUsername(m_LoadFromDatabaseDialog.getUser());
        query.setPassword(m_LoadFromDatabaseDialog.getPassword());
        query.setQuery(m_LoadFromDatabaseDialog.getQuery());
        query.setSparseData(m_LoadFromDatabaseDialog.getGenerateSparseData());
        if (query.isConnected())
            query.disconnectFromDatabase();
        query.connectToDatabase();

        showStatus("Loading data...");
        data = new ArrayList<>();
        dataset = query.retrieveInstances();
        for (i = 0; i < dataset.numInstances(); i++) {
            inst = new Instance();
            inst.set(dataset.instance(i));
            inst.setID(dataset.relationName() + "." + i);
            data.add(getContainerManager().newContainer(inst));
            showStatus("Loading data " + (i + 1) + "/" + dataset.numInstances());
        }
        loadData(dataset, data);
    } catch (Exception e) {
        GUIHelper.showErrorMessage(this, "Failed to load data from database:\n" + Utils.throwableToString(e),
                "Database error");
    }

    showStatus("");
}