/** * Queries a database to load results from the specified table name. The database connection must * have already made by m_InstanceQuery. * * @param tableName the name of the table containing results to retrieve. */ protected void setInstancesFromDatabaseTable(String tableName) { try { m_FromLab.setText("Reading from database, please wait..."); final Instances i = m_InstanceQuery.retrieveInstances("SELECT * FROM " + tableName); SwingUtilities.invokeAndWait( new Runnable() { public void run() { setInstances(i); } }); m_InstanceQuery.disconnectFromDatabase(); } catch (Exception ex) { m_FromLab.setText(ex.getMessage()); } }
/** * Test the class from the command line. The instance query should be specified with -Q sql_query * * @param args contains options for the instance query */ public static void main(String args[]) { try { InstanceQuery iq = new InstanceQuery(); String query = Utils.getOption('Q', args); if (query.length() == 0) { iq.setQuery("select * from Experiment_index"); } else { iq.setQuery(query); } iq.setOptions(args); try { Utils.checkForRemainingOptions(args); } catch (Exception e) { System.err.println("Options for weka.experiment.InstanceQuery:\n"); Enumeration en = iq.listOptions(); while (en.hasMoreElements()) { Option o = (Option) en.nextElement(); System.err.println(o.synopsis() + "\n" + o.description()); } System.exit(1); } Instances aha = iq.retrieveInstances(); iq.disconnectFromDatabase(); // query returned no result -> exit if (aha == null) return; // The dataset may be large, so to make things easier we'll // output an instance at a time (rather than having to convert // the entire dataset to one large string) System.out.println(new Instances(aha, 0)); for (int i = 0; i < aha.numInstances(); i++) { System.out.println(aha.instance(i)); } } catch (Exception e) { e.printStackTrace(); System.err.println(e.getMessage()); } }
/** Queries the user enough to make a database query to retrieve experiment results. */ protected void setInstancesFromDBaseQuery() { try { if (m_InstanceQuery == null) { m_InstanceQuery = new InstanceQuery(); } String dbaseURL = m_InstanceQuery.getDatabaseURL(); String username = m_InstanceQuery.getUsername(); String passwd = m_InstanceQuery.getPassword(); /*dbaseURL = (String) JOptionPane.showInputDialog(this, "Enter the database URL", "Query Database", JOptionPane.PLAIN_MESSAGE, null, null, dbaseURL);*/ DatabaseConnectionDialog dbd = new DatabaseConnectionDialog(null, dbaseURL, username); dbd.setVisible(true); // if (dbaseURL == null) { if (dbd.getReturnValue() == JOptionPane.CLOSED_OPTION) { m_FromLab.setText("Cancelled"); return; } dbaseURL = dbd.getURL(); username = dbd.getUsername(); passwd = dbd.getPassword(); m_InstanceQuery.setDatabaseURL(dbaseURL); m_InstanceQuery.setUsername(username); m_InstanceQuery.setPassword(passwd); m_InstanceQuery.setDebug(dbd.getDebug()); m_InstanceQuery.connectToDatabase(); if (!m_InstanceQuery.experimentIndexExists()) { System.err.println("not found"); m_FromLab.setText("No experiment index"); m_InstanceQuery.disconnectFromDatabase(); return; } System.err.println("found"); m_FromLab.setText("Getting experiment index"); Instances index = m_InstanceQuery.retrieveInstances("SELECT * FROM " + InstanceQuery.EXP_INDEX_TABLE); if (index.numInstances() == 0) { m_FromLab.setText("No experiments available"); m_InstanceQuery.disconnectFromDatabase(); return; } m_FromLab.setText("Got experiment index"); DefaultListModel lm = new DefaultListModel(); for (int i = 0; i < index.numInstances(); i++) { lm.addElement(index.instance(i).toString()); } JList jl = new JList(lm); jl.setSelectedIndex(0); int result; // display dialog only if there's not just one result! if (jl.getModel().getSize() != 1) { ListSelectorDialog jd = new ListSelectorDialog(null, jl); result = jd.showDialog(); } else { result = ListSelectorDialog.APPROVE_OPTION; } if (result != ListSelectorDialog.APPROVE_OPTION) { m_FromLab.setText("Cancelled"); m_InstanceQuery.disconnectFromDatabase(); return; } Instance selInst = index.instance(jl.getSelectedIndex()); Attribute tableAttr = index.attribute(InstanceQuery.EXP_RESULT_COL); String table = InstanceQuery.EXP_RESULT_PREFIX + selInst.toString(tableAttr); setInstancesFromDatabaseTable(table); } catch (Exception ex) { // 1. print complete stacktrace ex.printStackTrace(); // 2. print message in panel m_FromLab.setText("Problem reading database: '" + ex.getMessage() + "'"); } }