File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC 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
Bookmark "Syntax error while inserting a file into Mysql database." Watch "Syntax error while inserting a file into Mysql database." New topic
Author

Syntax error while inserting a file into Mysql database.

Vazzdoin Rocko
Ranch Hand

Joined: Feb 24, 2010
Posts: 40
Hi,
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
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

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
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Syntax error while inserting a file into Mysql database.