ITPub博客

首页 > 数据库 > 数据库开发技术 > 使用Java实现在SQLserver中实现图片的存储

使用Java实现在SQLserver中实现图片的存储

原创 数据库开发技术 作者:tonyscau 时间:2008-04-22 14:52:58 0 删除 编辑

使用Java实现在SQLserver中实现图片的存储

Connection conn = null;
try{
DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://IP:1433;DataBaseName=Moftec","sa","mymm");
}
catch(Exception e )
{
//捕捉异常并抛出IOException
out.println("Can not connec to the database! The exception is " + e.toString());
}
FileInputStream fis=null;
File file = new File("E:/java/chart.jpg");
try{
fis = new FileInputStream(file);
}catch(FileNotFoundException e){
out.println("Not find file!");
}
PreparedStatement ps = conn.prepareStatement("Insert into gs_img (lei,years,img) values (?,?,?)");
ps.setString(1,"ivan");
ps.setInt(2,4);
ps.setBinaryStream(3,fis,(int)file.length());
ps.executeUpdate();
ps.close();
try{
fis.close();
out.println("写进去了!");
}catch(IOException e){
out.println("fis cann't cloase!");
}
[@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7558084/viewspace-1002830/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-04-24

  • 博文量
    21
  • 访问量
    64995