By calling the command line parameters into the jar package

                                                                                    By calling the command line parameters into the jar package

  Recently because the project needs, need to implement a function, that is, the timing of the implementation of a script on the server to the data in the database for business processing, database to be operated There are many, mysql, db2, oracle, sqlserver, etc., but I still shell not familiar with, so I have to use java to achieve, a labeled jar package, the parameters can be called by different databases to meet the requirements. Online search, everyone uses the Apache Commons-CLI packet parsing command line arguments, but say are not very clear, I am here with their own hands to summarize, I hope to be able to help a friend in need.

  I'm here because there are only a few projects to lead the pack, there is no choice by maven project management, just to build a java project, the introduction of the jar package I needed, for example, I want to operate mysql, mysql driver package that will certainly require Right.


 

First, the project directory

 

 

 Is the main class Entrance.java program, DtOptions.java is provided on the command line parameters, DT.java class is a constant, indicates the type of database, dealArgs.java is the processing parameters, Database.java connection with the database entity class, CreateConnection.java to handle operations related to the database connection.


 

Second, project development

1, the introduction of the relevant jar package

The figure can be seen, the introduction of Apache Commons-CLI package and driver package mysql, mysql to operate here I am, for example, it can be self-introduction needed.

2, command line arguments to

We are interested can read the official guide https://commons.apache.org/proper/commons-cli/usage.html , I am here not elaborate.

 1 public class DtOptions {
 2 
 3     public static Options generateOp() {
 4         final Options options = new Options();
 5         options.addOption(new Option("ant", "ant", false, "command help"));
 6         options.addOption(new Option("t", "type", true, "database type"));
 7         options.addOption(new Option("l", "url", true, "database url"));
 8         options.addOption(new Option("u", "username", true, "database username"));
 9         options.addOption(new Option("p", "password", true, "database password"));
10         return options;
11     }
12 }

 

1  // Construction Options object 
2 Options Options = new new Options ();
 . 3  // set a command item has four parameters, the first one is referred to as command, the second command is the full name
 4  // third argument specifies the command items whether it is required, and the fourth is the description of this command 
5 options.addOption ( new new the Option ( "Ant", "Ant", false , "Help the command"));

3, the analytical parameters

 1 public class Entrance {
 2 
 3     public static void main(String[] args) throws ClassNotFoundException, ParseException {
 4         // create the parser
 5         CommandLineParser parser = new DefaultParser();
 6         Options options = DtOptions.generateOp();
 7         CommandLine line = parser.parse(options, args);
 8         try {
 9             if (line.hasOption("ant")) {
10                 HelpFormatter formatter = new HelpFormatter();
11                 formatter.printHelp("ant", options);
12             } else {
13                 Database database = dealArgs.getDatabase(parser, options, args);
14                 Connection conn = CreateConnection.create(database);
15                 conn.close();
16             }
17         } catch (ParseException e) {
18             e.printStackTrace();
19         } catch (SQLException e) {
20             e.printStackTrace();
21         }
22 
23     }
24 }

 

 1 public class dealArgs {
 2 
 3     public static Database getDatabase(CommandLineParser parser, Options options, String[] args) throws ParseException {
 4 
 5         Database database = new Database();
 6         CommandLine line = parser.parse(options, args);
 7 
 8         if (line.hasOption("t")) {//数据库类型
 9             database.setType(line.getOptionValue("t"));
10         }
11         if (line.hasOption("l")) {//url
12             database.setUrl(line.getOptionValue("l"));
13         }
14         if (line.hasOption("u")) {//用户名
15             database.setUsername(line.getOptionValue("u"));
16         }
17         if (line.hasOption("p")) {//密码
18             database.setPassword(line.getOptionValue("p"));
19         }
20         return database;
21 
22     }
23 }

The parameters are to be passed in the args array master class, we need to do is to command-line parameters already defined rules introduced by CommandLineParser objects can be resolved.

At this point, after the project labeled jar package, would be able to call it by reference into, the command is as follows:

1 java -jar stat.jar -t 0 -l jdbc:mysql://localhost:3306 -u root -p root

If the user does not need to know what the parameters or can not call, you can use the following command to view the participation requirements:

1 java -jar stat.jar -ant

what? You ask me why? Carefully observe the main class, so there is a piece of code:

. 1 HelpFormatter Formatter = new new HelpFormatter ();
 2  // set into the command-line parameters, I define a parameter called the ant into
 3  // this parameter into the parameter that is used to display the program information 
. 4 formatter.printHelp ( "ant" , options);

Other code is to establish the parameters according to the type of database in the database connection, not the code I posted here, you can go to see the github.

Third, the project package

You may refer to the following this blog, talking about the good

https://blog.csdn.net/rogerxue12345/article/details/84344753

Fourth, other

Project has been uploaded to github, please share with me:

https://github.com/Thinker-Mars/Demo/tree/master/stat

 

Guess you like

Origin www.cnblogs.com/cone/p/11441251.html