Skip to content

BenoitDuffez/ScriptRunner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 

Repository files navigation

ScriptRunner

Allows to virtually use source script.sql with your JDBC

Initial code was duplicated from: http://pastebin.com/f10584951

Initial code was found using: http://stackoverflow.com/a/1044837/334493

The initial code didn't allow custom delimiters (at least not dynamically) so it was not suited for procedure declarations.

Possible alternative: http://www.mybatis.org/mybatis-3/getting-started.html (see #13)

Example

Here is an example of a supported script:

drop procedure if exists RecordStep;

delimiter $$

CREATE PROCEDURE `p2` (in txt varchar(100))
LANGUAGE SQL
DETERMINISTIC
SQL SECURITY DEFINER
COMMENT 'A procedure'
BEGIN
    SELECT concat('Hello World: ', txt);
END$$

delimiter ;

call p2("test");
call p("this will work too");

Usage

It is used pretty straightforwardly:

try {
    Class.forName("com.mysql.jdbc.Driver");
    mConnection = DriverManager.getConnection("jdbc:mysql://" + SERVER + "/" + DATABASE + "?" + "user=" + USER + "&password=" + PASSWORD);
} catch (ClassNotFoundException e) {
    System.err.println("Unable to get mysql driver: " + e);
} catch (SQLException e) {
    System.err.println("Unable to connect to server: " + e);
}
ScriptRunner runner = new ScriptRunner(mConnection, false, false);
String file = "~/path/to/script.sql";
runner.runScript(new BufferedReader(new FileReader(file)));

About

Allows to virtually use `source script.sql` with your JDBC

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages