0

I have below code working for me.

import java.sql.*;
class MyClass {
  public static void main (String[] Owner ) throws Exception
  {
   Class.forName ("oracle.jdbc.OracleDriver");

   Connection conn = DriverManager.getConnection
     ("jdbc:oracle:thin:@//host:port/SID", "username", "password");

   try {
     Statement stmt = conn.createStatement();

     try {
          for (int i=0; i < owner.length; i++){
             String consumerName =  "TestConsumer";
             ResultSet msgs = Stmt.executeQuery("select msg_id from Table where owner = '" + owner[i] + "' and consumer_name = '" + consumerName + "' and msg_state = 'READY'" );

                   while (msgs.next())
                     System.out.println (msgs.getString(1));
                                     try { msgs.close(); } catch (Exception closeMsgsExcp) {}
             } 
        }     

            finally {
             try { stmt.close(); } catch (Exception closeStmtExcp) {}
               }
      } 

      finally {
           try { conn.close(); } catch (Exception closeConnExcp) {}
             }

   }

}

But when i try to change this code to the following i get an error - Error: Main method not found in class MyClass, please define the main method as: public static void main(String[] args). I need to receive owner and consumerName as arguments/input for my program.

import java.sql.*;
class MyClass {
  public static void main (String[] Owner, String[] consumerName ) throws Exception
  {
   Class.forName ("oracle.jdbc.OracleDriver");

   Connection conn = DriverManager.getConnection
     ("jdbc:oracle:thin:@//host:port/SID", "username", "password");

   try {
     Statement stmt = conn.createStatement();

     try {
          for (int i=0; i < owner.length; i++){
             //String consumerName =  "TestConsumer";
             ResultSet msgs = Stmt.executeQuery("select msg_id from Table where owner = '" + owner[i] + "' and consumer_name = '" + consumerName[i] + "' and msg_state = 'READY'" );

                   while (msgs.next())
                     System.out.println (msgs.getString(1));
                                     try { msgs.close(); } catch (Exception closeMsgsExcp) {}
             } 
        }     

            finally {
             try { stmt.close(); } catch (Exception closeStmtExcp) {}
               }
      } 

      finally {
           try { conn.close(); } catch (Exception closeConnExcp) {}
             }

   }

}

, how can this be done?

8
  • 5
    main accepts String[] args. You want to pass two strings: owner and consumer. They will be args[0] and args[1] respectively. Commented Sep 2, 2019 at 6:42
  • 3
    The main method should always have just a single parameter, of type String[]. Do you actually need multiple owners and multiple consumers? If not, just use public static void main(String[] args) and then String owner = args[0]; String consumer = args[1];. Next, you should definitely read up on parameterized SQL rather than embedding the value directly into your SQL. Commented Sep 2, 2019 at 6:42
  • 2
    yet another case of "writing advanced stuff (persistence, ...) without understanding the basics" Just put all your input in a single array of Strings. How would you make your program know which array ended where when calling it from command prompt anyway? Commented Sep 2, 2019 at 6:44
  • I need multiple owners and multiple consumers @Jon Skeet Commented Sep 2, 2019 at 7:01
  • @HarisRahim that changes nothing to the fact that the signature of the main method that can be used as entry point is fixed. Commented Sep 2, 2019 at 7:03

2 Answers 2

1

Use the String-Array to pass the parameters in. The main-Method has only the args parameter.

Java Doc: Main Method

You can pass all owners into the array, then put a limiter String into it (which can't be an owner or consumer) and then put all consumers into the array. In the main you iterate over the args-array and create two arrays of it.

Sign up to request clarification or add additional context in comments.

4 Comments

he is using String arrays. It would be better to point out to use a SINGLE String array.
Can I change this main method to a different method named MyMethod , which can then have 2 String arrays(?), and then invoke it from the main method which will pass the 2 String arrays as args (?).
The method which is called when you start your application has to be the public static void main(String[] name) (see link above). That method can call other methods, but the name has to be main. If you want to call the other method (with two arrays) you have to extract the two strings out of the args-array.
Thanks @JensK and Jon Skeet for your suggestions, i am now taking the one array of owner:consumer .... and then splitting it.
1

You cannot change the syntax of public static void main.

1 Comment

Well you can - you just can't use it as an entry point afterwards.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.