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

📄 databaseloader.java

📁 矩阵的QR分解算法
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
	    vals[i - 1] = index.doubleValue();	  }	  break;	case DatabaseConnection.BOOL:	  boolean boo = rs.getBoolean(i);	  if (rs.wasNull()) {	    vals[i - 1] = Instance.missingValue();	  } else {	    vals[i - 1] = (boo ? 1.0 : 0.0);	  }	  break;	case DatabaseConnection.DOUBLE:	  //	  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 DatabaseConnection.BYTE:	  byte by = rs.getByte(i);	  if (rs.wasNull()) {	    vals[i - 1] = Instance.missingValue();	  } else {	    vals[i - 1] = (double)by;	  }	  break;	case DatabaseConnection.SHORT:	  short sh = rs.getByte(i);	  if (rs.wasNull()) {	    vals[i - 1] = Instance.missingValue();	  } else {	    vals[i - 1] = (double)sh;	  }	  break;	case DatabaseConnection.INTEGER:	  int in = rs.getInt(i);	  if (rs.wasNull()) {	    vals[i - 1] = Instance.missingValue();	  } else {	    vals[i - 1] = (double)in;	  }	  break;	case DatabaseConnection.LONG:	  long lo = rs.getLong(i);	  if (rs.wasNull()) {	    vals[i - 1] = Instance.missingValue();	  } else {	    vals[i - 1] = (double)lo;	  }	  break;	case DatabaseConnection.FLOAT:	  float fl = rs.getFloat(i);	  if (rs.wasNull()) {	    vals[i - 1] = Instance.missingValue();	  } else {	    vals[i - 1] = (double)fl;	  }	  break;	case DatabaseConnection.DATE:          Date date = rs.getDate(i);          if (rs.wasNull()) {	    vals[i - 1] = Instance.missingValue();	  } else {            // TODO: Do a value check here.            vals[i - 1] = (double)date.getTime();          }          break;	default:	  vals[i - 1] = Instance.missingValue();	}      }       Instance inst = new Instance(1.0, vals);       //get rid of m_idColumn       if(m_DataBaseConnection.getUpperCase())              m_idColumn = m_idColumn.toUpperCase();       if(m_structure.attribute(0).name().equals(m_idColumn)){            inst.deleteAttributeAt(0);            m_oldStructure.add(inst);            inst = m_oldStructure.instance(0);            m_oldStructure.delete(0);       }       else{        //instances is added to and deleted from the structure to get the true nominal values instead of the index of the values.        m_structure.add(inst);        inst = m_structure.instance(0);        m_structure.delete(0);       }       return inst;         }  /**   * Read the data set incrementally---get the next instance in the data    * set or returns null if there are no   * more instances to get. If the structure hasn't yet been    * determined by a call to getStructure then method does so before   * returning the next instance in the data set.   *   * @param structure the dataset header information, will get updated in    * case of string or relational attributes   * @return the next instance in the data set as an Instance object or null   * if there are no more instances to be read   * @throws IOException if there is an error during parsing   */  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){            Instance current = m_datasetPseudoInc.instance(0);            m_datasetPseudoInc.delete(0);            return current;        }        else{            resetStructure();            return null;        }    }    //real incremental mode. At the moment(version 1.0) only for MySQL and HSQLDB (Postgres not tested, should work)    setRetrieval(INCREMENTAL);    try{        if(!m_DataBaseConnection.isConnected())            connectToDatabase();        //if no key columns specified by user, try to detect automatically        if(m_firstTime && m_orderBy.size() == 0){            if(!checkForKey())                throw new Exception("A unique order cannot be detected automatically.\nYou have to use SELECT * in your query to enable this feature.\nMaybe JDBC driver is not able to detect key.\nDefine primary key in your database or use -P option (command line) or enter key columns in the GUI.");        }        if(m_firstTime){            m_firstTime = false;            m_rowCount = getRowCount();        }        //as long as not all rows has been loaded        if(m_counter < m_rowCount){            if (m_DataBaseConnection.execute(limitQuery(m_query,m_counter,m_choice)) == false) {                throw new Exception("Tuple could not be retrieved.");            }            m_counter++;            ResultSet rs = m_DataBaseConnection.getResultSet();            rs.next();            Instance current = readInstance(rs);            rs.close();            return current;        }        else{            m_DataBaseConnection.disconnectFromDatabase();            resetStructure();            return null;        }    }catch(Exception ex) {        printException(ex);    }    return null;  }        /**    * Gets the setting   *    * @return the current setting   */    public String[] getOptions() {          Vector options = new Vector();    if ( (getUrl() != null) && (getUrl().length() != 0) ) {      options.add("-url");      options.add(getUrl());    }        if ( (getUser() != null) && (getUser().length() != 0) ) {      options.add("-user");      options.add(getUser());    }        if ( (getPassword() != null) && (getPassword().length() != 0) ) {      options.add("-password");      options.add(getPassword());    }    options.add("-Q");     options.add(getQuery());        StringBuffer text = new StringBuffer();    for (int i = 0; i < m_orderBy.size(); i++) {      if (i > 0)        text.append(", ");      text.append((String) m_orderBy.elementAt(i));    }    options.add("-P");     options.add(text.toString());        if (m_inc)      options.add("-I");        return (String[]) options.toArray(new String[options.size()]);  }    /**    * Lists the available options   *    * @return an enumeration of the available options   */    public java.util.Enumeration listOptions() {           FastVector newVector = new FastVector();     newVector.addElement(new Option(           "\tThe JDBC URL to connect to.\n"           + "\t(default: from DatabaseUtils.props file)",           "url", 1, "-url <JDBC URL>"));          newVector.addElement(new Option(           "\tThe user to connect with to the database.\n"           + "\t(default: none)",           "user", 1, "-user <name>"));          newVector.addElement(new Option(           "\tThe password to connect with to the database.\n"           + "\t(default: none)",           "password", 1, "-password <password>"));          newVector.addElement(new Option(	 "\tSQL query of the form\n"	 + "\t\tSELECT <list of columns>|* FROM <table> [WHERE]\n"	 + "\tto execute.\n"         + "\t(default: Select * From Results0)",	 "Q",1,"-Q <query>"));          newVector.addElement(new Option(	 "\tList of column names uniquely defining a DB row\n"	 + "\t(separated by ', ').\n"         + "\tUsed for incremental loading.\n"	 + "\tIf not specified, the key will be determined automatically,\n"	 + "\tif possible with the used JDBC driver.\n"	 + "\tThe auto ID column created by the DatabaseSaver won't be loaded.",	 "P",1,"-P <list of column names>"));     newVector.addElement(new Option(	 "\tSets incremental loading", 	 "I", 0, "-I"));          return  newVector.elements();  }    /**    * Sets the options.   *   <!-- options-start -->   * Valid options are: <p/>   *    * <pre> -url &lt;JDBC URL&gt;   *  The JDBC URL to connect to.   *  (default: from DatabaseUtils.props file)</pre>   *    * <pre> -user &lt;name&gt;   *  The user to connect with to the database.   *  (default: none)</pre>   *    * <pre> -password &lt;password&gt;   *  The password to connect with to the database.   *  (default: none)</pre>   *    * <pre> -Q &lt;query&gt;   *  SQL query of the form   *   SELECT &lt;list of columns&gt;|* FROM &lt;table&gt; [WHERE]   *  to execute.   *  (default: Select * From Results0)</pre>   *    * <pre> -P &lt;list of column names&gt;   *  List of column names uniquely defining a DB row   *  (separated by ', ').   *  Used for incremental loading.   *  If not specified, the key will be determined automatically,   *  if possible with the used JDBC driver.   *  The auto ID column created by the DatabaseSaver won't be loaded.</pre>   *    * <pre> -I   *  Sets incremental loading</pre>   *    <!-- options-end -->   *   * @param options the options   * @throws Exception if options cannot be set   */    public void setOptions(String[] options) throws Exception {          String optionString, keyString, tmpStr;        optionString = Utils.getOption('Q', options);        keyString = Utils.getOption('P', options);        reset();        tmpStr = Utils.getOption("url", options);    if (tmpStr.length() != 0)      setUrl(tmpStr);    tmpStr = Utils.getOption("user", options);    if (tmpStr.length() != 0)      setUser(tmpStr);        tmpStr = Utils.getOption("password", options);    if (tmpStr.length() != 0)      setPassword(tmpStr);        if (optionString.length() != 0)      setQuery(optionString);        m_orderBy.removeAllElements();        m_inc = Utils.getFlag('I', options);        if(m_inc){        StringTokenizer st = new StringTokenizer(keyString, ",");        while (st.hasMoreTokens()) {            String column = st.nextToken();            column = column.replaceAll(" ","");            m_orderBy.addElement(column);        }    }  }    /**Prints an exception   * @param ex the exception to print   */    private void printException(Exception ex){        System.out.println("\n--- Exception caught ---\n");	while (ex != null) {		System.out.println("Message:   "                                   + ex.getMessage ());                if(ex instanceof SQLException){                    System.out.println("SQLState:  "                                   + ((SQLException)ex).getSQLState ());                    System.out.println("ErrorCode: "                                   + ((SQLException)ex).getErrorCode ());                    ex = ((SQLException)ex).getNextException();                }                else                    ex = null;		System.out.println("");	}              }  /** Main method.   * @param options the options   */  public static void main(String [] options) {          DatabaseLoader atf;      try {	atf = new DatabaseLoader();        atf.setOptions(options);        atf.setSource(atf.getUrl(), atf.getUser(), atf.getPassword());        if(!atf.m_inc)            System.out.println(atf.getDataSet());        else{            Instances structure = atf.getStructure();            System.out.println(structure);            Instance temp;            do {            temp = atf.getNextInstance(structure);            if (temp != null) {                System.out.println(temp);            }            } while (temp != null);        }      } catch (Exception e) {	e.printStackTrace();        System.out.println("\n"+e.getMessage());      }  }}

⌨️ 快捷键说明

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