简体   繁体   English

在Java中传递多个参数

[英]passing multiple arguments in java

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). 但是,当我尝试将此代码更改为以下代码时,我得到一个错误-错误:在类MyClass中找不到主要方法,请将该主要方法定义为:public static void main(String [] args)。 I need to receive owner and consumerName as arguments/input for my program. 我需要接收owner和ConsumerName作为程序的参数/输入。

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? , 如何才能做到这一点?

Use the String-Array to pass the parameters in. The main-Method has only the args parameter. 使用String-Array传递参数。main-Method仅具有args参数。

Java Doc: Main Method Java Doc:主要方法

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. 您可以将所有所有者传递到数组中,然后将限制器String放入其中(不能是所有者或使用者),然后将所有使用者放入数组中。 In the main you iterate over the args-array and create two arrays of it. 在主体中,您遍历args-array并为其创建两个数组。

您不能更改public static void main的语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM