简体   繁体   English

Java - 如何从MySQL中检索多个blob图像

[英]Java -How to retrieve multiple blob images from MySQL

I am quite new to java and mysql and I am trying to retrieve multiple (a lot!) blob images from a mysql-database. 我是java和mysql的新手,我正在尝试从mysql数据库中检索多个(很多!)blob图像。
I need some sort of loop-query to get through all of the images. 我需要某种循环查询来完成所有图像。

I started with a tutorial ( http://www.roseindia.net/java/java-get-example/get-blob.shtml ) and this works great for me. 我从一个教程( http://www.roseindia.net/java/java-get-example/get-blob.shtml )开始,这对我很有用。 - keep in mind, that I am looking to get the pictures straight to my computer. - 请记住,我希望将照片直接送到我的电脑。

Do you guys have any suggestions on how to expand the existing code in order to retrieve not one but multiple images? 你们有没有关于如何扩展现有代码的建议,以便检索不是一个而是多个图像?

This is the code I am using: 这是我正在使用的代码:

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

Hint 暗示

Zero int should not be between two 'id' int不应该在两个'id'之间


First You can make some changes in your query and in your GetResult: 首先您可以在查询和GetResult中进行一些更改:

Your query should look like : 您的查询应如下所示:

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

Or you can use IN : 或者您可以使用IN

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

Second To get multiple results, you have to a List instead. 第二个要获得多个结果,您必须使用List

and instead of if(rs.next()) you have to loop throw your result with while(rs.next()) 而不是if(rs.next())你必须循环抛出你的结果while(rs.next())


Third If you want to return your values, you have to use a method instead, and not call your Constructor. 第三,如果要返回值,则必须使用方法,而不是调用构造函数。

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

Forth To avoid any kind of Syntax error, or SQL Injection you have to PreparedStatement instead. 第四要避免任何形式的语法错误,或SQL注入你要的PreparedStatement代替。


Hope this instruction can help you, and gives you an idea, good luck. 希望这条指令可以帮助你,并给你一个想法,祝​​你好运。

Performing multiple queries may be cumbersome and not efficient. 执行多个查询可能是麻烦且不高效的。

You could use a PreparedStatement and specify a IN clause in your query but you could not set a list of parameter with. 您可以使用PreparedStatement并在查询中指定IN子句,但无法设置参数列表。
A simple and efficient alternative way would be to create the query dynamically with as many ? 一种简单而有效的替代方法是使用尽可能多的动态创建查询? as the size of the input list id. 作为输入列表id的大小。 Then execute the query and iterate on the ResultSet and apply the processing unitary that you have used to get the blob from the ResultSet . 然后执行查询并迭代ResultSet并应用您用于从ResultSet blob的处理单元。

A not tested sample code : 未经测试的示例代码:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM