栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java执行SQL脚本文件到数据库详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java执行SQL脚本文件到数据库详解

本文实例为大家分享了Java执行SQL脚本文件到数据库的具体方式,供大家参考,具体内容如下

方式一:直接读取SQL脚本文件的内容,然后传递到SQL中。

代码:RunSqlService:

  @Autowired
  private RunSqlDao runSqlDao;
  
  
  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 : 

  
  public void runSqlBySqlStr(String sqlStr) {
    Map map=new HashMap();
    map.put("sqlStr", sqlStr);
    sqlSessionTemplate.selectList("runSql.runSqlBySqlStr", map);
  }

SQLMap:







这种写法:只支持数据的变化(新增、修改、删除),且SQL文件内容以begin开始,以end结束。无法更新表字段修改等操作。

方式二;使用scriptRunner

代码:RunSqlService:

  
  public void runSqlByscriptRunner(String sqlPath) throws Exception {
    try {
      SqlSession sqlSession = sqlSessionFactory.openSession();
      Connection conn = sqlSession.getConnection();
      scriptRunner runner = new scriptRunner(conn);
      runner.setEscapeProcessing(false);
      runner.setSendFullscript(true);      
      runner.runscript(new InputStreamReader(new FileInputStream(sqlPath), "UTF-8"));
    } catch (Exception e) {
      e.printStackTrace();
      throw e;
    }
  }


这种写法:只能有一行SQL,即一次执行一个SQL语句,否则就会报错。

方式三:使用scriptUtils

代码:RunSqlService:(以下两种方式:脚本.Sql 和RunSqlService 在同一目录下)

方法(1)

  
  public void runSqlBySpringUtils() throws Exception {
    try {
      SqlSession sqlSession = sqlSessionFactory.openSession();
      Connection conn = sqlSession.getConnection();
      ClassPathResource rc = new ClassPathResource("脚本.Sql", RunSqlDao.class);
      scriptUtils.executeSqlscript(conn, rc);
    } catch (Exception e) {
      e.printStackTrace();
      throw e;
    }
  }

方法(2)

  
  public void runSqlBySpringUtils() throws Exception {
    try {
      SqlSession sqlSession = sqlSessionFactory.openSession();
      Connection conn = sqlSession.getConnection();
      ClassPathResource rc = new ClassPathResource("脚本.Sql", RunSqlDao.class);
      EncodedResource er = new EncodedResource(rc, "utf-8");
      scriptUtils.executeSqlscript(conn, er);
    } catch (Exception e) {
      e.printStackTrace();
      throw e;
    }
  }

方法(1),脚本.Sql文件必须是ANSI的,否则执行到数据中汉字是乱码。

方法(2)解决了方法(1)的问题,完美了,喜欢的小伙伴们快拿去享用吧。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/145486.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号