你如何在 jdbc 中使用 ResultSet.getBinaryStream() 从所有列中获取值? [英] How do you get values from all columns using ResultSet.getBinaryStream() in jdbc?

查看:25
本文介绍了你如何在 jdbc 中使用 ResultSet.getBinaryStream() 从所有列中获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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.

推荐答案

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

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 :) )

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

    }

这篇关于你如何在 jdbc 中使用 ResultSet.getBinaryStream() 从所有列中获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆