wood burning stoves
The moose likes JDBC and Relational Databases and the fly likes Syntax error while inserting a file into Mysql database. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Syntax error while inserting a file into Mysql database." Watch "Syntax error while inserting a file into Mysql database." New topic

Syntax error while inserting a file into Mysql database.

Vazzdoin Rocko
Ranch Hand

Joined: Feb 24, 2010
Posts: 40
I'm trying to insert an .exe file into mysql blob field in the database. I;ve tried to both ways i.e LOAD_FILE method and FileInputStream method at the local and remote Database server,
but it is consistently giving an error.The stacktrace of the error is : :
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 'binary) values('abc','19541a2746e08a6b8f5145bdbaa23e45','http:jadhjkhdf/dasdas',' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3562)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3494)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1960)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2114)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2696)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2105)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2398)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2316)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2301)
at temp1.copyFile.main(copyFile.java:45)

The code i'm using is given below :

Please help me in resolving this issue. I guess my code is all correct and there is some ado with the Mysql version.
The current mysql version shown by my RHEL machin is :
mysql Ver 14.12 Distrib 5.0.22, for redhat-linux-gnu (i686) using readline 5.0
Martin Vajsar

Joined: Aug 22, 2010
Posts: 3718

I have no experience with MySQL, but according to its documentation binary is a reserved word.

You should escape the column name (probably enclosing it in back quotes), though changing the column name to something which is not a reserved word would be much better in the long run.
Vazzdoin Rocko
Ranch Hand

Joined: Feb 24, 2010
Posts: 40
Thanks Mr. Martin.
My problem has been resolved. i already knew that Binary is a keyword but never thought that it would affect column names too.. i thought it's scope is upto Table names only. Well , it's not a problme any more as i've renamed the column and my code is working fine now. Thank you very much for the guidance
Rob Spoor

Joined: Oct 27, 2005
Posts: 20080

You don't need to cast fis to InputStream, it already is one - FileInputStream extends InputStream.

How To Ask Questions How To Answer Questions
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: Syntax error while inserting a file into Mysql database.
It's not a secret anymore!