📅  最后修改于: 2020-10-13 01:29:30             🧑  作者: Mango
PreparedStatement的setCharacterStream()方法用于将字符信息设置到parameterIndex中。
1) public void setBinaryStream(int paramIndex,InputStream stream)throws SQLException |
2) public void setBinaryStream(int paramIndex,InputStream stream,long length)throws SQLException |
为了将文件存储到数据库中,表中使用了CLOB(字符大对象)数据类型。例如:
CREATE TABLE "FILETABLE"
("ID" NUMBER,
"NAME" CLOB
)
/
import java.io.*;
import java.sql.*;
public class StoreFile {
public static void main(String[] args) {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
PreparedStatement ps=con.prepareStatement(
"insert into filetable values(?,?)");
File f=new File("d:\\myfile.txt");
FileReader fr=new FileReader(f);
ps.setInt(1,101);
ps.setCharacterStream(2,fr,(int)f.length());
int i=ps.executeUpdate();
System.out.println(i+" records affected");
con.close();
}catch (Exception e) {e.printStackTrace();}
}
}