在java中传递多个参数

哈里斯·拉希姆 |

我有以下代码为我工作。

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) {}
             }

   }

}

但是,当我尝试将此代码更改为以下代码时,出现错误 - 错误:在 MyClass 类中找不到 Main 方法,请将主要方法定义为:public static void main(String[] args)。我需要接收 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) {}
             }

   }

}

, 如何才能做到这一点?

延斯

使用 String-Array 传入参数。 main-Method 只有 args 参数。

Java 文档:主要方法

您可以将所有所有者传递到数组中,然后将限制器字符串放入其中(不能是所有者或消费者),然后将所有消费者放入数组。在 main 中,您遍历 args-array 并创建它的两个数组。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章