纯Java项目批处理(打包方法二)

1. 创建一个普通的Java项目

在这里插入图片描述
主类

public class Demo {
    public static void main(String[] args) {
        System.out.println("hello world");
        System.out.println("你好,世界!");

        UserDao dao = new UserDao(JDBCUtil.getConn(), null);
        try {
            dao.insertData();
            System.out.println("执行dao方法成功");
        } catch (SQLException e) {
            System.out.println("执行dao方法报错!!!");
            throw new RuntimeException(e);
        }
    }
}

JDBC工具类

public class JDBCUtil {
    private static final ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
    private static Properties prop = new Properties();
    private static DruidDataSource dataSource = new DruidDataSource();

    private static Connection conn = null;

    static {
        //数据库连接初始化
        try{
            prop.load(JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
        }catch(IOException e){
            System.out.println("数据库参数文件读取错误");
            e.printStackTrace();
        }

        dataSource.setUsername(prop.getProperty("jdbc.username"));
        dataSource.setUrl(prop.getProperty("jdbc.url"));
        dataSource.setPassword(prop.getProperty("jdbc.password"));

        try{
            dataSource.setDriverClassName(prop.getProperty("jdbc.driverClass"));
        }catch(Exception e){
            System.out.println("连接池构造异常1!");
            e.printStackTrace();
        }

        dataSource.setInitialSize(Integer.valueOf(prop.getProperty("jdbc.initialSize")));
        dataSource.setMinIdle(Integer.valueOf(prop.getProperty("jdbc.minPoolSize")));
        dataSource.setMaxActive(Integer.valueOf(prop.getProperty("jdbc.maxPoolSize")));
        dataSource.setMaxWait(Integer.valueOf(prop.getProperty("jdbc.maxWait")));
        dataSource.setTimeBetweenEvictionRunsMillis(Integer.valueOf(prop.getProperty("jdbc.timeBetweenEvictionRunsMillis")));
        dataSource.setMinEvictableIdleTimeMillis(Integer.valueOf(prop.getProperty("jdbc.minEvictableIdleTimeMillis")));

        try {
            threadLocal.set(dataSource.getConnection());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    //获取连接
    public static Connection getConn(){
        conn = threadLocal.get();
        System.out.println("threadLocal获取连接:" + conn);

        try{
            if (conn == null || conn.isClosed()) {
                Class.forName(prop.getProperty("jdbc.driverClass")).toString();
                conn = DriverManager.getConnection(prop.getProperty("jdbc.url").toString(),
                                                   prop.getProperty("jdbc.username").toString(),
                                                   prop.getProperty("jdbc.password").toString());
                try{
                    dataSource.setDriverClassName(prop.getProperty("jdbc.driverClass"));
                }catch(Exception e){
                    System.out.println("连接池构造异常2!");
                    e.printStackTrace();
                    throw new IllegalStateException();
                }
            }
            System.out.println("conn:"+conn);
        }catch(Exception e){
            System.out.println("获取数据库连接异常!");
            e.printStackTrace();
            throw new IllegalStateException();
        }
        return conn;
    }
    
    //关闭连接
    public static void closeConn(){
        try{
            if(threadLocal.get() != null && !threadLocal.get().isClosed()){
                threadLocal.get().close();
                threadLocal.remove();
            }
        }catch(Exception e){
            System.out.println("关闭数据库连接异常!");
        }
    }
    //事务开始
    public static void beginTransaction(){
        try{
            getConn().setAutoCommit(false);
        }catch(SQLException e){
            System.out.println("开始事务异常");
            throw new IllegalStateException();
        }
    }

    //事务提交
    public static void commit(){
        try{
            getConn().commit();
        }catch(SQLException e){
            System.out.println("提交事务异常");
            throw new IllegalStateException();
        }
    }

    //事务回滚
    public static void rollback(){
        try{
            Connection conn = getConn();
            conn.setAutoCommit(false);
            conn.rollback();
            conn.setAutoCommit(true);
        }catch(SQLException e){
            System.out.println("事务回滚异常");
            throw new IllegalStateException();
        }
    }
}

JDBC操作类

public class UserDao {
    private Connection conn;
    private JSONObject json;
    PreparedStatement ps = null;
    public UserDao(Connection conn, JSONObject json) {
        this.conn = conn;
        this.json = json;
    }
    
    public void insertData() throws SQLException {

        String sql = "insert into tb_user(name,age,address) values(?,?,?)";
        ps = conn.prepareStatement(sql);
        try{
            for (int i=0; i<100; i++) {
                ps.setString(1, "张三"+i);
                ps.setInt(2, 23);
                ps.setString(3, "星颂家园24号2302室");
                ps.addBatch();
            }
            ps.executeBatch();
        }catch(SQLException e){
            System.out.println("执行插入数据异常!");
        }finally {
            if (ps != null) {
                ps.close();
            }
            if (conn != null){
                conn.close();
            }
        }
    }
}

配置文件

jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.227.122:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2b8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
jdbc.username=root
jdbc.password=root
jdbc.initialSize=20
jdbc.minPoolSize=10
jdbc.maxPoolSize=100
jdbc.maxWait=60000
jdbc.timeBetweenEvictionRunsMillis=60000
jdbc.minEvictableIdleTimeMillis=300000

2. 引入依赖jar包

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3. 构建包设置

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4. 构建项目jar包

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这里我们用解压缩工具打开jar包看看,可以看到一个jar包里面我们需要的东西都在
在这里插入图片描述

编写shell脚本

demo3.sh

#!/bin/bash

JAVA_HOME=/usr/local/src/jdk1.8
LANG=zh_CN.UTF-8

BATCHROOT=/app/demoBatch

${JAVA_HOME}/bin/java -Xms1024M -Xmx1024M -Dfile.encoding=UTF-8 -cp ${BATCHROOT}/jamy-demo.jar com.jamy.song.Demo

测试

将刚才生成的jamy-demo.jar包上传到linux服务器上,存放位置就是shell脚本配置的位置(/app/demoBatch/)

执行shell脚本之前查询sql
在这里插入图片描述
执行shell脚本
在这里插入图片描述
执行shell脚本之后我们再来查询sql看看
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

下雨天的太阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值