Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to use a class

Posted on 2012-03-18
7
Medium Priority
?
305 Views
Last Modified: 2012-03-18
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();
    }
  }
0
Comment
Question by:onyourmark
  • 5
  • 2
7 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37735699
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
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37735716
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
0
 

Author Comment

by:onyourmark
ID: 37735731
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 37735741
In general you can call main method - but that is not the "java way".
"Java way" is if you want to use class form another class - you write it in such a way to use it form another class - not with main() method but with a separate say static or instance nmethod. This one is supposed to be used from the system via java command

So in this case - the simplest would be to copy the code from inside main and just use the code.
Or modify this class and put this code in method or constructor.
For such a simple case i'd rather copy the code.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37735747
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
0
 

Author Closing Comment

by:onyourmark
ID: 37735751
Thanks. Still trying to understand the Java method. Not quite there yet though.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37735753
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question