使用Java實現在SQLserver中實現圖片的儲存

tonyscau發表於2008-04-22

使用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部落格 ” ,連結:http://blog.itpub.net/7558084/viewspace-1002830/,如需轉載,請註明出處,否則將追究法律責任。

相關文章