从配置文件里面读取sql并批量执行

public class SqlExecutor {

private static final Logger log = LoggerFactory.getLogger(SqlExecutor.class);

public static void main(String[] args) {
    //从文件中读取sql,并处理sql
    List list = readSqlFromFile();
    //批量处理sql语句
    executeBatchSql(list);
}

private static List readSqlFromFile(){
    BufferedReader in = null;
    //--- 1.首先从文件里面读取数据,----但是注意:文件路径需要走配置
    String filePath = getProperties("filePath");
    log.info("sql数据的来源:"+filePath);
    List sqlList = new ArrayList();
    try {
        File file = new File(filePath);
        in = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        String line = "";
        while((line = in.readLine())!=null){
            if (!"".equals(line))
                sqlList.add(line);
        }
        log.info("将从文件里面读取的数据,有"+sqlList.size()+"条");
    } catch (Exception e) {
        log.error("读取sql文件出现异常");
    }finally{
        if(null!=in){
            try {
                in.close();
            } catch (IOException e) {
                log.error("关闭读流出现异常");
            }
        }
    }
    //---2.将list中的字符串进行判断,将影响数据库和表的sql去掉,不执行,并从原集合中删除,写入到一个新的文件中;
    if (null ==sqlList|| 0 == sqlList.size())
        return null;
    List sqlListAfterDispose = new ArrayList();
    for (String s:sqlList) {
        //去掉每条sql后面可能带有的“;”
        String replaceString = s.trim().replace(";","");
        //判断sql是否是影响数据库结构或者删除数据
        if (null!=replaceString && !"".equals(replaceString)) {
            if (replaceString.startsWith("delete") ||replaceString.startsWith("drop") ||replaceString.startsWith("truncate"))
                continue;
            sqlListAfterDispose.add(replaceString);
        }
    }
    if (null ==sqlListAfterDispose|| 0 == sqlListAfterDispose.size()){
        log.info("从文件读出sql,经过处理后,没有需要执行的sql");
        return null;
    }
    log.info("从文件读出sql处理之后,有:"+sqlListAfterDispose.size()+"条");
    return sqlListAfterDispose;
}

private static void executeBatchSql(List sqlListAfterDispose){
    //初始化Connection
    Connection con = null;
    //初始化Statement
    Statement statement = null;
    //读取配置参数
    String url = getProperties("url");
    String username = getProperties("username");
    String password = getProperties("password");
    String driverClassName = getProperties("driverClassName");
    String excutesqlBatchNumber = getProperties("excutesqlBatchNumber");
    if (StringUtils.isNullOrEmpty(username)||StringUtils.isNullOrEmpty(url)||StringUtils.isNullOrEmpty(password)||StringUtils.isNullOrEmpty(driverClassName))
        log.info("从配置文件里面读取的配置参数为空,不能再往下面执行程序");
    log.info("从配置文件里面读取的连接数据库的有关参数:url,username,password,driverClassName:"+url+","+username+","+password+","+driverClassName);
    //----1、批处理sql
    try {
        //加载驱动程序
        Class.forName(driverClassName);
        //getConnection()方法,连接MySQL数据库!!
        con = DriverManager.getConnection(url,username,password);
        if(con.isClosed()){
            log.info("连接数据库失败,将不往下面执行!");
            return;
        }
        //创建statement类对象,用来执行SQL语句!!
        statement = con.createStatement();
        int sqlExcuteNumber = Integer.parseInt(excutesqlBatchNumber);
        //当配置文件里面,配置的sql执行条数为小于等于0条时,就当配置的参数为300条数据
        if (sqlExcuteNumber<=0){
            sqlExcuteNumber=300;
        }
        int count = 0;
            for (int i = 1; i < sqlListAfterDispose.size() + 1; i++) {
                statement.addBatch(sqlListAfterDispose.get(i - 1));
                //每次执行多少条
                if (i % sqlExcuteNumber == 0) {
                    int [] intArr = statement.executeBatch();
                    statement.clearBatch();
                    count=count+intArr.length;
                }
            }
        //每次执行多少条,剩下的再进行批处理
        int [] intArray = statement.executeBatch();
        count=count+intArray.length;
        log.info("sql共批量执行"+count+"条");
    } catch(ClassNotFoundException e) {
        //数据库驱动类异常处理
        log.error("抱歉,没有找到驱动!");
        e.printStackTrace();
    } catch(SQLException e) {
        //数据库连接失败异常处理
        log.error("sql异常!");
        e.printStackTrace();
    }finally {
        if (null != statement) {
            try {
                statement.close();
            } catch (SQLException e) {
                log.error("关闭Statement类出现异常");
            }
        }
        if (null != con) {
            try {
                con.close();
            } catch (SQLException e) {
                log.error("关闭Connection类出现异常");
            }
        }
    }
}

public static String getProperties(String sign,String... args){
    InputStream in = Object.class.getResourceAsStream("/config.properties");
    Properties prop = new Properties();
    String properties="";
    try {
        prop.load(in);
        properties = prop.getProperty(sign).toString();
    } catch (IOException e) {
        log.error("从配置文件读取配置参数,出现异常");
    }finally{
        try {
            if(null!=in)
            {
                in.close();
            }
        } catch (IOException e) {
            log.error("从配置文件读取配参数时关闭读流,出现异常");
        }
    }

    return properties;
}

}

你可能感兴趣的:(从配置文件里面读取sql并批量执行)