package TEST_BLOB;
import java.sql.;
import java.io.;
public class GET_BLOB {
public static void main(String[] args) {
{
try{
FileInputStream file = new FileInputStream(“D:\test2.jpg”);
Class.forName(“dm.jdbc.driver.DmDriver”);
String userName = “SYSDBA”;
String password = “SYSDBA”;
Connection conn = DriverManager.getConnection(“jdbc:dm://localhost:5236”,userName,password);
PreparedStatement ps = conn.prepareStatement(“insert into t values(?)”);
ps.setBinaryStream(1, file, file.available());
ps.executeUpdate();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(“select blob_column from t”);
while(rs.next()){
Blob blob = rs.getBlob(1);
InputStream in = blob.getBinaryStream();
FileOutputStream fout = new FileOutputStream(“D:\copy.jpg”);
int b = -1;
while((b=in.read())!=-1){
fout.write(b);
}
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}}
本文展示了一个使用Java进行Blob数据操作的示例,包括从文件读取数据并插入到数据库,以及从数据库中检索Blob数据并写回到文件。示例使用了DmDriver驱动连接数据库,演示了PreparedStatement和ResultSet的使用。
4301

被折叠的 条评论
为什么被折叠?



