public class Demo4 {
public static void main(String[] args) throws Exception{
//1获取连接
Connection conn = DbUtils.getConnection();
//2创建命令对象
PreparedStatement pstat = conn.prepareStatement("select * from bigdata where id=?");
//3设置参数
pstat.setInt(1, 1);
//4执行
ResultSet rs = pstat.executeQuery();
//5处理
if(rs.next()){
Reader reader = rs.getCharacterStream("content");
InputStream is = rs.getBinaryStream("photo");
//处理 content
FileWriter fw=new FileWriter("src\\empcopy.xml");
char[] buf=new char[1024];
int len=0;
while((len=reader.read(buf))!=-1){
fw.write(buf,0,len);
fw.flush();
}
fw.close();
reader.close();
//处理图片
FileOutputStream fos=new FileOutputStream("src\\004.jpg");
byte[] buf2=new byte[1024];
int len2=0;
while((len2=is.read(buf2))!=-1){
fos.write(buf2,0,len2);
fos.flush();
}
fos.close();
is.close();
}
rs.close();
pstat.close();
conn.close();
System.out.println("执行完毕");
}
}
从根据id数据库中读取数据,并写入另一个文档,实现复制
最新推荐文章于 2023-10-07 14:03:13 发布
本文介绍了一个使用Java进行数据库操作的示例,展示了如何通过PreparedStatement执行SQL查询,读取字符流和二进制流数据,并将数据写入本地文件。具体包括获取数据库连接,设置SQL参数,执行查询,处理查询结果,读取并保存文本和图片数据。
3万+

被折叠的 条评论
为什么被折叠?



