I am trying to run an SQL script in a java application using JDBC and HikariConfig.
CREATE TABLE table_AA(NAME VARCHAR(50) NOT NULL);
CREATE PROCEDURE `ASDF`()
BEGIN
SELECT * FROM table_AA;
end;
Code in java used to run the SQL code
package YourPluginName.Storage;
import YourPluginName.Main.Main;
import org.apache.ibatis.jdbc.ScriptRunner;
import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;
public class DatabaseSetup {
public static boolean setupTables() {
try (Connection connection = SQLPool.getConnection()) {
System.out.println("Connection established......");
//Initialize the script runner
ScriptRunner sr = new ScriptRunner(connection);
//Creating a reader object
Reader reader = new BufferedReader(new InputStreamReader(Main.getPlugin().getClass().getResourceAsStream("/SQL_SETUP.sql")));
//Running the script
sr.runScript(reader);
} catch (SQLException e) {
e.printStackTrace();
}
return true;
}
private static File getFile(String fileName) {
ClassLoader classLoader = Main.getPlugin().getClass().getClassLoader();
File file = new File(classLoader.getResource(fileName).getFile());
return file;
}
}
And for reference the definition of SQLPool
package YourPluginName.Storage;
import YourPluginName.Main.Main;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.bukkit.configuration.file.FileConfiguration;
import java.sql.Connection;
import java.sql.SQLException;
public class SQLPool {
private static String host, username, database, password;
private static int port;
private static HikariConfig config = new HikariConfig();
private static HikariDataSource ds;
static {
try {
FileConfiguration config = Main.getPlugin().getConfig();
host = config.getString("host");
port = config.getInt("port");
database = config.getString("database");
username = config.getString("username");
password = config.getString("password");
} catch (Exception e) {
e.printStackTrace();
Main.log().error("Could not load MySQL data from configuration file");
}
if (host == null) {
Main.log().error("Host is null");
}
if (database == null) {
Main.log().error("Database is null");
}
if (username == null) {
Main.log().error("Username is null");
}
if (password == null) {
Main.log().error("Password is null");
}
Main.log().log("Connecting to HOST: " + host + " on PORT: " + port + " FOR DATABASE " + database);
config.setJdbcUrl("jdbc:mysql://" + host + ":" + port + "/" + database + "");
config.setUsername(username);
config.setPassword(password);
config.setDriverClassName("com.mysql.jdbc.Driver");
config.addDataSourceProperty("autoReconnect", true);
config.addDataSourceProperty("cachePrepStmts", true);
config.addDataSourceProperty("prepStmtCacheSize", 250);
config.addDataSourceProperty("prepStmtCacheSqlLimit", 2048);
config.addDataSourceProperty("useServerPrepStmts", true);
config.addDataSourceProperty("cacheResultSetMetadata", true);
config.setConnectionTimeout(3000);
ds = new HikariDataSource(config);
Main.log().log("DATABASE IS WORKING!");
}
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
public static boolean sendCommand(ThrowingConsumer<Connection> function) {
try (Connection connection = SQLPool.getConnection()) {
try {
connection.setAutoCommit(false);
function.acceptThrows(connection);
connection.commit();
Main.log().log("Successful database transaction");
return true;
} catch (Exception e) {
Main.log().error("Rolled back database transaction");
connection.rollback();
}
} catch (SQLException e) {
e.printStackTrace();
}
Main.log().error("Unsuccessful database transaction");
return false;
}
public static void close() {
try {
ds.close();
} catch (Exception e) {
Main.log().error("A SQLException was caught" + e);
}
}
}
When I run the code, the creation of the table works fine, but the creation of the procedure throws a weird error. I tried finding similar problems online but to no avail.
Cause: 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 '' at line 3
Any advice is appreciated!