【问题标题】:PreparedStatement.executeUpdate() not affecting databasePreparedStatement.executeUpdate() 不影响数据库
【发布时间】:2018-01-05 20:15:51
【问题描述】:

我想将值插入到数据库表中。该表有两列:id 和 image。 id 是自增的 int,图像是 blob 类型。这是我的代码:

try {
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection(url, userName, password);
    File imgfile = new File(request.getParameter("img"));
    InputStream fin = new FileInputStream(imgfile);
    PreparedStatement pre = con.prepareStatement("insert into pictures (image) values(?)");
    pre.setBinaryStream(1, fin, imgfile.length());
    int done=pre.executeUpdate();
} catch(SQLException | ClassNotFoundException e) {
    e.printStackTrace();
}

我检查了数据库,没有插入任何内容。服务器输出:

com.mysql.jdbc.MysqlDataTruncation:数据截断:数据太长 第 1 行的“图像”列

【问题讨论】:

  • 很确定 SQLException 被你提出并忽略了。
  • 改了发现异常
  • 你是如何定义列的?是一团吗?
  • 它是一个 blob,列大小 65535,如果有帮助的话
  • 数据太长 - 可能意味着您对 column1 的定义不正确。

标签: java mysql jdbc insert prepared-statement


【解决方案1】:

我修好了。我将image 列的数据类型从blob 更改为longblob 谢谢。

【讨论】:

    猜你喜欢
    • 2016-11-11
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多