Link to home
Start Free TrialLog in
Avatar of onyourmark
onyourmark

asked on

how to use a class

Hi. I want to use this class defined below. Would I use it by compiling it, putting it in the same directory as the program from which I call it and then call it by writing  

Arff2Database <input.arff>
 
in my program?

Do I need an import statement?
Thanks.

Usage:
 Arff2Database <input.arff>

Source code:
  import weka.core.*;
  import weka.core.converters.*;
  import java.io.*;
 
  /**
   * A simple API example of transferring an ARFF file into a MySQL table.
   * It loads the data into the database "weka_test" on the MySQL server
   * running on the same machine. Instead of using the relation name of the
   * database as the table name, "mytable" is used instead. The
   * DatabaseUtils.props file must be configured accordingly.
   *
   * Usage: Arff2Database input.arff
   *
   * @author FracPete (fracpete at waikato dot ac dot nz)
   */
  public class Arff2Database {
 
    /**
     * loads a dataset into a MySQL database
     *
     * @param args    the commandline arguments
     */
    public static void main(String[] args) throws Exception {
      Instances data = new Instances(new BufferedReader(new FileReader(args[0])));
      data.setClassIndex(data.numAttributes() - 1);
 
      DatabaseSaver save = new DatabaseSaver();
      save.setUrl("jdbc:mysql://localhost:3306/weka_test");
      save.setUser("fracpete");
      save.setPassword("fracpete");
      save.setInstances(data);
      save.setRelationForTableName(false);
      save.setTableName("mytable");
      save.connectToDatabase();
      save.writeBatch();
    }
  }
Avatar of for_yan
for_yan
Flag of United States of America image

Yes, in general you need  class to be accessible in CLASSPATH
You need import statment if the class is in diifferent package

I think this class is designed to be called as the starting execution class, srather tah to bereferences in the ocde - it has nothing but th e main() method

so theis code needs ti be compiled into class

javac Arff2Database.java

and then used form command likene as it is written there:


java  Arff2Database input.arff
Most of java classes represent some kind of objects, there are many way you may use them in your other c,lasees, but used most frequently and typivla it is in this way :

somewhere in your code of some other of your classe:

MyCustomClass mcc = new MyCustomClass(...possible arguments...); //create instance of the classs

mcc.methodOfClassMyCustomClass(.....parameters of the method...."); //execute method of this class
Avatar of onyourmark
onyourmark

ASKER

Hello. This code is being used to write a file to a database. I have other code (called ProcessData) and at the end of that code I want to write the results to a database and so I was going to use the class Arff2Database defined above. But Arff2Database has a main method in it so I do not know if I can call it from another class that also has a main method and I don't know if that is the proper and best way to do it. I guess I could just copy the code from the main method of Arff2Database into my ProcessData class and use it like that but I was wondering what the "java" way to do this is? I mean to have a class that does one thing (write to a database) and then call it from another class.
Thanks.
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
actually the main work here is done by the object of the class Instances named data, so you do the same thing - use the object of this class; which still parctivcally means you'll copy the code
Thanks. Still trying to understand the Java method. Not quite there yet though.
this is more correct statement:

actually the main work here is done by the object of the class DatabaseSaver named save, so you do the same thing - use the object of this class; which still parctivcally means you'll copy the code