java中ibatis2直接执行my sql脚本

原来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());
		}

你可能感兴趣的:(ibatis)