java - 为什么包含 blob 的 INSERT INTO 不保留记录

标签 java mysql

我正在使用 Java 和 Serenity 编写自动化测试,需要数据库包含一个文件。我没有成功。

测试将在 Windows、Mac 和可能的 Linux 上的本地主机上运行,​​因此绝对文件名将不起作用。我无法让 INSERT INTO LOAD_FILE 工作;我不知道它的当前文件夹在哪里。

我试过直接编码到数据库。为方便起见,这些文件来自项目文件夹。 (这与使用 Spring/Hibernate HQL 的程序访问不同)我没有收到错误,但记录没有显示在我的测试页面或 MySQL Workbench 中。

MySQL代码:

CREATE TABLE IF NOT EXISTS other_resources (
    id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
    orgid INTEGER NOT NULL,
    res_name VARCHAR(255),
    res_blob LONGBLOB NOT NULL,
    CONSTRAINT fk_other_resources_orgid FOREIGN KEY (orgid) REFERENCES orgs (id) ON DELETE CASCADE
);

我尝试了三种不同的代码组合。所有的都是成功的。我不确定我是否必须复制该文件,或者语句方法是否可以完成。

int orgId = rsOrg.getInt(1);
Path path = FileSystems.getDefault().getPath(".");

File file = new File(path.toString(), "pom.xml");
finStream = new FileInputStream(file);
byte[] fecho = new byte[(int) file.length()];
prepstatem = connection.prepareStatement("INSERT INTO other_resources (orgid, res_name, res_blob) VALUES(?, ?, ?)");
prepstatem.setInt(1, orgId);
prepstatem.setNString(2, "AnotherRes");
prepstatem.setBinaryStream(3, finStream, file.length());
int b = prepstatem.executeUpdate();
finStream.read(fecho);
finStream.close();

File fileT = new File(path.toString(), "serenity.properties");
FileInputStream finST = new FileInputStream(fileT);
PreparedStatement pst = connection.prepareStatement("INSERT INTO other_resources (orgid, res_name, res_blob) VALUES(?, ?, ?)");
pst.setInt(1, orgId);
pst.setNString(2, "SerpropRes");
pst.setBlob(3, finST);
int c = pst.executeUpdate();
System.out.println("INSERT INTO tom " + c);
finST.close();

File fileD = new File(path.toString(), "database.properties");
FileInputStream finsDB = new FileInputStream(fileD);
byte[] dbecho = new byte[(int) fileD.length()];
Blob blobDB = new SerialBlob(dbecho);
PreparedStatement prest = connection.prepareStatement("INSERT INTO other_resources (orgid, res_name, res_blob) VALUES(?, ?, ?)");
prest.setInt(1, orgId);
prest.setNString(2, "DatabaseRes");
prest.setBlob(3, blobDB);
int d = prest.executeUpdate();
System.out.println("INSERT INTO tom " + d);
finsDB.close();

我查询了数据库看是否添加了记录。报告了所有三个文件的 id、res_name 和 res_blob 大小。但是,我正在运行的应用程序没有显示记录,MySQL Workbench 也没有显示记录。

Statement stOR = connection.createStatement();
String queryOR = "SELECT * FROM other_resources";
ResultSet rsOR = stOR.executeQuery(queryOR);
while (rsOR.next())
{
    System.out.println("id " + rsOR.getInt(1));
    System.out.println("orgid " + rsOR.getInt(2));
    System.out.println("name " + rsOR.getString(3));
    System.out.println("blob length " + rsOR.getBlob(4).length());
}

当我在 MySQL Workbench 中使用脚本添加一条记录时,它显示的 id 为 4,如果添加了这 3 个文件,我希望如此。

有什么我遗漏的或关于 MySQL 的古怪之处吗?

最佳答案

当我添加 connection.commit();

时,它开始工作了

关于java - 为什么包含 blob 的 INSERT INTO 不保留记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53509524/

相关文章:

java - 如何在 SFTP 中返回系统类型?

mysql - 是否可以创建一个表,然后在一个查询中从中进行选择?

php - 是否可以在另一个 SELECT 语句中使用 SELECT 语句的结果?

php - 更新 wampserver 中的值

php - 将数据发送到另一个数据库服务器?

java - 使用哈希数组和 Java 8 流对字符串进行排序

java - Jetty.xml导入本地类

java - 识别唯一的字符串

java - for 循环外部变量的动态添加和使用 - Selenium

python - 监听简单表上的插入/更新事件