📄 lobdemo.java
字号:
package onlyfun.caterpillar;
import java.io.*;
import java.sql.*;
public class LobDemo {
public static void main(String[] args) {
DBSource dbsource = null;
Connection conn = null;
PreparedStatement pstmt = null;
try {
dbsource = new SimpleDBSource();
conn = dbsource.getConnection();
//取得文件
File file = new File(args[0]);
int length = (int) file.length();
InputStream fin = new FileInputStream(file);
//填入数据库
pstmt = conn.prepareStatement(
"INSERT INTO t_file VALUES(?, ?, ?)");
pstmt.setInt(1, 1);
pstmt.setString(2, args[0]);
pstmt.setBinaryStream (3, fin, length);
pstmt.executeUpdate();
pstmt.clearParameters();
fin.close();
}
catch(SQLException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
finally {
if(pstmt != null) {
try {
pstmt.close();
}
catch(SQLException e) {
e.printStackTrace();
}
}
}
Statement stmt = null;
try {
// 从数据库取出文件
stmt = conn.createStatement();
ResultSet result = stmt.executeQuery(
"SELECT * FROM t_file");
result.next();
String filename = result.getString(2);
Blob blob = result.getBlob(3);
// 写入文件,文件名附加.bak
FileOutputStream fout =
new FileOutputStream(filename + ".bak");
fout.write(blob.getBytes(1, (int)blob.length()));
fout.flush();
fout.close();
}
catch(SQLException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
finally {
if(stmt != null) {
try {
stmt.close();
}
catch(SQLException e) {
e.printStackTrace();
}
}
if(conn != null) {
try {
dbsource.closeConnection(conn);
}
catch(SQLException e) {
e.printStackTrace();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -