java - 为什么包含 blob 的 INSERT INTO 不保留记录
问题描述
我正在使用 Java 和 Serenity 编写自动化测试,并且需要数据库包含一个文件。我没有成功。
测试将在 Windows、Mac 和可能的 Linux 上的 localhost 上运行,因此绝对文件名将不起作用。我无法让 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
);
我尝试了三种不同的代码组合。都是成功的。我不确定我是否必须复制文件或 Statement 方法是否会这样做。
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 的一些奇怪的东西吗?
解决方案
I got it to work when I added connection.commit();
推荐阅读
- asp.net - WAF is blocking ASP.NET website due to Scriptresource.axd
- node.js - Pro.find 不是猫鼬模型导出中的函数错误
- git - 需要帮助合并断开连接的 git 存储库
- css - React 本机 android 文本具有额外的底部填充
- jquery - 关闭身体点击动画
- node.js - LoopBack 将查找结果保存到变量中
- r - R : 从 rpart 预测到单行结果
- neo4j - Cypher 查询:获取符合条件的行详细信息
- java - 将 Google Snapshot API 集成到 Android 应用程序会导致 com.google.android.gms.common.api.ResolvableApiException: 4: 4:
- python - 导入和使用年、月、日值的 Python 日期时间问题