/* * 传入文件执行sql语句 * * */ public static void execute(String sqlFile) throws SQLException { Statement stmt = null; List<String> sqlList = new ArrayList<String>(); Connection conn = getConnection(); try { sqlList = loadSql(sqlFile); conn.setAutoCommit(false); stmt = conn.createStatement(); for (String sql : sqlList) { System.out.println(sql); stmt.addBatch(sql); } int[] rows = stmt.executeBatch(); System.out.println("Row count:" + Arrays.toString(rows)); conn.commit(); System.out.println("数据更新成功"); } catch (Exception e) { e.printStackTrace(); conn.rollback(); }finally{ stmt.close(); conn.close(); } }
/* * 读取sql文件,获取sql语句 * 返回所有sql语句的list集合 * */ private static List<String> loadSql(String sqlFile) throws Exception { List<String> sqlList = new ArrayList<String>(); /* * 读取文件的内容并写道StringBuffer中去 * */ /*InputStream sqlFileIn = new FileInputStream(sqlFile); StringBuffer sqlSb = new StringBuffer(); byte[] buff = new byte[sqlFileIn.available()]; int byteRead = 0; while((byteRead = sqlFileIn.read(buff)) != -1) { sqlSb.append(new String(buff, 0, byteRead)); }*/ InputStream sqlFileIn = new FileInputStream(sqlFile); InputStreamReader InputStreamReader = new InputStreamReader(sqlFileIn,"UTF-8"); StringBuffer sqlSb = new StringBuffer(); char[] buff = new char[12]; int byteRead = 0; while ((byteRead = InputStreamReader.read(buff)) != -1) { sqlSb.append(new String(buff, 0, byteRead)); } /* * windows下换行是/r/n,Linux下是/n, * 此处需要根据导出的sql文件进行具体的处理,我在处理的时候 * 也遇到了很多的问题,如果我这个不行可以在网上找找别的解析方法 * */ String sqlArr[] = sqlSb.toString().split("(;\\s*\\rr\\n)|(;\\s*\\n)"); for(int i = 0; i<sqlArr.length; i++) { String sql = sqlArr[i].replaceAll("--.*", "").trim(); if(!"".equals(sql)) { sqlList.add(sql); } } InputStreamReader.close(); sqlFileIn.close(); return sqlList; }
/* * 获取sql连接 * */ private static Connection getConnection(){ Connection conn = null; PropKit.use("config.txt"); try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password")); if(!conn.isClosed()) { System.out.println("数据库连接成功!"); } } catch (Exception e) { e.printStackTrace(); } return conn; }