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

时间:2014-05-22 10:06:27

标签: java database string unix args

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

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());
    }
  }
}

我认为我正在倒退,如果我在下一次“尝试”中将它们调暗?

1 个答案:

答案 0 :(得分:0)

感谢百万@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());
    }
  }
}