当我从数据库中读取 blob 数据时,我得到了 null
值。可能是什么问题?有人可以帮我解决这个问题吗?
Connection con = null;
PreparedStatement psStmt = null;
ResultSet rs = null;
try {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
con =
DriverManager.getConnection("jdbc:oracle:thin:@MyDatabase:1535:XE","password","password");
System.out.println("connection established"+con);
psStmt = con
.prepareStatement("Select Photo from Person where Firstname=?");
int i = 1;
psStmt.setLong(1, "Nani");
rs = null;
rs = psStmt.executeQuery();
InputStream inputStream = null;
while (rs.next()) {
inputStream = rs.getBinaryStream(1);
//Blob blob = rs.getBlob(1);
//Blob blob1 = (Blob)rs.getObject(1);
//System.out.println("blob length "+blob1);//rs.getString(1);
}
System.out.println("bytessssssss "+inputStream);//here i am getting null value.
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
最佳答案
我相信您没有使用 setString
函数将任何值分配给 firstname 导致 null
例如:
ps.preparedStatement("Select photo from person where firstname = ?");
ps.setString(1,"kick"); <----- add this line
system.out.println("bytes "+rs.getBinaryStream(1));
其他建议
没有必要在 try catch block 中使用 rs = null;
因为你在开始时有 rs=null;
你的代码。
改变
InputStream inputStream = null;
到
InputStream inputStream = new InputStream();
或
去掉 InputStream inputStream = null;
关于java - getBinaryStream(index) 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26094885/