📄 databasesaver.java
字号:
/*
* 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.
*/
/*
* DatabaseSaver.java
* Copyright (C) 2004 Stefan Mutter
*
*/
package weka.core.converters;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.Attribute;
import weka.core.Utils;
import weka.core.Option;
import weka.core.FastVector;
import weka.core.OptionHandler;
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.util.Properties;
import java.util.Enumeration;
import java.util.Vector;
import java.sql.*;
/**
* Writes to a database (tested with MySQL, InstantDB, HSQLDB).
*
* Available options are:
* -T <table name> <br>
* Sets the name of teh table (default: the name of the relation)<p>
*
* -P <br>
* If set, a primary key column is generated automatically (containing the row number as INTEGER). The name of this columns is defined in the DatabaseUtils file.<p>
*
* -i <input-file> <br>
* Specifies an ARFF file as input (for command line use) <p>
*
*
* @author Stefan Mutter (mutter@cs.waikato.ac.nz)
* @version $Revision: 1.1 $
*/
public class DatabaseSaver extends AbstractSaver implements BatchConverter, IncrementalConverter, DatabaseConverter, OptionHandler {
/** The database connection */
private DatabaseConnection m_DataBaseConnection;
/** The name of the tablein which the instances should be stored */
private String m_tableName;
/** An input arff file (for command line use) */
private String m_inputFile;
/** The database specific type for a string (read in from the properties file) */
private String m_createText;
/** The database specific type for a double (read in from the properties file) */
private String m_createDouble;
/** The database specific type for an int (read in from the properties file) */
private String m_createInt;
/** The name of the primary key column that will be automatically generated (if enabled). The name is read from DatabaseUtils.*/
private String m_idColumn;
/** counts the rowsand used as a primary key value */
private int m_count;
/** Flag indicating if a primary key column should be added */
private boolean m_id;
/** Flag indicating whether the default name of the table is the relaion name or not.*/
private boolean m_tabName;
/** The property file for the database connection */
protected static String PROPERTY_FILE
= "weka/experiment/DatabaseUtils.props";
/** Properties associated with the database connection */
protected static Properties PROPERTIES;
/** reads the property file */
static {
try {
PROPERTIES = Utils.readProperties(PROPERTY_FILE);
} catch (Exception ex) {
System.err.println("Problem reading properties. Fix before continuing.");
System.err.println(ex);
}
}
/** Constructor
* @throws Exception throws Exception if property file cannot be read
*/
public DatabaseSaver() throws Exception{
resetOptions();
m_createText = PROPERTIES.getProperty("CREATE_STRING");
m_createDouble = PROPERTIES.getProperty("CREATE_DOUBLE");
m_createInt = PROPERTIES.getProperty("CREATE_INT");
m_idColumn = PROPERTIES.getProperty("idColumn");
}
/** Resets the Saver ready to save a new data set
*/
public void resetOptions(){
super.resetOptions();
setRetrieval(NONE);
m_tableName = "";
m_count = 1;
m_id = false;
m_tabName = true;
try{
if(m_DataBaseConnection != null && m_DataBaseConnection.isConnected())
m_DataBaseConnection.disconnectFromDatabase();
m_DataBaseConnection = new DatabaseConnection();
}catch(Exception ex) {
printException(ex);
}
}
/** Cancels the incremental saving process and tries to drop the table if the write mode is CANCEL. */
public void cancel(){
if(getWriteMode() == CANCEL){
try{
m_DataBaseConnection.execute("DROP TABLE "+m_tableName);
if(m_DataBaseConnection.tableExists(m_tableName))
System.err.println("Table cannot be dropped.");
}catch(Exception ex) {
printException(ex);
}
resetOptions();
}
}
/**
* Returns a string describing this Saver
* @return a description of the Saver suitable for
* displaying in the explorer/experimenter gui
*/
public String globalInfo() {
return "Writes to a database";
}
/** Sets the table's name
* @param tn the name of the table
*/
public void setTableName(String tn){
m_tableName = tn;
}
/** Gets the table's name
* @return the table's name
*/
public String getTableName(){
return m_tableName;
}
/** Returns the tip text fo this property*/
public String tableNameTipText(){
return "Sets the name of the table.";
}
/** En/Dis-ables the automatic generation of a primary key
* @param boolean flag
*/
public void setAutoKeyGeneration(boolean flag){
m_id = flag;
}
/** Gets whether or not a primary key will be generated automatically
* @return true if a primary key column will be generated, false otherwise
*/
public boolean getAutoKeyGeneration(){
return m_id;
}
/** Returns the tip text fo this property*/
public String autoKeyGenerationTipText(){
return "If set to true, a primary key column is generated automatically (containing the row number as INTEGER). The name of the key is read from DatabaseUtils (idColumn)"
+" This primary key can be used for incremental loading (requires an unique key). This primary key will not be loaded as an attribute.";
}
/** En/Dis-ables that the relation name is used for the name of the table (default enabled).
* @param boolean flag
*/
public void setRelationForTableName(boolean flag){
m_tabName = flag;
}
/** Gets whether or not the relation name is used as name of the table
* @return true if the relation name is used as the name of the table, false otherwise
*/
public boolean getRelationForTableName(){
return m_tabName;
}
/** Returns the tip text fo this property*/
public String relationForTableNameTipText(){
return "If set to true, the relation name will be used as name for the database table. Otherwise the user has to provide a table name.";
}
/** Sets the database URL
* @param the URL
*/
public void setUrl(String url){
m_DataBaseConnection.setDatabaseURL(url);
}
/** Gets the database URL
* @return the URL
*/
public String getUrl(){
return m_DataBaseConnection.getDatabaseURL();
}
/** Returns the tip text fo this property*/
public String urlTipText(){
return "The URL of the database";
}
/** Sets the database user
* @param the user name
*/
public void setUser(String user){
m_DataBaseConnection.setUsername(user);
}
/** Gets the database user
* @return the user name
*/
public String getUser(){
return m_DataBaseConnection.getUsername();
}
/** Returns the tip text fo this property*/
public String userTipText(){
return "The user name for the database";
}
/** Sets the database password
* @param the password
*/
public void setPassword(String password){
m_DataBaseConnection.setPassword(password);
}
/** Returns the tip text fo this property*/
public String passwordTipText(){
return "The database password";
}
/** Sets the database url
* @param url the database url
* @param userName the user name
* @param password the password
*/
public void setDestination(String url, String userName, String password){
try{
m_DataBaseConnection = new DatabaseConnection();
m_DataBaseConnection.setDatabaseURL(url);
m_DataBaseConnection.setUsername(userName);
m_DataBaseConnection.setPassword(password);
} catch(Exception ex) {
printException(ex);
}
}
/** Sets the database url
* @param url the database url
*/
public void setDestination(String url){
try{
m_DataBaseConnection = new DatabaseConnection();
m_DataBaseConnection.setDatabaseURL(url);
} catch(Exception ex) {
printException(ex);
}
}
/** Sets the database url using the DatabaseUtils file */
public void setDestination(){
try{
m_DataBaseConnection = new DatabaseConnection();
} catch(Exception ex) {
printException(ex);
}
}
/**
* Opens a connection to the database
*
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -