读取一个SQL文件 + 批量执行batchUpdate(String[ ])
2017-04-13 10:11
489 查看
读取文件:先将文件转换为 InputStream流,然后再将流转为String
Thread.currentThread().getContextClassLoader().getResourceAsStream(resource)
批量执行:
FileUtil.java:
Game Over!
批量执行:
sql:
大小: 13.2 KB
查看图片附件
Thread.currentThread().getContextClassLoader().getResourceAsStream(resource)
批量执行:
String content = FileUtil.readFile("com/enation/javashop/produceArea.sql");
FileUtil.java:
public static String readFile(String resource) { InputStream stream = getResourceAsStream(resource); String content = readStreamToString(stream); return content; }
public static InputStream getResourceAsStream(String resource) { String stripped = resource.startsWith("/") ? resource.substring(1): resource; InputStream stream = null; ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader != null) { stream = classLoader.getResourceAsStream(stripped); } return stream; }
public static String readStreamToString(InputStream stream) { String fileContent = ""; try { InputStreamReader read = new InputStreamReader(stream, "utf-8"); BufferedReader reader = new BufferedReader(read); String line; while ((line = reader.readLine()) != null) { fileContent = fileContent + line + "\n"; } read.close(); read = null; reader.close(); read = null; } catch (Exception ex) { fileContent = ""; } return fileContent; }
Game Over!
批量执行:
sql:
INSERT INTO `es_produce_area_<userid>_<siteid>` VALUES ('1', '0', ',0,', '1', '杭州市','1'); INSERT INTO `es_produce_area_<userid>_<siteid>` VALUES ('2', '1', ',1,2,', '2', '上城区', '2');
String[] sql_ar = content.split(";\n");
jdbcTemplate.batchUpdate(sql_ar);
大小: 13.2 KB
查看图片附件
相关文章推荐
- sqlite数据库,进行批量执行.sql文件的时候,会出现一个:not an error的错误提示
- 如何批量将一个文件夹下所有的SQL文件执行,更新到数据库
- mybatis前台传来一个String,后后台执行sql变成了true
- 写sql语句exec master..xp_cmdshell 在一个sql文件中去执行多个sql文件
- 怎样把sqlplus执行的查询结果重定向输出到一个文件里面去?
- PostgreSQL sql放入文件批量执行
- 批量执行SQL文件
- 如何执行一个mysql的sql脚本文件
- Dom4j 读取一个XML文件和将String写成XML文件
- 技巧和诀窍: 如何上传一个.SQL文件到远程主机并且执行它来部署一个SQL数据库
- 将数据从一个数据库烤出来,想放到另一个数据库,由于某些原因不能直接执行sql文件,笨方法这是一个excel函数
- 如何上传一个.SQL文件到远程主机并且执行它来部署一个SQL数据库
- spark SQL读取ORC文件从Driver启动到开始执行Task(或stage)间隔时间太长(计算Partition时间太长)且产出orc单个文件中stripe个数太多问题解决方案
- Mybatis中执行String类型的自己拼写的sql,不执行配置文件中的sql
- C#读取*.sql文件,并执行里面的SQL语句
- 如何执行一个mysql的sql脚本文件
- 批量执行SQL文件
- 批量执行SQL文件
- java程序连接数据库并批量执行.sql文件
- 定义一个 GlobalSqlConfigHelper 类,实现读取 XML 文件中的 SQL 语句。