java - getBinaryStream(index) 问题

标签 java oracle jdbc

当我从数据库中读取 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;

source you should take a look at

关于java - getBinaryStream(index) 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26094885/

相关文章:

java - Weblogic 10.3.6 域向导无法创建 JVM

java - 使用 Hibernate 访问 Oracle DBLink 表

java - Hibernate 映射 - 没有@Id 的连接表

java - 如何使用 sslmode= require 通过 jdbc 连接到 Amazon RDS?

mysql - spring mvc不会连接到不同计算机上的mysql数据库

java - 迭代 Hashmap 时如何得到 ConcurrentModificationException?

java - 从 Unicode 字符串中获取字数(任何语言)

java - 为什么我会收到 java.sql.SQLException : ResultSet not open. 操作 'next' 不允许。 java Derby 数据库?

java - do-while 循环什么时候比 while 循环更好?

oracle - 在 GROUP BY 查询中将值聚合到表类型中