代码: -
package jdbc;
import java.io.File;
import java.io.FileReader;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Properties;
public class Storing_Clob {
public static void main(String[] args) throws Exception {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Properties p = new Properties();
p.put("user", "system");
p.put("password", "password");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",p);
PreparedStatement pstmt = con.prepareStatement("update myclob set CLOB_VALUE=? where ID = 100");
URL url = Storing_Clob.class.getResource("/images/clob1.txt");
File file = new File(url.toURI());
FileReader fr = new FileReader(file);
pstmt.setCharacterStream(1,fr,(int)file.length());
System.out.println("File size: "+(int)file.length());
System.out.println("No of rows affected: "+pstmt.executeUpdate());
con.close();
}
}
OutPut: - 字符串长度:5395
错误: -
Exception in thread "main" java.sql.SQLException: ORA-01460: unimplemented or unreasonable conversion requested
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java)
at oracle.jdbc.ttc7.Oall7.receive(Oall7.java)
at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java)
at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteFetch(TTC7Protocol.java)
at oracle.jdbc.driver.OracleStatement.executeNonQuery(OracleStatement.java)
at oracle.jdbc.driver.OracleStatement.doExecuteOther(OracleStatement.java)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java)
at jdbc.Storing_Image.main(Storing_Image.java:39)
错误的行: -
System.out.println("No of rows affected: "+pstmt.executeUpdate());
我正在尝试将blob文件(图像)存储到myblob表中。该表只有一个字段 - BLOB。 任何人都可以解释错误的原因以及如何解决? 提前罢了。
答案 0 :(得分:0)
可能面临类型转换问题,请尝试字节而不是 int :
pstmt.setCharacterStream(1,fr,(byte)file.length());