Java程序打包后带参数执行

Java程序打包后带参数执行

执行样式

java -jar xxx.jar  参数1  参数2

所需配置

maven

maven的pom文件夹中配置build相关内容,格式如下

    <!--打包必须,生成jar包-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--此处填写运行jar包时启动的main函数-->
                    <mainClass>com.xxxx.run.RunZB</mainClass>
                    <layout>JAR</layout>
                    <!--构建完整可执行程序,可以直接运行-->
                    <executable>true</executable>
                </configuration>
            </plugin>
        </plugins>
    </build>

上方用到的代码

public class RunZB {

    public final static Logger logger = LoggerFactory.getLogger(RunZB.class);

    private static String driver;

    private static String url;

    private static String username;

    private static String password;

    private static Connection conn=null;

    private static Statement stat=null;


    /**
     * args[0]:sql文件路径
     * args[1]:配置文件路径
     * @param args
     */
    public static void main(String[] args) {
        String path1 = args[0];
        String path2 = args[1];
        test_autoCommit( FileUtils.file2str(path1), path2);
    }

    /**
     * 原生jdbc,通过原生的事务来实现。
     * @param TxtSql
     */
    public static void test_autoCommit(List<String> TxtSql, String path) {

        ByteArrayOutputStream baoS = new ByteArrayOutputStream();
        String errorSql =null;
        try {

            connectOracle(path);
            for (String sql : TxtSql) {
//                logger.info("[" + path + "]中包含" + TxtSql.size() + "条sql语句");
                //4、执行sql语句
                stat.execute(sql);
                errorSql = sql;
//                logger.info("sql语句:[" + sql + "]执行成功!");
            }
            conn.commit();
            logger.info("文本成功执行!");
//            logger.info("文本[" + path + "]成功执行!");
            //5、处理结果集
        } catch (Exception e) {
            e.printStackTrace();
            //异常信息输出到日志
            e.printStackTrace(new PrintStream(baoS));
            String s = baoS.toString();
            logger.error(s);
//            logger.error("文本[" + path + "]因异常终止!所有sql都不生效!,出错在 “" + errorSql + "”附近");
            try {
                conn.rollback();//如果sql有错误进行回滚
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally{
            //6、关闭资源
            try {
                if(stat!=null)stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if(conn!=null)conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 连接Oracle数据库
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static void connectOracle(String path) throws ClassNotFoundException, SQLException {
        getConfig(path);
        //1、注册驱动
        Class.forName(driver);
        //2、获取连接
        conn= DriverManager.getConnection(url, username, password);
        //关闭自动事务
        conn.setAutoCommit(false);
        //System.out.println(conn);
        //3、创建statement对象
        stat=conn.createStatement();
    }

    public static void getConfig(String path) {
        List<String> filestr = FileUtils.filestr(path);
        List<String> config= new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        for (String s : filestr) {
            config.add(s.substring(s.indexOf('=') + 1));
        }
        map.put("driver", config.get(0));
        map.put("url", config.get(1));
        map.put("username", config.get(2));
        map.put("password", config.get(3));
        url = map.get("url");
        driver = map.get("driver");
        username = map.get("username");
        password = map.get("password");

    }


执行的一个案例

不传参

不传参就会报如下错误
在这里插入图片描述

正常传参就会按照自己的想法执行了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值