java中ibatis2直接执行my sql脚本

来源:互联网 发布:金达莱花网络歌手 编辑:程序博客网 时间:2024/05/19 03:25
原来ibatis2中,其实还可以有ScriptRunner类直接运行mysql中的脚本了(其实不限于mysql的脚本),马上来看代码:

String aSQLScriptFilePath = "/script.sql"; Class.forName("com.mysql.jdbc.Driver");Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/database", "username", "password");Statement stmt = null; try {ScriptRunner sr = new ScriptRunner(con, false, false); Reader reader = new BufferedReader(                               new FileReader(aSQLScriptFilePath)); sr.runScript(reader); } catch (Exception e) {System.err.println("Failed to Execute" + aSQLScriptFilePath+ " The error is " + e.getMessage());}
原创粉丝点击