我正在执行从 oracle 数据库到 postgres 9.0 数据库的数据迁移。所有字段都已成功传输,除非我试图移动一列文件(Blob 对象在Oracle) 到 postgres(postgres 中的 bytea)。这是我得到的异常
org.postgresql.util.PSQLException: ERROR: syntax error at or near "",#\\034\\034(7),01444\\037\'9=82<.342\\377\\333\\000C\\001\\011\\011\\011\\014\\013\\014\\030\\015\\015\\0302!\\034!22222222222222222222222222222222222222222222222222\\377\\300\\000\\021\\010\\000D\\0004\\003\\001""
下面是我用来将文件存储在数据库中的代码:
Class.forName("org.postgresql.Driver");
destDatabaseconnection = DriverManager.getConnection(
rb.getString("DESTINATION_DATABASE_CONNECTION_URL"),
rb.getString("DESTINATION_DATABASE_CONNECTION_USERNAME"),
rb.getString("DESTINATION_DATABASE_CONNECTION_PASSWORD"));
File file = new File("d://img//10090.gif");
System.out.println(file.isFile());
FileInputStream fis = new FileInputStream(file);
prepstmt = destDatabaseconnection
.prepareStatement("insert into entps.emp_photos(emp_number,emp_photo) values (?,?)");
prepstmt.setInt(1, 1);
prepstmt.setBinaryStream(2, fis, (int) file.length());
int check = prepstmt.executeUpdate();
System.out.println(check);
如果您曾经在 postgres 9.0
中存储过文件,请告诉我最佳答案
只是为了好玩,我刚刚创建了一个表(在 Postgres 9.2 中)
CREATE TABLE test
(
id integer NOT NULL,
file bytea,
CONSTRAINT id PRIMARY KEY (id)
)
并已成功向其上传文件(使用驱动程序 8.3 和 9.3):
public static void main(String[] args) throws Exception {
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection("jdbc:postgresql:test", "postgres", "");
File file = new File("/tmp/q");
FileInputStream fis = new FileInputStream(file);
PreparedStatement pstmt = conn
.prepareStatement("insert into test(id,file) values (?,?)");
pstmt.setInt(1, 1);
pstmt.setBinaryStream(2, fis, (int) file.length());
int check = pstmt.executeUpdate();
System.out.println(check);
}
请检查您的驱动程序。
关于sql - 将文件插入 Postgres 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20756063/