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

📄 databaseutils.java

📁 wekaUT是 university texas austin 开发的基于weka的半指导学习(semi supervised learning)的分类器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* *    This program is free software; you can redistribute it and/or modify *    it under the terms of the GNU General Public License as published by *    the Free Software Foundation; either version 2 of the License, or *    (at your option) any later version. * *    This program is distributed in the hope that it will be useful, *    but WITHOUT ANY WARRANTY; without even the implied warranty of *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *    GNU General Public License for more details. * *    You should have received a copy of the GNU General Public License *    along with this program; if not, write to the Free Software *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* *    DatabaseUtils.java *    Copyright (C) 1999 Len Trigg * */package weka.experiment;import weka.core.Utils;import java.util.Properties;import java.util.Vector;import java.util.StringTokenizer;import java.io.Serializable;import java.io.FileInputStream;import java.sql.SQLException;import java.sql.ResultSet;import java.sql.Statement;import java.sql.Types;import java.sql.DriverManager;import java.sql.Connection;import java.sql.DatabaseMetaData;import java.sql.ResultSetMetaData;/** * DatabaseUtils provides utility functions for accessing the experiment * database. The jdbc * driver and database to be used default to "jdbc.idbDriver" and * "jdbc:idb=experiments.prp". These may be changed by creating * a java properties file called DatabaseUtils.props in user.home or * the current directory. eg:<p> * * <code><pre> * jdbcDriver=jdbc.idbDriver * jdbcURL=jdbc:idb=experiments.prp * </pre></code><p> * * @author Len Trigg (trigg@cs.waikato.ac.nz) * @version $Revision: 1.2 $ */public class DatabaseUtils implements Serializable {  /** The name of the table containing the index to experiments */  public static final String EXP_INDEX_TABLE = "Experiment_index";  /** The name of the column containing the experiment type (ResultProducer) */  public static final String EXP_TYPE_COL    = "Experiment_type";  /** The name of the column containing the experiment setup (parameters) */  public static final String EXP_SETUP_COL   = "Experiment_setup";    /** The name of the column containing the results table name */  public static final String EXP_RESULT_COL  = "Result_table";  /** The prefix for result table names */  public static final String EXP_RESULT_PREFIX = "Results";  /** The name of the properties file */  protected static String PROPERTY_FILE    = "weka/experiment/DatabaseUtils.props";  /** Holds the jdbc drivers to be used (only to stop them being gc'ed) */  protected static Vector DRIVERS = new Vector();  /** Properties associated with the database connection */  protected static Properties PROPERTIES;  /* Load the database drivers -- the properties files only get consulted   * when the class is initially loaded, not for every object instantiated   */  static {    try {      PROPERTIES = Utils.readProperties(PROPERTY_FILE);         // Register the drivers in jdbc DriverManager      String drivers = PROPERTIES.getProperty("jdbcDriver",					    "jdbc.idbDriver");      if (drivers == null) {	throw new Exception("No jdbc drivers specified");      }      // The call to newInstance() is necessary on some platforms      // (with some java VM implementations)      StringTokenizer st = new StringTokenizer(drivers, ", ");      while (st.hasMoreTokens()) {	String driver = st.nextToken();	try {	  DRIVERS.addElement(Class.forName(driver).newInstance());	  System.err.println("Loaded driver: " + driver);	} catch (Exception ex) {	  // Drop through	}      }    } catch (Exception ex) {      System.err.println("Problem reading properties. Fix before continuing.");      System.err.println(ex);    }  }    /** Database URL */  protected String m_DatabaseURL;    /** The database connection */  protected Connection m_Connection;  /** The statement used for database queries */  protected Statement m_Statement;  /** True if debugging output should be printed */  protected boolean m_Debug = true;    /**   * Sets up the database drivers   *   * @exception Exception if an error occurs   */  public DatabaseUtils() throws Exception {    m_DatabaseURL = PROPERTIES.getProperty("jdbcURL",					   "jdbc:idb=experiments.prp");  }  /**   * Converts an array of objects to a string by inserting a space   * between each element. Null elements are printed as ?   *   * @param array the array of objects   * @return a value of type 'String'   */  public static String arrayToString(Object [] array) {    String result = "";    if (array == null) {      result = "<null>";    } else {      for (int i = 0; i < array.length; i++) {	if (array[i] == null) {	  result += " ?";	} else {	  result += " " + array[i];	}      }    }    return result;  }  /**   * Returns the name associated with a SQL type.   *   * @param type the SQL type   * @return the name of the type   */  public static String typeName(int type) {      switch (type) {    case Types.BIGINT :      return "BIGINT ";    case Types.BINARY:      return "BINARY";    case Types.BIT:      return "BIT";    case Types.CHAR:      return "CHAR";    case Types.DATE:      return "DATE";    case Types.DECIMAL:      return "DECIMAL";    case Types.DOUBLE:      return "DOUBLE";    case Types.FLOAT:      return "FLOAT";    case Types.INTEGER:      return "INTEGER";    case Types.LONGVARBINARY:      return "LONGVARBINARY";    case Types.LONGVARCHAR:      return "LONGVARCHAR";    case Types.NULL:      return "NULL";    case Types.NUMERIC:      return "NUMERIC";    case Types.OTHER:      return "OTHER";    case Types.REAL:      return "REAL";    case Types.SMALLINT:      return "SMALLINT";    case Types.TIME:      return "TIME";    case Types.TIMESTAMP:      return "TIMESTAMP";    case Types.TINYINT:      return "TINYINT";    case Types.VARBINARY:      return "VARBINARY";    case Types.VARCHAR:      return "VARCHAR";    default:      return "Unknown";    }  }  /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String databaseURLTipText() {    return "Set the URL to the database.";  }  /**   * Get the value of DatabaseURL.   *   * @return Value of DatabaseURL.   */  public String getDatabaseURL() {        return m_DatabaseURL;  }    /**   * Set the value of DatabaseURL.   *   * @param newDatabaseURL Value to assign to DatabaseURL.   */  public void setDatabaseURL(String newDatabaseURL) {        m_DatabaseURL = newDatabaseURL;  }  /**   * Opens a connection to the database   *   * @exception Exception if an error occurs   */  public void connectToDatabase() throws Exception {    if (PROPERTIES == null) {       try {	PROPERTIES = Utils.readProperties(PROPERTY_FILE);   	// Register the drivers in jdbc DriverManager	String drivers = PROPERTIES.getProperty("jdbcDriver",						"jdbc.idbDriver");	if (drivers == null) {	  throw new Exception("No jdbc drivers specified");	}	// The call to newInstance() is necessary on some platforms	// (with some java VM implementations)	StringTokenizer st = new StringTokenizer(drivers, ", ");	while (st.hasMoreTokens()) {	  String driver = st.nextToken();	  try {	    DRIVERS.addElement(Class.forName(driver).newInstance());	    System.err.println("Loaded driver: " + driver);	  } catch (Exception ex) {	    // Drop through	  }	}      } catch (Exception ex) {	System.err.println("Problem reading properties. Fix before continuing.");	System.err.println(ex);      }    }        if (m_Debug) {      System.err.println("Connecting to " + m_DatabaseURL);    }    if (m_Connection == null) {      m_Connection = DriverManager.getConnection(m_DatabaseURL);      m_Statement = m_Connection.createStatement();    }  }  /**   * Closes the connection to the database.   *   * @exception Exception if an error occurs   */  public void disconnectFromDatabase() throws Exception {    if (m_Debug) {      System.err.println("Disconnecting from " + m_DatabaseURL);    }    if (m_Connection != null) {      m_Connection.close();      m_Connection = null;      m_Statement = null;    }  }    /**   * Returns true if a database connection is active.   *   * @return a value of type 'boolean'   */  public boolean isConnected() {    return (m_Connection != null);  }  /**   * Executes a SQL query.   *   * @param query the SQL query   * @return true if the query generated results   * @exception SQLException if an error occurs   */  public boolean execute(String query) throws SQLException {    return m_Statement.execute(query);  }  /**   * Gets the results generated by a previous query.   *   * @return the result set.   * @exception SQLException if an error occurs   */  public ResultSet getResultSet() throws SQLException {    return m_Statement.getResultSet();  }    /**   * Checks that a given table exists.   *   * @param tableName the name of the table to look for.   * @return true if the table exists.   * @exception Exception if an error occurs.   */  public boolean tableExists(String tableName) throws Exception {    if (m_Debug) {      System.err.println("Checking if table " + tableName + " exists...");    }    DatabaseMetaData dbmd = m_Connection.getMetaData();    ResultSet rs = dbmd.getTables (null, null, tableName, null);    boolean tableExists = rs.next();    if (rs.next()) {      throw new Exception("This table seems to exist more than once!");    }    rs.close();    if (m_Debug) {      if (tableExists) {	System.err.println("... " + tableName + " exists");      } else {	System.err.println("... " + tableName + " does not exist");      }    }    return tableExists;  }    /**   * Executes a database query to see whether a result for the supplied key   * is already in the database.              *   * @param tableName the name of the table to search for the key in   * @param rp the ResultProducer who will generate the result if required   * @param key the key for the result   * @return true if the result with that key is in the database already   * @exception Exception if an error occurs   */  protected boolean isKeyInTable(String tableName,				 ResultProducer rp,				 Object[] key)    throws Exception {    String query = "SELECT Key_Run"      + " FROM " + tableName;    String [] keyNames = rp.getKeyNames();    if (keyNames.length != key.length) {      throw new Exception("Key names and key values of different lengths");    }    boolean first = true;    for (int i = 0; i < key.length; i++) {      if (key[i] != null) {	if (first) {	  query += " WHERE ";	  first = false;	} else {	  query += " AND ";	}	query += "Key_" + keyNames[i] + '=';	if (key[i] instanceof String) {	  query += '\'' + key[i].toString() + '\'';	} else {	  query += key[i].toString();	}      }    }    boolean retval = false;    if (m_Statement.execute(query)) {      ResultSet rs = m_Statement.getResultSet();      int numAttributes = rs.getMetaData().getColumnCount();      if (rs.next()) {	retval = true;	if (rs.next()) {	  throw new Exception("More than one result entry "			      + "for result key: " + query);	}      }      rs.close();    }    return retval;  }  /**   * Executes a database query to extract a result for the supplied key   * from the database.              *   * @param tableName the name of the table where the result is stored   * @param rp the ResultProducer who will generate the result if required

⌨️ 快捷键说明

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