将结果集写入csv

时间:2013-09-19 09:22:57

标签: java resultset

我正在尝试使用以下代码将结果集写入csv文件,

public static String getstaffid(String var_1, Connection connection,
        FileWriter fw) throws SQLException, Exception
// Create a statement
{
    String record = null;
    ResultSet rs = null;
    Statement stmt = connection.createStatement();
    boolean empty = true;
    try {
        rs = stmt
                .executeQuery("select username, firstname, lastname, middlename, street, city, stateorprovince, ziporpostalcode, countryorregion, fax, phone, extension, mobile, pager, title, primaryemail, secondaryemail, officename, description, comments, suspendeddate, userdata, employeeid, createuser, updateuser, createdate, updatedate, employeetype, servicedeskticketnumber, startdate, enddate, manager, businessapprover, technicalapprover, delegate, location, jobcodes, customproperty1, customproperty2, customproperty3, customproperty4, customproperty5, customproperty6, customproperty7, customproperty8, customproperty9, customproperty10 from globalusers where username = '"+ var_1 + "'");
        ResultSetMetaData metaData = rs.getMetaData();
        int columns = metaData.getColumnCount();


            while (rs.next()) {
            empty = false;
            record = rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4) + " " + rs.getString(5) + " " + rs.getString(6) + " " + rs.getString(7) + " " + rs.getString(8) + " " + rs.getString(9) + " " + rs.getString(10) + " " + rs.getString(11) + " " + rs.getString(12) + " " + rs.getString(13) + " " + rs.getString(14) + " " + rs.getString(15) + " " + rs.getString(16) + " " + rs.getString(17) + " " + rs.getString(18) + " " + rs.getString(19) + " " + rs.getString(20) + " " + rs.getString(21) + " " + rs.getString(22) + " " + rs.getString(23) + " " + rs.getString(24) + " " + rs.getString(25) + " " + rs.getString(26) + " " + rs.getString(27) + " " + rs.getString(28) + " " + rs.getString(29) + " " + rs.getString(30) + " " + rs.getString(31) + " " + rs.getString(32) + " " + rs.getString(33) + " " + rs.getString(34) + " " + rs.getString(35) + " " + rs.getString(36) + " " + rs.getString(37) + " " + rs.getString(38) + " " + rs.getString(39) + " " + rs.getString(40) + " " + rs.getString(41) + " " + rs.getString(42) + " " + rs.getString(43) + " " + rs.getString(44) + " " + rs.getString(45) + " " + rs.getString(46) + " " + rs.getString(47);
            fw.append(rs.getString(1));
            fw.append(',');
            fw.append(rs.getString(2));
            fw.append(',');
            fw.append(rs.getString(3));
            fw.append('\n'); 
            }
            System.out.println(record);
        }
         finally {
        fw.flush();
        rs.close();
        stmt.close();
    }

    return record;
}

这完全没问题。但是要检索字符串记录,我必须写一些类似的东西 record = rs.getString(1)+“”+ ........ +“”+ rs.getString(47)。如果对于喜欢说明天如果查询请求更多属性我必须添加更多rs.getString(48)等等。

所以我写了一个类似下面的函数,

for (int i = 1; i <= columns; i++) {
                 record = rs.getString(i) + " ";
                  }

其中列的值

ResultSetMetaData metaData = rs.getMetaData();
    int columns = metaData.getColumnCount();

但现在我得到了

空 空值 。 。 。 空。

而不是附加fw.append(rs.getString(1))..... fw.append(rs.getSting(n)),是否有一个小函数我可以编写以避免添加更多这些追加

非常感谢

2 个答案:

答案 0 :(得分:0)

修正了它。谢谢。对于可以使用它的人,我附上代码

public static String getstaffid(String var_1, Connection connection,
        FileWriter fw) throws SQLException, Exception
// Create a statement
{
    String record = null;
    ResultSet rs = null;
    Statement stmt = connection.createStatement();
    boolean empty = true;
    try {
            rs = stmt
                .executeQuery("select username, firstname, lastname, middlename, street, city, stateorprovince, ziporpostalcode, countryorregion, fax, phone, extension, mobile, pager, title, primaryemail, secondaryemail, officename, description, comments, suspendeddate, userdata, employeeid, createuser, updateuser, createdate, updatedate, employeetype, servicedeskticketnumber, startdate, enddate, manager, businessapprover, technicalapprover, delegate, location, jobcodes, customproperty1, customproperty2, customproperty3, customproperty4, customproperty5, customproperty6, customproperty7, customproperty8, customproperty9, customproperty10 from globalusers where username = '"+ var_1 + "'");
            ResultSetMetaData metaData = rs.getMetaData();
            int columns = metaData.getColumnCount();


            while (rs.next()) 
            {
                empty = false;
                for (int i = 1; i <= columns; i++) 
                {
                 record = rs.getString(i);
                 fw.append(rs.getString(i));
                 fw.append(',');
                }
                fw.append('\n');

            }
        }
        finally 
        {
            fw.flush();
            rs.close();
            stmt.close();
        }

    return record;
}           

答案 1 :(得分:0)

尝试使用查询

 "SELECT id,text,price into OUTFILE  '"+your-filename+
                    "' FIELDS TERMINATED BY ',' FROM testtable t"