2017-04-11 46 views
1

從MySQL數據庫檢索多個團塊圖像我很新的Java和MySQL和我想檢索多個(很多!)斑點圖像。
我需要某種循環查詢才能瀏覽所有圖像。Java的 - 如何從MySQL

我開始用教程(http://www.roseindia.net/java/java-get-example/get-blob.shtml),這對我的偉大工程。 - 請記住,我正在將圖片直接送到我的電腦。

你們是否有關於如何擴大現有的代碼,以便檢索不是一個而是多個圖像有什麼建議?

這是我使用的代碼:

class GetBlob { 
    FileOutputStream image; 
    Connection con = null; 
    PreparedStatement pstmt = null; 
    Statement stmt = null; 
    ResultSet res = null; 
    StringBuffer query = null; 
    String driverName = "com.mysql.jdbc.Driver"; 
    String url = "jdbc:mysql://localhost:3306/";; 
    String dbName = "portal"; 
    String userName = "root"; 
    String password = "xxxx"; 

public GetBlob() { 
    try { 
     Class.forName(driverName); 
     con = DriverManager.getConnection(url + dbName, userName, password); 
     stmt = con.createStatement(); 
     ResultSet rs = stmt.executeQuery("select * from pictures where id='1'"); 
     if (rs.next()) { 
      Blob test = rs.getBlob("picture"); 
      InputStream x = test.getBinaryStream(); 
      int size = x.available(); 
      OutputStream out = new FileOutputStream("/Users/User/Desktop/anu2.jpg"); 
      byte b[] = new byte[size]; 
      x.read(b); 
      out.write(b); 
     } 
    } catch (Exception e) { 
     System.out.println("Exception :" + e); 
    } finally { 
     try { 
      stmt.close(); 
      con.close(); 
     } catch (Exception e) { 
      System.out.println(e); 
     } 
     } 
    } 

public static void main(String args[]) throws IOException { 
    GetBlob blob = new GetBlob(); 
    } 
} 
+0

什麼類型的id'int'或'varchar'? –

+0

哦,對不起,忘了 - int –

+0

你應該通過[Oracle JDBC教程](https://docs.oracle.com/javase/tutorial/jdbc/basics/index.html)。 – vanje

回答

0

提示

int不應該是兩者之間'id'


首先 你可以做出一些改變您的查詢,並在您調用getResult:

您的查詢應該是這樣的:

"select * from pictures" -- without where because if the id is unique you will get only one 

或者你可以使用IN

"select * from pictures where id IN (id1, id2, id3, ...)" 

要獲得多個結果,你有一個List代替。的

,而是if(rs.next())你必須循環如果你想回到你的價值觀與while(rs.next())


扔你的結果,你必須使用一個方法來代替,而不是打電話給你的構造函數。

public static List<Images> getImages(){ 
    ... 
} 

第四 爲了避免出現任何語法錯誤,或SQL注入你要的PreparedStatement代替。


希望這條指令可以幫助你,並給你一個想法,祝​​你好運。

0

執行多個查詢可能是麻煩的並且效率不高。

您可以使用PreparedStatement並在查詢中指定IN子句,但無法設置參數列表。
一個簡單而有效的替代方法是動態創建與輸入列表ID的大小一樣多的?。然後執行查詢並在ResultSet上進行迭代,並應用您用於從ResultSet獲取blob的處理單元。

一個未經測試示例代碼:

List<Integer> ids = ...; // the ids you want to use for your query 
String sql= createQuery(ids);  
PreparedStatement preparedStatement = dbConnection.prepareStatement(sql); 
for (int i=0; i< ids.size(); i++){ 
    int id = ids.get(i); 
    preparedStatement.setInt(i+1, id); 
} 

ResultSet rs = stmt.executeQuery(sql); 

while (rs.next()) { 
    // your actual code 
    Blob test = rs.getBlob("picture"); 
    InputStream x = test.getBinaryStream(); 
    int size = x.available(); 
    OutputStream out = new FileOutputStream("/Users/User/Desktop/anu2.jpg"); 
    byte b[] = new byte[size]; 
    x.read(b); 
    out.write(b); 
} 


private String createQuery(List<Integer> ids){ 
    String query = "select * from pictures where id IN("; 

    for (int i=0; i< ids.size(); i++){   
     if (i>0){ 
      query += ","; 
     } 

     query += "?"; 
    } 

    query += ")"; 
    return query; 
}