0

我正在尝试使用以下代码将结果集写入 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;
}

这工作得很好。但是要检索字符串记录,我必须编写类似记录 = 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)),我可以编写一个小函数来避免添加更多这些附加。

非常感谢

4

2 回答 2

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;
}           
于 2013-09-19T10:04:59.377 回答
0

尝试使用查询

 "SELECT id,text,price into OUTFILE  '"+your-filename+
                    "' FIELDS TERMINATED BY ',' FROM testtable t"
于 2019-10-01T23:44:21.293 回答