没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Java执行执行SQL脚本文件到数据库详解脚本文件到数据库详解
主要为大家详细介绍了Java执行SQL脚本文件到数据库的相关方法,具有一定的参考价值,感兴趣的小伙伴们
可以参考一下
本文实例为大家分享了Java执行SQL脚本文件到数据库的具体方式,供大家参考,具体内容如下
方式一:方式一:直接读取SQL脚本文件的内容,然后传递到SQL中。
代码:RunSqlService:
@Autowired
private RunSqlDao runSqlDao;
/**
* 读取文件内容到SQL中执行
* @param sqlPath SQL文件的路径:如:D:/TestProject/web/sql/脚本.Sql
*/
public void runSqlByReadFileContent(String sqlPath) throws Exception {
try {
String sqlStr = readFileByLines(sqlPath);
// System.out.println("获得的文本:" + sqlStr);
if (sqlStr.length() > 0) {
runSqlDao.runSqlBySqlStr(sqlStr);
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
private String readFileByLines(String filePath) throws Exception {
StringBuffer str = new StringBuffer();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(filePath), "UTF-8"));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
// System.out.println("line " + line + ": " + tempString);
str = str.append(" " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return str.toString();
}
RunSqlDao :
/**
* @param sqlStr
*/
public void runSqlBySqlStr(String sqlStr) {
Map<String,Object> map=new HashMap<String,Object>();
map.put("sqlStr", sqlStr);
sqlSessionTemplate.selectList("runSql.runSqlBySqlStr", map);
}
资源评论
weixin_38569675
- 粉丝: 4
- 资源: 980
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功