All Downloads are FREE. Search and download functionalities are using the official Maven repository.

weka.core.converters.DatabaseSaver Maven / Gradle / Ivy

Go to download

The Waikato Environment for Knowledge Analysis (WEKA), a machine learning workbench. This version represents the developer version, the "bleeding edge" of development, you could say. New functionality gets added to this version.

There is a newer version: 3.9.6
Show newest version
/*
 *   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 3 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, see .
 */

/*
 *    DatabaseSaver.java
 *    Copyright (C) 2004-2012 University of Waikato, Hamilton, New Zealand
 *
 */

package weka.core.converters;

import weka.core.Attribute;
import weka.core.Capabilities;
import weka.core.Capabilities.Capability;
import weka.core.Environment;
import weka.core.EnvironmentHandler;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.Option;
import weka.core.OptionHandler;
import weka.core.OptionMetadata;
import weka.core.RevisionUtils;
import weka.core.Utils;
import weka.gui.FilePropertyMetadata;
import weka.gui.PasswordProperty;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Enumeration;
import java.util.Vector;

/**
 *  Writes to a database (tested with MySQL, InstantDB,
 * HSQLDB).
 * 

* * * Valid options are: *

* *

 * -url <JDBC URL>
 *  The JDBC URL to connect to.
 *  (default: from DatabaseUtils.props file)
 * 
* *
 * -user <name>
 *  The user to connect with to the database.
 *  (default: none)
 * 
* *
 * -password <password>
 *  The password to connect with to the database.
 *  (default: none)
 * 
* *
 * -T <table name>
 *  The name of the table.
 *  (default: the relation name)
 * 
* *
 * -truncate
 *  Truncate (i.e. delete any data) in table before inserting
 * 
* *
 * -P
 *  Add an ID column as primary key. The name is specified
 *  in the DatabaseUtils file ('idColumn'). The DatabaseLoader
 *  won't load this column.
 * 
* *
 * -custom-props <file>
 *  The custom properties file to use instead of default ones,
 *  containing the database parameters.
 *  (default: none)
 * 
* *
 * -i <input file name>
 *  Input file in arff format that should be saved in database.
 * 
* * * * @author Stefan Mutter ([email protected]) * @version $Revision: 12418 $ */ public class DatabaseSaver extends AbstractSaver implements BatchConverter, IncrementalConverter, DatabaseConverter, OptionHandler, EnvironmentHandler { /** for serialization. */ static final long serialVersionUID = 863971733782624956L; /** The database connection. */ protected DatabaseConnection m_DataBaseConnection; /** The name of the table in which the instances should be stored. */ protected String m_tableName; /** Table name with any environment variables resolved */ protected String m_resolvedTableName; /** An input arff file (for command line use). */ protected String m_inputFile; /** * The database specific type for a string (read in from the properties file). */ protected String m_createText; /** * The database specific type for a double (read in from the properties file). */ protected String m_createDouble; /** The database specific type for an int (read in from the properties file). */ protected String m_createInt; /** The database specific type for a date (read in from the properties file). */ protected String m_createDate; /** For converting the date value into a database string. */ protected SimpleDateFormat m_DateFormat; /** * The name of the primary key column that will be automatically generated (if * enabled). The name is read from DatabaseUtils. */ protected String m_idColumn; /** counts the rows and used as a primary key value. */ protected int m_count; /** Flag indicating if a primary key column should be added. */ protected boolean m_id; /** * Flag indicating whether the default name of the table is the relaion name * or not. */ protected boolean m_tabName; /** the database URL. */ protected String m_URL; /** the user name for the database. */ protected String m_Username; /** the password for the database. */ protected String m_Password = ""; /** the custom props file to use instead of default one. */ protected File m_CustomPropsFile = new File("${user.home}"); /** * Whether to truncate (i.e. drop and then recreate) the table if it already * exists */ protected boolean m_truncate; /** Environment variables to use */ protected transient Environment m_env; /** * Constructor. * * @throws Exception throws Exception if property file cannot be read */ public DatabaseSaver() throws Exception { resetOptions(); } /** * Main method. * * @param options should contain the options of a Saver. */ public static void main(String[] options) { StringBuffer text = new StringBuffer(); text.append("\n\nDatabaseSaver options:\n"); try { DatabaseSaver asv = new DatabaseSaver(); try { Enumeration