简体   繁体   English

如何使用Blob将文件存储在mysql数据库中

[英]How to store a file in a mysql database using blob

I have searched about this matter a lot but, I didn't get a proper solution for my problem, I have following method for storing a file using java, 我已经搜索了很多有关此问题的信息,但是我没有针对我的问题找到合适的解决方案,我有以下使用Java存储文件的方法,

private void uploadFile() throws Exception {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/FileDb?autoReconnect=true", "user1", "123789");
        System.out.println("DB connection extablished!");

        PreparedStatement statement = connection.prepareStatement("INSERT INTO FilesTb(`FileData`) VAULES(?)");

        File file = new File(txtDir.getText() + "/" + txtFile.getText());// get path and file name from text fields

        FileInputStream fIn = new FileInputStream(file);
        statement.setBlob(1, fIn);
        statement.executeUpdate();
        JOptionPane.showMessageDialog(null, "File stored successfully!");

    }

when I try to run this code I got following error, 当我尝试运行此代码时,出现以下错误,

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 'VAULES(_binary'<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<jasperReport xmlns=\"' at line 1
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
        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 com.views.FileUploader.uploadFile(FileUploader.java:154)
        at com.views.FileUploader.btnUploadActionPerformed(FileUploader.java:125)
        at com.views.FileUploader.access$100(FileUploader.java:27)
        at com.views.FileUploader$2.actionPerformed(FileUploader.java:66)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
        at java.awt.Component.processMouseEvent(Component.java:6504)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
        at java.awt.Component.processEvent(Component.java:6269)
        at java.awt.Container.processEvent(Container.java:2229)
        at java.awt.Component.dispatchEventImpl(Component.java:4860)
        at java.awt.Container.dispatchEventImpl(Container.java:2287)
        at java.awt.Component.dispatchEvent(Component.java:4686)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
        at java.awt.Container.dispatchEventImpl(Container.java:2273)
        at java.awt.Window.dispatchEventImpl(Window.java:2713)
        at java.awt.Component.dispatchEvent(Component.java:4686)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
        at java.awt.EventQueue.access$000(EventQueue.java:101)
        at java.awt.EventQueue$3.run(EventQueue.java:666)
        at java.awt.EventQueue$3.run(EventQueue.java:664)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
        at java.awt.EventQueue$4.run(EventQueue.java:680)
        at java.awt.EventQueue$4.run(EventQueue.java:678)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

please assist me regarding this.. 请为此提供帮助。

将文件数据转换为字节数组并在sql语句中设置

Try this 尝试这个

File fBlob = new File ("<your_path>");
FileInputStream is = new FileInputStream(fBlob);
statement.setBinaryStream(1, is, (int) fBlob.length());
statement.execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java从MySql数据库中检索Blob pdf文件 - How to retrive Blob pdf file from MySql database using java 如何在Google Cloud中使用文件服务将文件存储为Blob值 - How to store a file as blob values using the file service in google cloud 如何使用 HIbernate 作为 BLOB 将此图像保存到 MySQL 数据库? - How to save this image to MySQL database using HIbernate as BLOB? 如何使用JAVA从MySQL数据库中提取BLOB(从2d数组到BLOB) - How to pull BLOB(from 2d array to BLOB) from mySQL database using JAVA 如何使用休眠模式将文件存储在数据库中 - How to store file in database using hibernate 如何将用户配置文件中的图像链接存储到mysql db中(不使用BLOB) - How to store the link of image in user profile into mysql db (without using BLOB) 如何从XML文件中读取数据并将其存储到数据库(MySQL)中? - How to read data from an XML file and store it into database (MySQL)? 使用Angular将文件上传到数据库(blob) - Upload file to database (blob) using Angular 如何使用REST Web服务将图像存储在MySQL数据库中 - How to store an image in MySQL database using REST webservice 如何使用 JAVA 在 MySQL 数据库中存储马拉地语(印度语) - How to store Marathi(Indian lang) in MySQL database using JAVA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM