Solved

how to use a class

Posted on 2012-03-18
7
298 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 47

Accepted Solution

by:
for_yan earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
base64 decode encode 12 119
Need to track down Infection in a Server 2008 domain user profile 7 48
Java Timer (static) 9 36
couple of eclipse 5 23
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now