I am coding an application that connects to a localhost. When the application first runs I want it to initialize the Database using this method:
public void initDataBase() {
try {
Statement stm = con.createStatement();
stm.executeQuery("source shippingSQLscript.sql");
} catch (SQLException ex) {
ex.printStackTrace();
}
}
Where shippingSQLscript.sql contains the correct sql statements to insert all the data. However when I run it the method throws:
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 'source shippingSQLscript.sql'
at line 1
I have tried using stm.execute() as well but have had the same result.
You cannot do this with the JDBC driver.
sourceis only a command supported by the MySQL command line tool. See here:Here’s the list of commands for the command-line tool. Most are not supported as JDBC query statements.
You will have to load your SQL commands from the file in your code and send them to JDBC execute methods. Something like: