简体   繁体   中英

insert blob data in to database

think i have a little problem with this code: when i try to insert in database the value i recived one error.

PS: i try to use :

  1. setBinaryStream(int parameterIndex, InputStream x) - The data will be read from the InputStream as needed until end-of-file is reached. This was added in JDBC 4.0 (Java 1.6).

  2. setBinaryStream(int parameterIndex, InputStream x, int length) - The data will be read from the InputStream as needed for "length" bytes.

  3. setBinaryStream(int parameterIndex, InputStream x, long length) - The data will be read from the InputStream as needed for "length" bytes. This was added in JDBC 4.0 (Java 1.6).

but everytime i recived the simple error. i don t know what a doing ..... ima code monkey...

this is the class:

public class DataServlet extends HttpServlet{
    public void doPost(HttpServletRequest  request,HttpServletResponse response)throws ServletException,IOException {

        Connection conn = ConnectionToDB.connettiDB();                  
        InputStream inputstream = null;         
        Part filepart = request.getPart("image");

        inputstream = filepart.getInputStream();            
        try{
             String sql = "insert into blob (data) values (?)";
             PreparedStatement statement = conn.prepareStatement(sql);               
             if (inputstream != null) {
                    // fetches input stream of the upload file for the blob column
                 statement.setBlob(1, inputstream);
                }

                // sends the statement to the database server
                int row = statement.executeUpdate();
                if (row > 0) {
                    System.out.println( "File uploaded and saved into database");
                }

        } catch (SQLException ex) {
             System.out.println( "ERROR: " + ex.getMessage());
            ex.printStackTrace();}
    }
}

this is the error message:

         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 'blob
     (data) values (_binary'‰PNG

         \0\0\0
         IHDR\0\0\0\0\0\0\0\0\0;0®¢\0\' at line 1
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown
     Source)
            at java.lang.reflect.Constructor.newInstance(Unknown Source)
            at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
            at com.mysql.jdbc.Util.getInstance(Util.java:386)
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
            at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
            at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
            at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
            at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
            at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624)
            at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127)
            at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2427)
            at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2345)
            at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2330)
            at com.CvManagement.src.servlet.DataServlet.doPost(DataServlet.java:69)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
            at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
            at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
            at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
            at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
            at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
            at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
            at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
            at java.lang.Thread.run(Unknown Source)

My initial assessment that this is a bug in the driver is probably wrong. The most likely problem is your use of the unquoted word blob in your query. blob is a reserved word and therefor needs to be quoted when used as an object name.

I'd expect it to work if you change your query to:

String sql = "insert into `blob` (data) values (?)";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM