gpt4 book ai didi

java - 如何从数据库生成 .pdf 文件?

转载 作者:行者123 更新时间:2023-12-01 11:21:07 25 4
gpt4 key购买 nike

try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/EMP", "root", "");
PreparedStatement ps = con.prepareStatement("insert into imgtable values(?,?)");
ps.setString(234, "tester");
FileInputStream fin = new FileInputStream("d:\\resume.pdf");
ps.setBinaryStream(2, fin, fin.available());
int i = ps.executeUpdate();
System.out.println(i + " Your details successfully uploaded");
con.close();
} catch (Exception e) {
e.printStackTrace();
}

使用此代码我已将 pdf 文件插入数据库,它工作正常,请帮助进行进一步处理。如何从数据库下载 pdf 文件。使用 blob 数据类型。

最佳答案

stmt = con.prepareStatement("SELECT myimg from imgtable where mykey = ?");

stmt.setString(1, "tester");
rs = stmt.executeQuery();
if (!rs.next()) {
System.out.println("File not found in the database.");
} else {
Blob b = rs.getBlob(1);
BufferedOutputStream os;
File f = new File("d:\\retrievedResume.pdf");


os = new BufferedOutputStream(new FileOutputStream(f));
os.write(b.getBytes(0, (int) b.length()), 0, (int) b
.length());
os.flush();
os.close();
}

关于java - 如何从数据库生成 .pdf 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31220550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com