Java 如何在 jdbc 中使用 ResultSet.getBinaryStream() 从所有列中获取值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24943894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:53:17  来源:igfitidea点击:

How do you get values from all columns using ResultSet.getBinaryStream() in jdbc?

javasqljdbcdb2resultset

提问by Harish Vangavolu

How do I to write an entire table to a flat file (text file) using jdbc? So far I've attempted the following:

如何使用 jdbc 将整个表写入平面文件(文本文件)?到目前为止,我已经尝试了以下几点:

Statement statement = connection.createStatement();
   ResultSet result = statement.executeQuery("SELECT * FROM tablename");
   BufferedInputStream buffer;
   FileOutputStream out = new FileOutputStream("flatfile.txt");
   while(result.next() )
   {
      buffer =  new BufferedInputStream(result.getBinaryStream("????") );
      byte[] buf = new byte[4 * 1024]; //4K buffer
      int len;
      while( (len = buffer.read(buf, 0, buf.length) ) != -1 )
      {
          out.write(buf, 0, len );
      }
   }
   out.close();

"????" is just my placeholder. I am stuck on what to pass in as an argument.

“???” 只是我的占位符。我被困在什么作为参数传递。

采纳答案by Adarsh

You can get all the column names and the entire data from your table using the code below. writeToFile method will contain the logic to writing to file (if that was not obvious enough :) )

您可以使用以下代码从表中获取所有列名和整个数据。writeToFile 方法将包含写入文件的逻辑(如果这还不够明显:))

    ResultSetMetaData metadata = rs.getMetaData();
    int columnCount = metadata.getColumnCount();    
    for (int i = 1; i <= columnCount; i++) {
        writeToFile(metadata.getColumnName(i) + ", ");      
    }
    System.out.println();
    while (rs.next()) {
        String row = "";
        for (int i = 1; i <= columnCount; i++) {
            row += rs.getString(i) + ", ";          
        }
        System.out.println();
        writeToFile(row);

    }

回答by sendon1982

result.getBinaryStream("????")will only return for the value for that column as you put as placeholder.

result.getBinaryStream("????")只会在您作为占位符放置时返回该列的值。

If you want to get all the column, you need to use ResultSetMetaDatafrom ResultSet

如果要获取所有列,则需要使用ResultSetMetaDatafromResultSet

    ResultSetMetaData metadata = resultSet.getMetaData();
    int columnCount = metadata.getColumnCount();
    for (int i=1; i<=columnCount; i++) 
    {
        String columnName = metadata.getColumnName(i);
        System.out.println(columnName);
    }

回答by rogerdpack

Here's how I dump a table from a JDBC connection, very useful for debugging if you want to see all rows that are in an in memory (ex: HSQL) DB for instance:

这是我从 JDBC 连接转储表的方法,如果您想查看内存(例如:HSQL)数据库中的所有行,这对于调试非常有用:

  public static void spitOutAllTableRows(String tableName, Connection conn) {
    try {
      System.out.println("current " + tableName + " is:");
      try (PreparedStatement selectStmt = conn.prepareStatement(
              "SELECT * from " + tableName, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
           ResultSet rs = selectStmt.executeQuery()) {
        if (!rs.isBeforeFirst()) {
          System.out.println("no rows found");
        }
        else {
          while (rs.next()) {
            for (int i = 1; i < rs.getMetaData().getColumnCount() + 1; i++) {
              System.out.print(" " + rs.getMetaData().getColumnName(i) + "=" + rs.getObject(i));
            }
            System.out.println("");
          }
        }
      }
    }
    catch (SQLException e) {
      throw new RuntimeException(e);
    }
  }

output is like

输出就像

 current <yourtablename> is:
 ID=1 COLUMN1=abc COLUMN2=null
 ID=2 COLUMN1=def COLUMN2=ghi
 ...