public Instances retrieveInstances(String query) throws Exception {  System.err.println("Executing query: " + query); connectToDatabase(); if (execute(query) == false) { throw new Exception("Query didn't produce results"); } ResultSet rs = getResultSet(); System.err.println("Getting metadata..."); ResultSetMetaData md = rs.getMetaData();   int numAttributes = md.getColumnCount(); int [] attributeTypes = new int [numAttributes]; Hashtable [] nominalIndexes = new Hashtable [numAttributes]; FastVector [] nominalStrings = new FastVector [numAttributes]; for (int i = 1; i <= numAttributes; i++) { switch (md.getColumnType(i)) { case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: case Types.BINARY: case Types.VARBINARY: case Types.LONGVARBINARY:  attributeTypes[i - 1] = Attribute.NOMINAL; nominalIndexes[i - 1] = new Hashtable(); nominalStrings[i - 1] = new FastVector(); break; case Types.BIT:  attributeTypes[i - 1] = Attribute.NOMINAL; nominalIndexes[i - 1] = new Hashtable(); nominalIndexes[i - 1].put("false", new Double(0)); nominalIndexes[i - 1].put("true", new Double(1)); nominalStrings[i - 1] = new FastVector(); nominalStrings[i - 1].addElement("false"); nominalStrings[i - 1].addElement("true"); break; case Types.NUMERIC: case Types.DECIMAL:  attributeTypes[i - 1] = Attribute.NUMERIC; break; case Types.TINYINT:  attributeTypes[i - 1] = Attribute.NUMERIC; break; case Types.SMALLINT:  attributeTypes[i - 1] = Attribute.NUMERIC; break; case Types.INTEGER:  attributeTypes[i - 1] = Attribute.NUMERIC; break; case Types.BIGINT:  attributeTypes[i - 1] = Attribute.NUMERIC; break; case Types.REAL:  attributeTypes[i - 1] = Attribute.NUMERIC; break; case Types.FLOAT: case Types.DOUBLE:  attributeTypes[i - 1] = Attribute.NUMERIC; break;  case Types.DATE:  attributeTypes[i - 1] = Attribute.STRING; break; case Types.TIME:  attributeTypes[i - 1] = Attribute.STRING; break; case Types.TIMESTAMP:  attributeTypes[i - 1] = Attribute.STRING; break; default:  attributeTypes[i - 1] = Attribute.STRING; } }   System.err.println("Creating instances..."); FastVector instances = new FastVector(); int rowCount = 0; while(rs.next()) { if (rowCount % 100 == 0) { System.err.print("read " + rowCount + " instances \r"); System.err.flush(); } double[] vals = new double[numAttributes]; for(int i = 1; i <= numAttributes; i++) { switch (md.getColumnType(i)) { case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: case Types.BINARY: case Types.VARBINARY: case Types.LONGVARBINARY: String str = rs.getString(i);  if (rs.wasNull()) { vals[i - 1] = Instance.missingValue(); } else { Double index = (Double)nominalIndexes[i - 1].get(str); if (index == null) { index = new Double(nominalStrings[i - 1].size()); nominalIndexes[i - 1].put(str, index); nominalStrings[i - 1].addElement(str); } vals[i - 1] = index.doubleValue(); } break; case Types.BIT: boolean boo = rs.getBoolean(i); if (rs.wasNull()) { vals[i - 1] = Instance.missingValue(); } else { vals[i - 1] = (boo ? 1.0 : 0.0); } break; case Types.NUMERIC: case Types.DECIMAL:  double dd = rs.getDouble(i);  if (rs.wasNull()) { vals[i - 1] = Instance.missingValue(); } else {  vals[i - 1] =  dd; } break; case Types.TINYINT: byte by = rs.getByte(i); if (rs.wasNull()) { vals[i - 1] = Instance.missingValue(); } else { vals[i - 1] = (double)by; } break; case Types.SMALLINT: short sh = rs.getByte(i); if (rs.wasNull()) { vals[i - 1] = Instance.missingValue(); } else { vals[i - 1] = (double)sh; } break; case Types.INTEGER: int in = rs.getInt(i); if (rs.wasNull()) { vals[i - 1] = Instance.missingValue(); } else { vals[i - 1] = (double)in; } break; case Types.BIGINT: long lo = rs.getLong(i); if (rs.wasNull()) { vals[i - 1] = Instance.missingValue(); } else { vals[i - 1] = (double)lo; } break; case Types.REAL: float fl = rs.getFloat(i); if (rs.wasNull()) { vals[i - 1] = Instance.missingValue(); } else { vals[i - 1] = (double)fl; } break; case Types.FLOAT: case Types.DOUBLE: double dou = rs.getDouble(i); if (rs.wasNull()) { vals[i - 1] = Instance.missingValue(); } else { vals[i - 1] = (double)dou; } break;  case Types.DATE: case Types.TIME: case Types.TIMESTAMP: default: vals[i - 1] = Instance.missingValue(); } } Instance newInst; if (m_CreateSparseData) { newInst = new SparseInstance(1.0, vals); } else { newInst = new Instance(1.0, vals); } instances.addElement(newInst); rowCount++; } rs.close();    System.err.println("Creating header..."); FastVector attribInfo = new FastVector(); for (int i = 0; i < numAttributes; i++) { String attribName = md.getColumnName(i + 1); switch (attributeTypes[i]) { case Attribute.NOMINAL: attribInfo.addElement(new Attribute(attribName, nominalStrings[i])); break; case Attribute.NUMERIC: attribInfo.addElement(new Attribute(attribName)); break; case Attribute.STRING: attribInfo.addElement(new Attribute(attribName, null)); break; default: throw new Exception("Unknown attribute type"); } } Instances result = new Instances("QueryResult", attribInfo, instances.size()); for (int i = 0; i < instances.size(); i++) { result.add((Instance)instances.elementAt(i)); } return result;
public Instances retrieveInstances() throws Exception { return retrieveInstances(m_Query);
  public Instances retrieveInstances(String query) throws Exception {    System.err.println("Executing query: " + query);    connectToDatabase();    if (execute(query) == false) {      throw new Exception("Query didn't produce results");    }    ResultSet rs = getResultSet();    System.err.println("Getting metadata...");    ResultSetMetaData md = rs.getMetaData();    // Determine structure of the instances    int numAttributes = md.getColumnCount();    int [] attributeTypes = new int [numAttributes];    Hashtable [] nominalIndexes = new Hashtable [numAttributes];    FastVector [] nominalStrings = new FastVector [numAttributes];    for (int i = 1; i <= numAttributes; i++) {      switch (md.getColumnType(i)) {      case Types.CHAR:      case Types.VARCHAR:      case Types.LONGVARCHAR:      case Types.BINARY:      case Types.VARBINARY:      case Types.LONGVARBINARY:	//System.err.println("String --> nominal");	attributeTypes[i - 1] = Attribute.NOMINAL;	nominalIndexes[i - 1] = new Hashtable();	nominalStrings[i - 1] = new FastVector();	break;      case Types.BIT:	////System.err.println("boolean --> nominal");	attributeTypes[i - 1] = Attribute.NOMINAL;	nominalIndexes[i - 1] = new Hashtable();	nominalIndexes[i - 1].put("false", new Double(0));	nominalIndexes[i - 1].put("true", new Double(1));	nominalStrings[i - 1] = new FastVector();	nominalStrings[i - 1].addElement("false");	nominalStrings[i - 1].addElement("true");	break;      case Types.NUMERIC:      case Types.DECIMAL:	//System.err.println("BigDecimal --> numeric");	attributeTypes[i - 1] = Attribute.NUMERIC;	break;      case Types.TINYINT:	//System.err.println("byte --> numeric");	attributeTypes[i - 1] = Attribute.NUMERIC;	break;      case Types.SMALLINT:	//System.err.println("short --> numeric");	attributeTypes[i - 1] = Attribute.NUMERIC;	break;      case Types.INTEGER:	//System.err.println("int --> numeric");	attributeTypes[i - 1] = Attribute.NUMERIC;	break;      case Types.BIGINT:	//System.err.println("long --> numeric");	attributeTypes[i - 1] = Attribute.NUMERIC;	break;      case Types.REAL:	//System.err.println("float --> numeric");	attributeTypes[i - 1] = Attribute.NUMERIC;	break;      case Types.FLOAT:      case Types.DOUBLE:	//System.err.println("double --> numeric");	attributeTypes[i - 1] = Attribute.NUMERIC;	break;	/*case Types.BINARY:      case Types.VARBINARY:      case Types.LONGVARBINARY:	//System.err.println("byte[] --> unsupported");	attributeTypes[i - 1] = Attribute.STRING;	break; */      case Types.DATE:	//System.err.println("Date --> unsupported");	attributeTypes[i - 1] = Attribute.STRING;	break;      case Types.TIME:	//System.err.println("Time --> unsupported");	attributeTypes[i - 1] = Attribute.STRING;	break;      case Types.TIMESTAMP:	//System.err.println("Time --> unsupported");	attributeTypes[i - 1] = Attribute.STRING;	break;      default:	//System.err.println("Unknown column type");	attributeTypes[i - 1] = Attribute.STRING;      }    }    // Step through the tuples    System.err.println("Creating instances...");    FastVector instances = new FastVector();    int rowCount = 0;    while(rs.next()) {      if (rowCount % 100 == 0) {	System.err.print("read " + rowCount + " instances \r");	System.err.flush();      }      double[] vals = new double[numAttributes];      for(int i = 1; i <= numAttributes; i++) {	switch (md.getColumnType(i)) {	case Types.CHAR:	case Types.VARCHAR:	case Types.LONGVARCHAR:	case Types.BINARY:	case Types.VARBINARY:	case Types.LONGVARBINARY:	  String str = rs.getString(i);	  	  if (rs.wasNull()) {	    vals[i - 1] = Instance.missingValue();	  } else {	    Double index = (Double)nominalIndexes[i - 1].get(str);	    if (index == null) {	      index = new Double(nominalStrings[i - 1].size());	      nominalIndexes[i - 1].put(str, index);	      nominalStrings[i - 1].addElement(str);	    }	    vals[i - 1] = index.doubleValue();	  }	  break;	case Types.BIT:	  boolean boo = rs.getBoolean(i);	  if (rs.wasNull()) {	    vals[i - 1] = Instance.missingValue();	  } else {	    vals[i - 1] = (boo ? 1.0 : 0.0);	  }	  break;	case Types.NUMERIC:	case Types.DECIMAL:	  //	  BigDecimal bd = rs.getBigDecimal(i, 4); 	  double dd = rs.getDouble(i);	  // Use the column precision instead of 4?	  if (rs.wasNull()) {	    vals[i - 1] = Instance.missingValue();	  } else {	    //	    newInst.setValue(i - 1, bd.doubleValue());	    vals[i - 1] =  dd;	  }	  break;	case Types.TINYINT:	  byte by = rs.getByte(i);	  if (rs.wasNull()) {	    vals[i - 1] = Instance.missingValue();	  } else {	    vals[i - 1] = (double)by;	  }	  break;	case Types.SMALLINT:	  short sh = rs.getByte(i);	  if (rs.wasNull()) {	    vals[i - 1] = Instance.missingValue();	  } else {	    vals[i - 1] = (double)sh;	  }	  break;	case Types.INTEGER:	  int in = rs.getInt(i);	  if (rs.wasNull()) {	    vals[i - 1] = Instance.missingValue();	  } else {	    vals[i - 1] = (double)in;	  }	  break;	case Types.BIGINT:	  long lo = rs.getLong(i);	  if (rs.wasNull()) {	    vals[i - 1] = Instance.missingValue();	  } else {	    vals[i - 1] = (double)lo;	  }	  break;	case Types.REAL:	  float fl = rs.getFloat(i);	  if (rs.wasNull()) {	    vals[i - 1] = Instance.missingValue();	  } else {	    vals[i - 1] = (double)fl;	  }	  break;	case Types.FLOAT:	case Types.DOUBLE:	  double dou = rs.getDouble(i);	  if (rs.wasNull()) {	    vals[i - 1] = Instance.missingValue();	  } else {	    vals[i - 1] = (double)dou;	  }	  break;	  /*case Types.BINARY:	case Types.VARBINARY:	case Types.LONGVARBINARY: */	case Types.DATE:	case Types.TIME:	case Types.TIMESTAMP:	default:	  vals[i - 1] = Instance.missingValue();	}      }      Instance newInst;      if (m_CreateSparseData) {	newInst = new SparseInstance(1.0, vals);      } else {	newInst = new Instance(1.0, vals);      }      instances.addElement(newInst);      rowCount++;    }    rs.close();    //disconnectFromDatabase();  (perhaps other queries might be made)        // Create the header and add the instances to the dataset    System.err.println("Creating header...");    FastVector attribInfo = new FastVector();    for (int i = 0; i < numAttributes; i++) {      String attribName = md.getColumnName(i + 1);      switch (attributeTypes[i]) {      case Attribute.NOMINAL:	attribInfo.addElement(new Attribute(attribName, nominalStrings[i]));	break;      case Attribute.NUMERIC:	attribInfo.addElement(new Attribute(attribName));	break;      case Attribute.STRING:	attribInfo.addElement(new Attribute(attribName, null));	break;      default:	throw new Exception("Unknown attribute type");      }    }    Instances result = new Instances("QueryResult", attribInfo, 				     instances.size());    for (int i = 0; i < instances.size(); i++) {      result.add((Instance)instances.elementAt(i));    }    return result;  }