从Java程序运行SQL文件脚本 [英] Running SQL files scripts from a Java program

查看:146
本文介绍了从Java程序运行SQL文件脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组用于转换原始数据集的SQL文件.当前,我打开每个文件并执行它.如何执行Java程序中的每个文件?目标是使此过程更加自动化.

I have a set of SQL files that transform my original dataset. Currently, I open each file and execute it. How can I execute each file inside a Java program? The goal is to make this process a lot more automatic.

我想做类似SqlScript.execute("myScript.sql");

注意这些SQL脚本作用于一个数据库.我认为我必须传递某种连接字符串.我正在使用MySQL.

NOTE these SQL scripts act on one database. I assume I would have to pass some kind of connection string. I am using MySQL.

  1. 我需要在Java中执行哪些对象,库,程序包等?

推荐答案

Ibatis提供了

Ibatis provides a ScriptRunner that will help you. Simple code snippets you can refer:

Connection conn=getConnection();//some method to get a Connection
ScriptRunner runner=new ScriptRunner(conn, false, false);
InputStreamReader reader = new InputStreamReader(new FileInputStream("foo.sql"));
runner.runScript(reader);
reader.close();
conn.close();

这篇关于从Java程序运行SQL文件脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆