Connection conn = getDBConnection(); //MYSQL CONNECTION.
conn.prepareStatement("use testspl").execute();
conn.prepareStatement("SOURCE c:\Test.sql").execute()
Is this right way to fire a queries in mysql?
I tried but its not working, I’m getting the error:
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 C:\New.sql'”
Can someone help me figure out where I’ve gone wrong?
JDBC doesn’t work like that: It handles only pure SQL – not mysql command prompt "utility" commands like you are trying to execute.
Problem with the first command:
The choice of database should be part of the connection properties in
getDBConnection(); your connection should already be to the database you want.Problem with the second command:
"SOURCE somefile.sql"is not valid SQL – it’s mysql-only "funk".Try reading the contents of the file
c:\Test.sqlthen passing that as a String toprepareStatement()You might want to try using apache commons IO
FileUtils.readFileToString()to read the file in – it’s a breeze to use