?? 實例.txt
字號:
import java.sql.*;
import java.io.*;
public class pic {
public static void main(String[] args) throws Exception {
// 將圖片寫入流中
File file = new File("d:\\image1.jpg");
int length = (int) (file.length());
FileInputStream fis = new FileInputStream(file);
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//僅jdbc2005和jtds生效,jdbc2000異常
Connection con = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;databasename=bbs", "sa", "");
PreparedStatement stm = con
.prepareStatement("insert into bbs_user(username,uface) values(?,?)");
stm.setString(1, "Neo");
stm.setBinaryStream(2, fis, length); // 把流中內容寫入image字段中
stm.executeUpdate();
fis.close();
stm.close();
System.out.println("DB=>Stream WriteIn Success! ");
// 將流中圖片讀到硬盤上
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from bbs_user");
rs.next();
Blob bb = rs.getBlob(5);// 讀出image字段內容
FileOutputStream fop = new FileOutputStream("e:\\abc.jpg");// 自動建立該文件
fop.write(bb.getBytes(1, (int)bb.length()));// 把blob寫入輸出流中
fop.flush();
fop.close();
st.close();
System.out.println("Stream=>Disk ReadOut Success! ");
con.close();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -