2014-09-29 71 views
2

當我從數據庫中讀取blob數據時,得到的值爲null。什麼可能是這個問題?有人能幫助我嗎?getBinaryStream問題(索引)

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(); 

     } 

回答

1

我相信你沒有使用setString功能分配任何價值姓從而導致空

例如:

ps.preparedStatement("Select photo from person where firstname = ?"); 
ps.setString(1,"kick"); <----- add this line 
system.out.println("bytes "+rs.getBinaryStream(1)); 

另一個建議

沒有必要使用rs = null;內部try catch塊,因爲你的代碼在 開頭有rs=null;

變化

InputStream inputStream = null; 

InputStream inputStream = new InputStream(); 

擺脫InputStream inputStream = null;

source you should take a look at

+0

感謝您的回答。我更新了我的問題,請通過。儘管我遇到了同樣的問題,但我已經完成了你所提到的任何事情。 – Nani 2014-09-29 08:13:12

+0

@Nani可以更改InputStream inputStream = null;到InputStream inputStream = new InputStream()plz? – 2014-09-29 08:16:30

+0

@Nani擺脫rs = null;在你的try catch塊內 – 2014-09-29 08:18:49

0

最Ô bvious錯誤是使用setLong而不是setString。

但是,一種做法是致命的:提前宣佈。在其他語言中這是一種很好的做法,但是在java中應該聲明儘可能接近。

這縮小了範圍,通過這個範圍你會發現錯誤!即在循環之外,在失敗的rs.next()之後調用inputStream。也許是因爲沒有找到記錄。

這種實踐聲明儘可能接近,也有助於嘗試使用資源,這裏用來自動關閉語句和結果集。

Connection con = null; 
try { 
    Class.forName("oracle.jdbc.driver.OracleDriver"); 
    con = DriverManager.getConnection(
      "jdbc:oracle:thin:@MyDatabase:1535:XE","password","password"); 
    System.out.println("connection established"+con); 
    try (PreparedStatement psStmt = con.prepareStatement(
      "Select Photo from Person where Firstname=?")) { 
     int i = 1; 
     psStmt.setString(1, "Nani"); 
     try (ResultSet rs = psStmt.executeQuery()) { 
      while (rs.next()) { 
       try (InputStream inputStream = rs.getBinaryStream(1)) { 
        //Blob blob = rs.getBlob(1); 
        //Blob blob1 = (Blob)rs.getObject(1); 
        //System.out.println("blob length "+blob1);//rs.getString(1); 
        Files.copy(inputStream, Paths.get("C:/photo-" + i + ".jpg")); 
       } 
       ++i; 
      } 
      //ERROR System.out.println("bytessssssss "+inputStream); 
     } // Closes rs. 
    } // Closes psStmt. 
} 
+1

@KickButtowski絕對的點,但是我也看到inputStream被保留_after_ rs.next循環;因此我的答案。另外,因爲我想糾正可能成爲內嵌的聲明風格。 – 2014-09-29 08:45:13

0

1-在您的代碼中設置SQL查詢的參數值時,請務必使用該字段的適當數據類型。所以在這裏,你應該使用的

psStmt.setString(1, "Nani"); 

代替

psStmt.setLong(1, "Nani"); 

2-確保查詢是正確的(表名,字段名)。

3-確保該表包含數據。