将 blob 数据插入数据库
Posted
技术标签:
【中文标题】将 blob 数据插入数据库【英文标题】:insert blob data in to database 【发布时间】:2014-11-19 11:23:05 【问题描述】:认为我对这段代码有一点问题: 当我尝试在数据库中插入该值时,我收到了一个错误。
PS:我尝试使用:
setBinaryStream(int parameterIndex, InputStream x) - 将根据需要从 InputStream 中读取数据,直到到达文件末尾。这是在 JDBC 4.0 (Java 1.6) 中添加的。
setBinaryStream(int parameterIndex, InputStream x, int length) - 根据需要从 InputStream 中读取“length”字节的数据。
setBinaryStream(int parameterIndex, InputStream x, long length) - 根据需要从 InputStream 中读取“length”字节的数据。这是在 JDBC 4.0 (Java 1.6) 中添加的。
但每次我收到一个简单的错误。 我不知道在做什么......我是一个代码猴子......
这是类:
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();
这是错误信息:
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)
【问题讨论】:
尝试使用PreparedStatement.setInputStream()
而不是setBlob()
也许会有所帮助***.com/questions/8348427/…
@a_horse_with_no_name 对于 PreparedStatement 类型,方法 setInputStream() 未定义
我将关闭它,因为@prsmax 提供的链接似乎很有帮助。
对不起,我的意思是setBinaryStream()
【参考方案1】:
我最初认为这是驱动程序中的错误的评估可能是错误的。最可能的问题是您在查询中使用了未引用的词 blob
。 blob
是 reserved word,因此在用作对象名称时需要引用。
如果您将查询更改为:
String sql = "insert into `blob` (data) values (?)";
【讨论】:
以上是关于将 blob 数据插入数据库的主要内容,如果未能解决你的问题,请参考以下文章