1

我想将表格的整个数据放入文本文件中,我使用的代码如下

 String url = "jdbc:sqlserver://localhost:1433;databaseName=DBTEST;user=Data;password=123;";
      try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    } catch (ClassNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
try {
      Connection con = DriverManager.getConnection(url);
      Statement select = con.createStatement();
      ResultSet result = select.executeQuery
                          ("select * from testTable");         

      System.out.println("Got results:");
      System.out.println(result);
      FileWriter fw = new FileWriter("I:/File/InputFile.txt"); 
      BufferedWriter bw = new BufferedWriter(fw);
      while(result.next()) 
      {  
          try{
                  bw.write(String.valueOf(result.getFloat("XColumn")));; // \n is for to write in a new Line .  
                  bw.newLine();
          }  
          catch (Exception e) {  
              System.out.println("Errrrr......... writing to a file..... :P"+e);  
          }            
      }  
      bw.close();
      select.close();
      con.close();
    }
    catch( Exception e ) {
      e.printStackTrace();
    }

在这里,我只得到我指定的对应的 column(XColumn) 值result.getFloat("XColumn")。我怎样才能得到整个表的价值?

注意:我的表格格式为 testTable(XColumn,YColumn,ZColumn)。我想要 txt 文件中的所有列值。

4

6 回答 6

3

您可以使用ResultSetMetaData按编号提取所有列:

    ResultSet rs = ...;
    ResultSetMetaData rsmd = rs.getMetaData();
    int columnCount = rsmd.getColumnCount();
    while(rs.next()){
        StringBuilder row = new StringBuilder();
        for(int i = 0; i < columnCount; i++){
            row.append(rs.getObject(i));
        }
    }
于 2013-09-24T11:58:39.430 回答
3

一种选择是只检索每一列,因为您知道它的名称。

如果您不知道它的名称(因为以后有人可能会添加更多列),您可以检索ResultSetMetaData http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getMetaData() .

该元数据将提供查询返回的列的数量、名称和类型ResultSet

于 2013-09-24T11:55:16.137 回答
3

结合来自 ResultSet 的元数据信息以获取列名

ResultSetMetaData metaData = rs.getMetaData();

列中的实际值通过索引获取它们:

metaData.getColumnName(i);
rs.getString(i);
于 2013-09-24T12:00:10.870 回答
2

首先,使用语句是不明智的。为防止sql注入,请使用preparedStatement。

PreparedStatement pt= con.prepareStatement("select * from testTable");
resultSet rs=pt.executeQuery();
while(rs.next())
{
 try{
                  bw.write(String.valueOf(result.getFloat("XColumn")));; // \n is for to write in a new Line .  
                  bw.newLine();
          }  
          catch (Exception e) {  
              System.out.println("Errrrr......... writing to a file..... :P"+e);  
          }   
}

如果不知道列名,则使用ResultSetMetaData

于 2013-09-24T11:56:45.597 回答
1

ResultSet中,您不能一次调用所有行值。你应该分开做。

于 2013-09-24T11:57:54.617 回答
0
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package practice;
import java.io.*;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;


/**
 *
 * @author mahamud.ashik
 */
public class Practice {

    /**
     * @param args the command line arguments
     */
    OracleConnectionHandler dbConne =new OracleConnectionHandler();

    public static void main(String[] args) throws IOException {
        // TODO code application logic here
       try {

           Statement st=OracleConnectionHandler.getConnection().createStatement();
           ResultSet rs=st.executeQuery("select col1,col2,col3 from tableName");


          // for print column name 
           ResultSetMetaData rsmd = rs.getMetaData();


      int numberOfColumns = rsmd.getColumnCount();

      for (int i = 1; i <= numberOfColumns; i++) {
        if (i > 1) System.out.print(",  ");
        String columnName = rsmd.getColumnName(i);
        System.out.print(columnName);
      }
      System.out.println("");
       // write in text file    
            FileWriter fw = new FileWriter("d://stockIn3.txt"); 
      BufferedWriter bw = new BufferedWriter(fw);

           while(rs.next()){
               try {
                   String name=rs.getString("col1");
                   String account_no=rs.getString("col2");
                   String sp_id=rs.getString("col3");

               bw.write(name);
               bw.append(";");
               bw.write(account_no);
               bw.append(";");
               bw.write(sp_id);

               bw.newLine();

               }catch (Exception e){
                   System.out.println("Exception"+e);
               }
           }
           bw.close();st.close();
           }

       catch (Exception e){
          System.out.println(e);
       }
    }
}
于 2013-12-09T11:47:13.283 回答