def db = [
moduleGroup: 'mysql',
moduleName: 'mysql-connector-java',
moduleVersion: '5.1.18',
driver: "com.mysql.jdbc.Driver",
url: 'jdbc:mysql://localhost:3306/bham',
user: mySqlUser,
password: mySqlPassword
]
configurations {
sql
}
task connect << {
// This is needed to get mySql driver onto the Groovy/Gradle classpath
configurations.sql.each { file ->
println "Adding URL: $file"
gradle.class.classLoader.addURL(file.toURI().toURL())
}
def sql = groovy.sql.Sql.newInstance(db.url, db.user, db.password, db.driver)
sql.execute("actStatusCodeLkp.sql")
String sqlFilePath = "src/main/resources/sqlscripts/actStatusCodeLkp.sql"
String sqlString = new File(sqlFilePath).text
sql.execute(sqlString)
sql.close()
}
actStatusCodeLkp.sql
insert into act_status_code (id, code, display_name, code_system_name, code_system) values (1, 'active', 'active', 'ActStatus', '2.16.840.1.113883.5.14');
insert into act_status_code (id, code, display_name, code_system_name, code_system) values (2, 'cancelled', 'cancelled', 'ActStatus', '2.16.840.1.113883.5.14');
insert into act_status_code (id, code, display_name, code_system_name, code_system) values (3, 'aborted', 'aborted', 'ActStatus', '2.16.840.1.113883.5.14');
insert into act_status_code (id, code, display_name, code_system_name, code_system) values (4, 'completed', 'completed', 'ActStatus', '2.16.840.1.113883.5.14');
It seems that sql.execute command does not tokenize the file into 4 different insert statements and throws.
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near 'insert into act_status_code (id, code, display_name, code_system_name, code_syst' at line 2
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
It works if I just keep one insert statement in the file. What is the clean work around here, did not really find anything regarding this online. Also, when using maven, I am able to run the same sql file using sql-maven-plugin.
To run SQL script in MySQL, use the MySQL workbench. First, you need to open MySQL workbench. Now, File -> Open SQL Script to open the SQL script. Note − Press OK button twice to connect with MySQL.
You can execute . sql script files in Java using the runScript() method of the ScriptRunner class of Apache iBatis. To this method you need to pass a connection object. Register the MySQL JDBC Driver using the registerDriver() method of the DriverManager class.
Something like this helped me. Notice getting allowMultiQueries: 'true' in the properties
def props = [user: grailsApplication.config.dataSource.username, password: grailsApplication.config.dataSource.password, allowMultiQueries: 'true'] as Properties
def url = grailsApplication.config.dataSource.url
def driver = grailsApplication.config.dataSource.driverClassName
def sql = Sql.newInstance(url, props, driver)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With