Package java.io

Examples of java.io.IOException


      query.append(" )");
      //System.out.println(query.toString());
      m_DataBaseConnection.update(query.toString());
      m_DataBaseConnection.close();
      if(!m_DataBaseConnection.tableExists(m_tableName)){
          throw new IOException("Table cannot be built.");
      }
  }
View Full Code Here


            insert.append(", ");
      }
      insert.append(" )");
      //System.out.println(insert.toString());
      if (m_DataBaseConnection.update(insert.toString()) < 1) {
        throw new IOException("Tuple cannot be inserted.");
      }
      else {
  m_DataBaseConnection.close();
      }
  }
View Full Code Here

 
      int writeMode = getWriteMode();
      Instances structure = getInstances();
     
      if(m_DataBaseConnection == null)
           throw new IOException("No database has been set up.");
      if(getRetrieval() == BATCH)
          throw new IOException("Batch and incremental saving cannot be mixed.");
      setRetrieval(INCREMENTAL);
     
      try{
        if(!m_DataBaseConnection.isConnected())
            connectToDatabase();
        if(writeMode == WAIT){
            if(structure == null){
                setWriteMode(CANCEL);
                if(inst != null)
                    throw new Exception("Structure(Header Information) has to be set in advance");
            }
            else
                setWriteMode(STRUCTURE_READY);
            writeMode = getWriteMode();
        }
        if(writeMode == CANCEL){
          cancel();
        }
        if(writeMode == STRUCTURE_READY){
          setWriteMode(WRITE);
          writeStructure();
          writeMode = getWriteMode();
        }
        if(writeMode == WRITE){
          if(structure == null)
              throw new IOException("No instances information available.");
          if(inst != null){
            //write instance
            writeInstance(inst)
          }
          else{
View Full Code Here

   */
  public void writeBatch() throws IOException {
 
      Instances instances = getInstances();
      if(instances == null)
          throw new IOException("No instances to save");
      if(getRetrieval() == INCREMENTAL)
          throw new IOException("Batch and incremental saving cannot be mixed.");
      if(m_DataBaseConnection == null)
           throw new IOException("No database has been set up.");
      setRetrieval(BATCH);
      try{
          if(!m_DataBaseConnection.isConnected())
              connectToDatabase();
          setWriteMode(WRITE);
View Full Code Here

  public void close() throws IOException {
    inStr.close();
    try {
      client.disconnect(true);
    } catch (Throwable t) {
      throw new IOException(t.getMessage());
    }
  }
View Full Code Here

   * @throws IOException if an error occurs
   */
  public Instances getStructure() throws IOException {

    if (m_DataBaseConnection == null) {
      throw new IOException("No source database has been specified");
    }
    connectToDatabase();
  pseudo:
      try{
    if(m_pseudoIncremental && m_structure == null){
        if (getRetrieval() == BATCH) {
            throw new IOException("Cannot mix getting instances in both incremental and batch modes");
        }
        setRetrieval(NONE)
        m_datasetPseudoInc = getDataSet();
        m_structure = new Instances(m_datasetPseudoInc,0);
        setRetrieval(NONE);
        return m_structure;
    }
    if (m_structure == null) {
      if(m_checkForTable) {
        if(!m_DataBaseConnection.tableExists(endOfQuery(true)))
          throw new IOException(
              "Table does not exist according to metadata from JDBC driver. "
              + "If you are convinced the table exists, set 'checkForTable' "
              + "to 'False' in your DatabaseUtils.props file and try again.");
      }
        //finds out which SQL statement to use for the DBMS to limit the number of resulting rows to one
        int choice = 0;
        boolean rightChoice = false;
        while (!rightChoice){
            try{
                if (m_DataBaseConnection.execute(limitQuery(m_query,0,choice)) == false) {
                    throw new IOException("Query didn't produce results");
                }
                m_choice = choice;
                rightChoice = true;
            }
            catch (SQLException ex) {
                choice++;
                if(choice == 3){
                    System.out.println("Incremental loading not supported for that DBMS. Pseudoincremental mode is used if you use incremental loading.\nAll rows are loaded into memory once and retrieved incrementally from memory instead of from the database.");
                    m_pseudoIncremental = true;
                    break pseudo;
                }
            }
        }
        String end = endOfQuery(false);
        ResultSet rs = m_DataBaseConnection.getResultSet();
        ResultSetMetaData md = rs.getMetaData();
//        rs.close();
        int numAttributes = md.getColumnCount();
        int [] attributeTypes = new int [numAttributes];
        m_nominalIndexes = Utils.cast(new Hashtable [numAttributes]);
        m_nominalStrings = Utils.cast(new ArrayList [numAttributes]);
        for (int i = 1; i <= numAttributes; i++) {
            switch (m_DataBaseConnection.translateDBColumnType(md.getColumnTypeName(i))) {
                case DatabaseConnection.STRING :
                    //System.err.println("String --> nominal");
                    ResultSet rs1;
                    String columnName = md.getColumnLabel(i);
                    if(m_DataBaseConnection.getUpperCase())
                        columnName = columnName.toUpperCase();
                    m_nominalIndexes[i - 1] = new Hashtable<String,Double>();
                    m_nominalStrings[i - 1] = new ArrayList<String>();
                    String query = "SELECT COUNT(DISTINCT( "+columnName+" )) FROM " + end;
                    if (m_DataBaseConnection.execute(query) == true){
                        rs1 = m_DataBaseConnection.getResultSet();
                        rs1.next();
                        int count = rs1.getInt(1);
                        rs1.close();
                        //                        if(count > m_nominalToStringLimit || m_DataBaseConnection.execute("SELECT DISTINCT ( "+columnName+" ) FROM "+ end) == false){
                        if(count > m_nominalToStringLimit ||
                           m_DataBaseConnection.execute("SELECT DISTINCT ( "
                                                        + columnName
                                                        + " ) FROM "
                                                        + end
                                                        + " ORDER BY "
                                                        + columnName) == false){
                            attributeTypes[i - 1] = Attribute.STRING;
                            break;
                        }
                        rs1 = m_DataBaseConnection.getResultSet();
                    }
                    else{
                        //System.err.println("Count for nominal values cannot be calculated. Attribute "+columnName+" treated as String.");
                        attributeTypes[i - 1] = Attribute.STRING;
                        break;
                    }
                    attributeTypes[i - 1] = Attribute.NOMINAL;
                    stringToNominal(rs1,i);
                    rs1.close();
                    break;
                case DatabaseConnection.TEXT:
                    //System.err.println("boolean --> string");
                    columnName = md.getColumnLabel(i);
                    if(m_DataBaseConnection.getUpperCase())
                      columnName = columnName.toUpperCase();
                    m_nominalIndexes[i - 1] = new Hashtable<String,Double>();
                    m_nominalStrings[i - 1] = new ArrayList<String>();
                    query = "SELECT COUNT(DISTINCT( "+columnName+" )) FROM " + end;
                    if (m_DataBaseConnection.execute(query) == true){
                      rs1 = m_DataBaseConnection.getResultSet();
                      stringToNominal(rs1,i);
                      rs1.close();
                    }
                    attributeTypes[i - 1] = Attribute.STRING;
                    break;
                case DatabaseConnection.BOOL:
                    //System.err.println("boolean --> nominal");
                    attributeTypes[i - 1] = Attribute.NOMINAL;
                    m_nominalIndexes[i - 1] = new Hashtable<String,Double>();
                    m_nominalIndexes[i - 1].put("false", new Double(0));
                    m_nominalIndexes[i - 1].put("true", new Double(1));
                    m_nominalStrings[i - 1] = new ArrayList<String>();
                    m_nominalStrings[i - 1].add("false");
                    m_nominalStrings[i - 1].add("true");
                    break;
                case DatabaseConnection.DOUBLE:
                    //System.err.println("BigDecimal --> numeric");
                    attributeTypes[i - 1] = Attribute.NUMERIC;
                    break;
                case DatabaseConnection.BYTE:
                    //System.err.println("byte --> numeric");
                    attributeTypes[i - 1] = Attribute.NUMERIC;
                    break;
                case DatabaseConnection.SHORT:
                    //System.err.println("short --> numeric");
                    attributeTypes[i - 1] = Attribute.NUMERIC;
                    break;
                case DatabaseConnection.INTEGER:
                    //System.err.println("int --> numeric");
                    attributeTypes[i - 1] = Attribute.NUMERIC;
                    break;
                case DatabaseConnection.LONG:
                    //System.err.println("long --> numeric");
                    attributeTypes[i - 1] = Attribute.NUMERIC;
                    break;
                case DatabaseConnection.FLOAT:
                    //System.err.println("float --> numeric");
                    attributeTypes[i - 1] = Attribute.NUMERIC;
                    break;
                case DatabaseConnection.DATE:
                    attributeTypes[i - 1] = Attribute.DATE;
                    break;
                case DatabaseConnection.TIME:
                  attributeTypes[i - 1] = Attribute.DATE;
                  break;
                default:
                    //System.err.println("Unknown column type");
                    attributeTypes[i - 1] = Attribute.STRING;
            }
        }
        ArrayList<Attribute> attribInfo = new ArrayList<Attribute>();
        for (int i = 0; i < numAttributes; i++) {
            /* Fix for databases that uppercase column names */
            //String attribName = attributeCaseFix(md.getColumnName(i + 1));
            String attribName = md.getColumnLabel(i + 1);
            switch (attributeTypes[i]) {
                case Attribute.NOMINAL:
                    attribInfo.add(new Attribute(attribName, m_nominalStrings[i]));
                    break;
                case Attribute.NUMERIC:
                    attribInfo.add(new Attribute(attribName));
                    break;
                case Attribute.STRING:
                    Attribute att = new Attribute(attribName, (ArrayList<String>)null);
                    for (int n = 0; n < m_nominalStrings[i].size(); n++) {
                      att.addStringValue((String) m_nominalStrings[i].get(n));
                    }
                    attribInfo.add(att);
                    break;
                case Attribute.DATE:
                    attribInfo.add(new Attribute(attribName, (String)null));
                    break;
                default:
                    throw new IOException("Unknown attribute type");
            }
        }
        m_structure = new Instances(endOfQuery(true), attribInfo,0);
        //get rid of m_idColumn
        if(m_DataBaseConnection.getUpperCase())
View Full Code Here

   * @throws IOException if there is no source or parsing fails
   */
  public Instances getDataSet() throws IOException {

    if (m_DataBaseConnection == null) {
      throw new IOException("No source database has been specified");
    }
    if (getRetrieval() == INCREMENTAL) {
      throw new IOException("Cannot mix getting Instances in both incremental and batch modes");
    }
    setRetrieval(BATCH);
    connectToDatabase();
   
   
    Instances result = null;
    try{
    if (m_DataBaseConnection.execute(m_query) == false)
      throw new Exception("Query didn't produce results");
    ResultSet rs = m_DataBaseConnection.getResultSet();
    ResultSetMetaData md = rs.getMetaData();
    // Determine structure of the instances
    int numAttributes = md.getColumnCount();
    int [] attributeTypes = new int [numAttributes];
    m_nominalIndexes = Utils.cast(new Hashtable [numAttributes]);
    m_nominalStrings = Utils.cast(new ArrayList [numAttributes]);
    for (int i = 1; i <= numAttributes; i++) {
      switch (m_DataBaseConnection.translateDBColumnType(md.getColumnTypeName(i))) {
 
      case DatabaseConnection.STRING :
        ResultSet rs1;
        String columnName = md.getColumnLabel(i);
        if(m_DataBaseConnection.getUpperCase())
            columnName = columnName.toUpperCase();
        String end = endOfQuery(false);
        m_nominalIndexes[i - 1] = new Hashtable<String,Double>();
        m_nominalStrings[i - 1] = new ArrayList<String>();
        if(m_DataBaseConnection.execute("SELECT DISTINCT ( "
                                        + columnName+" ) FROM "
                                        + end
                                        + " ORDER BY "
                                        + columnName) == false){
            throw new Exception("Nominal values cannot be retrieved");
        }
        rs1 = m_DataBaseConnection.getResultSet();
        attributeTypes[i - 1] = Attribute.NOMINAL;
        stringToNominal(rs1,i);
//        rs1.close(); 
  break;
      case DatabaseConnection.TEXT:
        columnName = md.getColumnLabel(i);
        if(m_DataBaseConnection.getUpperCase())
            columnName = columnName.toUpperCase();
        end = endOfQuery(false);
        m_nominalIndexes[i - 1] = new Hashtable<String,Double>();
        m_nominalStrings[i - 1] = new ArrayList<String>();
        if(m_DataBaseConnection.execute("SELECT DISTINCT ( "+columnName+" ) FROM "+ end) == false){
            throw new Exception("Nominal values cannot be retrieved");
        }
        rs1 = m_DataBaseConnection.getResultSet();
        attributeTypes[i - 1] = Attribute.STRING;
        stringToNominal(rs1,i);
        rs1.close()
  break;
      case DatabaseConnection.BOOL:
  //System.err.println("boolean --> nominal");
  attributeTypes[i - 1] = Attribute.NOMINAL;
  m_nominalIndexes[i - 1] = new Hashtable<String,Double>();
  m_nominalIndexes[i - 1].put("false", new Double(0));
  m_nominalIndexes[i - 1].put("true", new Double(1));
  m_nominalStrings[i - 1] = new ArrayList<String>();
  m_nominalStrings[i - 1].add("false");
  m_nominalStrings[i - 1].add("true");
  break;
      case DatabaseConnection.DOUBLE:
  //System.err.println("BigDecimal --> numeric");
  attributeTypes[i - 1] = Attribute.NUMERIC;
  break;
      case DatabaseConnection.BYTE:
  //System.err.println("byte --> numeric");
  attributeTypes[i - 1] = Attribute.NUMERIC;
  break;
      case DatabaseConnection.SHORT:
  //System.err.println("short --> numeric");
  attributeTypes[i - 1] = Attribute.NUMERIC;
  break;
      case DatabaseConnection.INTEGER:
  //System.err.println("int --> numeric");
  attributeTypes[i - 1] = Attribute.NUMERIC;
  break;
      case DatabaseConnection.LONG:
  //System.err.println("long --> numeric");
  attributeTypes[i - 1] = Attribute.NUMERIC;
  break;
      case DatabaseConnection.FLOAT:
  //System.err.println("float --> numeric");
  attributeTypes[i - 1] = Attribute.NUMERIC;
  break;
      case DatabaseConnection.DATE:
  attributeTypes[i - 1] = Attribute.DATE;
  break;
      case DatabaseConnection.TIME:
  attributeTypes[i - 1] = Attribute.DATE;
  break;
      default:
  //System.err.println("Unknown column type");
  attributeTypes[i - 1] = Attribute.STRING;
      }
    }
   
    // For sqlite
    // cache column names because the last while(rs.next()) { iteration for
    // the tuples below will close the md object: 
    Vector<String> columnNames = new Vector<String>();
    for (int i = 0; i < numAttributes; i++) {
      columnNames.add(md.getColumnLabel(i + 1));
    }

    // Step through the tuples
    //System.err.println("Creating instances...");
    ArrayList<Instance> instances = new ArrayList<Instance>();
    while(rs.next()) {
      double[] vals = new double[numAttributes];
      for(int i = 1; i <= numAttributes; i++) {
  switch (m_DataBaseConnection.translateDBColumnType(md.getColumnTypeName(i))) {
  case DatabaseConnection.STRING :
    String str = rs.getString(i);
   
    if (rs.wasNull()) {
      vals[i - 1] = Utils.missingValue();
            } else {
                Double index = (Double)m_nominalIndexes[i - 1].get(str);
                if (index == null) {
                    index = new Double(m_structure.attribute(i-1).addStringValue(str));
                }
                vals[i - 1] = index.doubleValue();
            }
    break;
  case DatabaseConnection.TEXT:
    str = rs.getString(i);

    if (rs.wasNull()) {
      vals[i - 1] = Utils.missingValue();
    }
    else {
      Double index = (Double)m_nominalIndexes[i - 1].get(str);
      if (index == null) {
        index = new Double(m_structure.attribute(i-1).addStringValue(str));
      }
      vals[i - 1] = index.doubleValue();
    }
    break;
  case DatabaseConnection.BOOL:
    boolean boo = rs.getBoolean(i);
    if (rs.wasNull()) {
      vals[i - 1] = Utils.missingValue();
    } else {
      vals[i - 1] = (boo ? 1.0 : 0.0);
    }
    break;
  case DatabaseConnection.DOUBLE:
    double dd = rs.getDouble(i);
    if (rs.wasNull()) {
      vals[i - 1] = Utils.missingValue();
    } else {
      vals[i - 1] =  dd;
    }
    break;
  case DatabaseConnection.BYTE:
    byte by = rs.getByte(i);
    if (rs.wasNull()) {
      vals[i - 1] = Utils.missingValue();
    } else {
      vals[i - 1] = (double)by;
    }
    break;
  case DatabaseConnection.SHORT:
    short sh = rs.getShort(i);
    if (rs.wasNull()) {
      vals[i - 1] = Utils.missingValue();
    } else {
      vals[i - 1] = (double)sh;
    }
    break;
  case DatabaseConnection.INTEGER:
    int in = rs.getInt(i);
    if (rs.wasNull()) {
      vals[i - 1] = Utils.missingValue();
    } else {
      vals[i - 1] = (double)in;
    }
    break;
  case DatabaseConnection.LONG:
    long lo = rs.getLong(i);
    if (rs.wasNull()) {
      vals[i - 1] = Utils.missingValue();
    } else {
      vals[i - 1] = (double)lo;
    }
    break;
  case DatabaseConnection.FLOAT:
    float fl = rs.getFloat(i);
    if (rs.wasNull()) {
      vals[i - 1] = Utils.missingValue();
    } else {
      vals[i - 1] = (double)fl;
    }
    break;
  case DatabaseConnection.DATE:
          Date date = rs.getDate(i);
          if (rs.wasNull()) {
      vals[i - 1] = Utils.missingValue();
    } else {
            // TODO: Do a value check here.
            vals[i - 1] = (double)date.getTime();
          }
          break;
  case DatabaseConnection.TIME:
          Time time = rs.getTime(i);
          if (rs.wasNull()) {
      vals[i - 1] = Utils.missingValue();
    } else {
            // TODO: Do a value check here.
            vals[i - 1] = (double) time.getTime();
          }
          break;
  default:
    vals[i - 1] = Utils.missingValue();
  }
      }
      Instance newInst;
      newInst = new DenseInstance(1.0, vals);
      instances.add(newInst);
    }  
   
    // Create the header and add the instances to the dataset
    //System.err.println("Creating header...");
    ArrayList<Attribute> attribInfo = new ArrayList<Attribute>();
    for (int i = 0; i < numAttributes; i++) {
      /* Fix for databases that uppercase column names */
      //String attribName = attributeCaseFix(md.getColumnName(i + 1));
//      String attribName = md.getColumnName(i + 1);
      String attribName = columnNames.get(i);
      switch (attributeTypes[i]) {
      case Attribute.NOMINAL:
  attribInfo.add(new Attribute(attribName, m_nominalStrings[i]));
  break;
      case Attribute.NUMERIC:
  attribInfo.add(new Attribute(attribName));
  break;
      case Attribute.STRING:
  Attribute att = new Attribute(attribName, (ArrayList<String>) null);
  attribInfo.add(att);
  for (int n = 0; n < m_nominalStrings[i].size(); n++) {
    att.addStringValue((String) m_nominalStrings[i].get(n));
  }
  break;
      case Attribute.DATE:
  attribInfo.add(new Attribute(attribName, (String)null));
  break;
      default:
  throw new IOException("Unknown attribute type");
      }
    }
    result = new Instances(endOfQuery(true), attribInfo,
             instances.size());
    for (int i = 0; i < instances.size(); i++) {
View Full Code Here

  public Instance getNextInstance(Instances structure) throws IOException {

    m_structure = structure;
     
    if (m_DataBaseConnection == null)
      throw new IOException("No source database has been specified");
    if (getRetrieval() == BATCH) {
      throw new IOException("Cannot mix getting Instances in both incremental and batch modes");
    }
    //pseudoInremental: Load all instances into main memory in batch mode and give them incrementally to user
    if(m_pseudoIncremental){
        setRetrieval(INCREMENTAL);
        if(m_datasetPseudoInc.numInstances() > 0){
View Full Code Here

        try{
            File input = new File(inputString);
            loader.setFile(input);
            setInstances(loader.getDataSet());
        } catch(Exception ex){
            throw new IOException("No data set loaded. Data set has to be in ARFF format.");
        }
    }
    if (outputString.length() != 0){
      boolean validExt = false;
      for (String ext: getFileExtensions()) {
  if (outputString.endsWith(ext)) {
    validExt = true;
    break;
  }
      }
      //add appropriate file extension
      if(!validExt){
  if(outputString.lastIndexOf('.') != -1)
    outputString = (outputString.substring(0,outputString.lastIndexOf('.'))) + FILE_EXTENSION;
  else
    outputString = outputString + FILE_EXTENSION;
      }
      try{
  File output = new File(outputString);
  setFile(output);
      } catch(Exception ex) {
  throw new IOException("Cannot create output file (Reason: " + ex.toString() + "). Standard out is used.");
      }
    }
  }
View Full Code Here

    String out = file.getAbsolutePath();
    if(m_outputFile != null){
        try{
            if(file.exists()){
                if(!file.delete())
                    throw new IOException("File already exists.");
            }
            if(out.lastIndexOf(File.separatorChar) == -1){
                success = file.createNewFile();
            }
            else{
                String outPath = out.substring(0,out.lastIndexOf(File.separatorChar));
                File dir = new File(outPath);
                if(dir.exists())
                    success = file.createNewFile();
                else{
                    dir.mkdirs();
                    success = file.createNewFile();
                }
            }
            if(success){
              if (m_useRelativePath) {
                try {
                  m_outputFile = Utils.convertToRelativePath(file);
                } catch (Exception e) {
                  m_outputFile = file;
                }
              } else {
                m_outputFile = file;
              }
              setDestination(new FileOutputStream(m_outputFile));
            }
        } catch(Exception ex){
            throw new IOException("Cannot create a new output file (Reason: " + ex.toString() + "). Standard out is used.");
        } finally{
            if(!success){
                System.err.println("Cannot create a new output file. Standard out is used.");
                m_outputFile = null; //use standard out
            }
View Full Code Here

TOP

Related Classes of java.io.IOException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.