環境:
DB : mssql 2000
Table schema :
id int,
fileContent image,
path varchar(255);
從檔案加入至資料庫
code:
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433", "cf-intermediate",
"cf-intermediate");
File file = new File("C:/MyEclipse5.1/workspace/myJava/abc.doc");
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
// 將檔案讀入位元陣列
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1];
while (bufferedInputStream.read(bytes) != -1) {
arrayOutputStream.write(bytes);
}
arrayOutputStream.close();
bufferedInputStream.close();
bytes = arrayOutputStream.toByteArray();
System.out.println("bytes="+bytes.length);
PreparedStatement preState = con.prepareStatement("insert into erpglm_append values(?,?,?)");
preState.setBytes(1, bytes);
preState.setString(2, file.getPath());
preState.setString(3, "N");
preState.execute();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
從資料庫取得資料並存成檔案:
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433", "cf-intermediate",
"cf-intermediate");
String sql = "select fileContent from erpglm_append where id=1";
Statement stat = con.createStatement();
ResultSet rs = stat.executeQuery(sql);
byte[] bytes = null;
while (rs.next()) {
bytes = (byte[]) rs.getObject("fileContent");
}
rs.close();
stat.close();
con.close();
System.out.println("bytes="+bytes.length);
BufferedInputStream bufferedInputStream = new BufferedInputStream(new ByteArrayInputStream(bytes));
File file = new File("C:/MyEclipse5.1/workspace/myJava/abc1.doc");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
byte[] data = new byte[1];
while (bufferedInputStream.read(data) != -1) {
bufferedOutputStream.write(data);
}
bufferedOutputStream.flush();
bufferedInputStream.close();
bufferedOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
訂閱:
發佈留言 (Atom)
NGINX SSL/設定檔案
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #...
-
Source: Automatically Retry Failed Jobs in Quartz Retrying continuously until success: If you want to keep trying over and over again un...
-
*主餐類 漢堡王炸雞腿-227大卡 華嫩雞條(6PCS)-259大卡 火烤漢堡-311大卡 火烤吉士漢堡-353大卡 小華堡-397大卡 華雪魚-471大卡 雙層吉士漢堡-534大卡 雙層燒烤培根堡-580大卡 華辣雞腿堡-597大卡 華香雞排堡-685大卡...
-
Download source files from here Upload files to the CentOS server Prepare the necessary files #yum -y install gcc gcc-c++ autoconf lib...
沒有留言:
發佈留言