Java通过unix传入参数而不是硬编码字符串

时间:2021-08-25 21:25:54

I have the following script (cleaned as it may have sensitive stuff like connection strings in it) and it works, it works fairly well, the only thing I'm worried about is having the DB user name and password hard coded in there, I've tried to figure out how to pass in strings as I execute it, but with no joy.

我有以下脚本(清理,因为它可能有敏感的东西,如连接字符串),它工作,它工作得相当好,我唯一担心的是在那里硬编码数据库用户名和密码,我我试图弄清楚如何在执行它时传入字符串,但没有任何快乐。

import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class map_check {
  private static final String CONNECT = "ConnectionString goes here";
  private static final String Query1 = "SELECT 1 \n"+
                                       " , 2 \n"+
                                       " , 3 \n"+
                                       " , 4 \n"+
                                       " , 5 \n"+
                                       " , 6 \n"+
                                       " , 7 \n"+
                                       " , 8  \n"+
                                       "FROM db.tab.log \n"+
                                       "where   CONVERT(VARCHAR(19),StartTime,104)  \n"+
                                       "= CONVERT(VARCHAR(19),GETDATE(),104) \n"+
                                       "order by starttime asc";
  private static final String SQLUSER = "**SCOTT**";
  private static final String SQLPSWD = "**TIGER**";

  public static void main(String[] paramArrayOfString) {
    try {
      Class.forName("net.sourceforge.jtds.jdbc.Driver");
    } catch (ClassNotFoundException localClassNotFoundException1) {
      System.out.print("ClassNotFoundException: ");
      System.out.println(localClassNotFoundException1.getMessage());
      System.exit(2);
    }

    try {
      Connection SQL_CONN = DriverManager.getConnection(CONNECT, SQLUSER, SQLPSWD);

      Statement SQL_Stmt = SQL_CONN.createStatement();
      ResultSet SQL_RS = SQL_Stmt.executeQuery(Query1);
      String format = "%-40s%s%n";
      System.out.println("1, 2, 3, 4, 5, 6, 7, 8");
      while (SQL_RS.next()) {
        System.out.println(SQL_RS.getString("1")+","+
                           SQL_RS.getString("2")+","+
                           SQL_RS.getString("3")+","+
                           SQL_RS.getString("4")+","+
                           SQL_RS.getString("5")+","+
                           SQL_RS.getString("6")+","+
                           SQL_RS.getString("7")+","+
                           SQL_RS.getString("8"));
      }

      SQL_Stmt.close();
      SQL_CONN.close();

      System.exit(0);
    } catch (SQLException localSQLException) {
      System.err.println("SQLException: " + localSQLException.getMessage());
    }
  }
}

I think i'm doing it all backwards, should I be dimming them inside the next "try"?

我想我是倒退了,我应该在下一次“尝试”中调暗它们吗?

1 个解决方案

#1


0  

Thanks a million @Peter Lawrey - I get what you were saying now, Answer is below, Really appreciate it, not finding java easy at all!

万分感谢@Peter Lawrey - 我得到你现在所说的,答案如下,真的很感激,没有找到简单的java!

import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class map_check_new {
  private static final String MAPCONNECT = "connstring";
  private static final String Query1 = "SELECT 1 \n"+
                                       " , 2 \n"+
                                       " , 3 \n"+
                                       " , 4 \n"+
                                       " , 5 \n"+
                                       " , 6 \n"+
                                       " , 7 \n"+
                                       " , 8  \n"+
                                       "FROM db.tab.audit \n"+
                                       "where   CONVERT(VARCHAR(19),StartTime,104)  \n"+
                                       "= CONVERT(VARCHAR(19),GETDATE(),104) \n"+
                                       "order by starttime asc";
  public static void main(String[] args) {
 try {
      Class.forName("net.sourceforge.jtds.jdbc.Driver");
    } catch (ClassNotFoundException localClassNotFoundException1) {
      System.out.print("ClassNotFoundException: ");
      System.out.println(localClassNotFoundException1.getMessage());
      System.exit(2);
    }

    try {
      Connection SQL_CONN = DriverManager.getConnection(CONNECT, args[0], args[1]);

      Statement SQL_Stmt = SQL_CONN.createStatement();
      ResultSet SQL_RS = SQL_Stmt.executeQuery(Query1);
      String format = "%-40s%s%n";
      System.out.println("1, 2, 3, 4, 5, 6, 7, 8");
      while (SQL_RS.next()) {
        System.out.println(SQL_RS.getString("1")+","+
                           SQL_RS.getString("2")+","+
                           SQL_RS.getString("3")+","+
                           SQL_RS.getString("4")+","+
                           SQL_RS.getString("5")+","+
                           SQL_RS.getString("6")+","+
                           SQL_RS.getString("7")+","+
                           SQL_RS.getString("8"));
      }

      SQL_Stmt.close();
      SQL_CONN.close();

      System.exit(0);
    } catch (SQLException localSQLException) {
      System.err.println("SQLException: " + localSQLException.getMessage());
    }
  }
}

#1


0  

Thanks a million @Peter Lawrey - I get what you were saying now, Answer is below, Really appreciate it, not finding java easy at all!

万分感谢@Peter Lawrey - 我得到你现在所说的,答案如下,真的很感激,没有找到简单的java!

import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class map_check_new {
  private static final String MAPCONNECT = "connstring";
  private static final String Query1 = "SELECT 1 \n"+
                                       " , 2 \n"+
                                       " , 3 \n"+
                                       " , 4 \n"+
                                       " , 5 \n"+
                                       " , 6 \n"+
                                       " , 7 \n"+
                                       " , 8  \n"+
                                       "FROM db.tab.audit \n"+
                                       "where   CONVERT(VARCHAR(19),StartTime,104)  \n"+
                                       "= CONVERT(VARCHAR(19),GETDATE(),104) \n"+
                                       "order by starttime asc";
  public static void main(String[] args) {
 try {
      Class.forName("net.sourceforge.jtds.jdbc.Driver");
    } catch (ClassNotFoundException localClassNotFoundException1) {
      System.out.print("ClassNotFoundException: ");
      System.out.println(localClassNotFoundException1.getMessage());
      System.exit(2);
    }

    try {
      Connection SQL_CONN = DriverManager.getConnection(CONNECT, args[0], args[1]);

      Statement SQL_Stmt = SQL_CONN.createStatement();
      ResultSet SQL_RS = SQL_Stmt.executeQuery(Query1);
      String format = "%-40s%s%n";
      System.out.println("1, 2, 3, 4, 5, 6, 7, 8");
      while (SQL_RS.next()) {
        System.out.println(SQL_RS.getString("1")+","+
                           SQL_RS.getString("2")+","+
                           SQL_RS.getString("3")+","+
                           SQL_RS.getString("4")+","+
                           SQL_RS.getString("5")+","+
                           SQL_RS.getString("6")+","+
                           SQL_RS.getString("7")+","+
                           SQL_RS.getString("8"));
      }

      SQL_Stmt.close();
      SQL_CONN.close();

      System.exit(0);
    } catch (SQLException localSQLException) {
      System.err.println("SQLException: " + localSQLException.getMessage());
    }
  }
}