public static void main(String[] args) throws SQLException { Connection connection = null; Statement stmt = null; String tableName = null; try { //1. 导入驱动,加载具体的驱动类 Class.forName("oracle.jdbc.OracleDriver"); //2. 与数据库建立连接 connection = DriverManager.getConnection(URL, USERNAME, PWD); //3.执行sql语句 stmt = connection.createStatement(); //创建sql语句执行对象 String sqlFile = "E:\\sql\\load_data.sql"; //创建sql语句 // String sqlFile = args[0]; connection.setAutoCommit(false); String fileContent = readFileByLines(sqlFile); if (fileContent.startsWith("select") || fileContent.startsWith("SELECT")) { if (fileContent.contains("/")) { int index = fileContent.indexOf("/"); fileContent = fileContent.substring(0, index); } System.out.println("执行select语句" + fileContent); ResultSet resultSet = stmt.executeQuery(fileContent); int row = resultSet.getRow(); } else if (fileContent.trim().startsWith("insert") || fileContent.startsWith("INSERT")) { //sql用/拼接表名 if (fileContent.contains("/")) { int index = fileContent.indexOf("/"); //获取表名 tableName = fileContent.substring(index, fileContent.length()).replace("/", ""); //执行清空语句 String cleanSqlScript = "DELETE TABLE " + tableName; int deleteTableData = stmt.executeUpdate(cleanSqlScript); System.out.println("执行清空表数据语句:" + "DELETE TABLE " + tableName); String subStringSql = fileContent.substring(0, index); //根据;分割语句批量执行 String[] split = subStringSql.split("-|\\||;|\\*|\\+|\\?|\\{|\\}|\\$|\\^"); for (int i = 0; i < split.length; i++) { System.out.println("执行insert语句" + split[i]); stmt.addBatch(split[i]); } stmt.executeBatch(); String selectSql = "select count(*) from " + tableName; System.out.println("查询的sql为=" + selectSql); ResultSet rs = stmt.executeQuery(selectSql); while (rs.next()) { System.out.print("写入数据总量:" + rs.getString(1)); } } else { System.out.println("执行insert语句" + fileContent); //根据;分割语句批量执行 String[] split = fileContent.split("-|\\||;|\\*|\\+|\\?|\\{|\\}|\\$|\\^"); for (int i = 0; i < split.length; i++) { System.out.println("执行insert语句" + split[i]); stmt.addBatch(split[i]); } stmt.executeBatch(); String selectSql = "select count(*) from " + tableName; System.out.println("查询的sql为=" + selectSql); ResultSet rs = stmt.executeQuery(selectSql); while (rs.next()) { System.out.print("写入数据总量:" + rs.getString(1)+" "); } } connection.commit(); } else { //建表语句走这里 System.out.println("执行SQL语句" + fileContent); int updateResult = stmt.executeUpdate(fileContent); connection.commit(); } } catch (Exception e) { connection.rollback(); e.printStackTrace(); System.exit(1); } finally { try { //释放数据库资源 if (stmt != null) { stmt.close(); } if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); System.exit(1); } } } public static String readFileByLines(String fileName) { File file = new File(fileName); BufferedReader reader = null; StringBuilder sb = new StringBuilder(); try { System.out.println("以行为单位读取文件内容,一次读一整行:"); reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1; // 一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { // 显示行号 System.out.println("line " + line + ": " + tempString); sb.append(tempString); line++; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } return sb.toString(); }