0

我想在从函数“LookUpTransaction”返回之前获取以下代码中生成的列表作为字符串。我该怎么办。?

public String[] LookUpTransaction() {
        List list=new ArrayList();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/bank", "root", "root");
            PreparedStatement ps = con
                    .prepareStatement("select accountno,details,amount from transaction");
            rs = ps.executeQuery();
            while(rs.next()){
                list.add(rs.getString(1));
                list.add(rs.getString(2));
                list.add(rs.getString(3));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String[] strarray = new String[list.size()];
        return list.toArray(strarray);
    }
4

2 回答 2

0

您可以使用return Arrays.toString(strarray);在这种情况下,您必须将方法的返回类型更改为String. 那就是你的代码必须修改如下:

public String LookUpTransaction() {
        List list=new ArrayList();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/bank", "root", "root");
            PreparedStatement ps = con
                    .prepareStatement("select accountno,details,amount from transaction");
            rs = ps.executeQuery();
            while(rs.next()){
                list.add(rs.getString(1));
                list.add(rs.getString(2));
                list.add(rs.getString(3));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String[] strarray = new String[list.size()];
        strarray = list.toArray(strarray);
        return Arrays.toString(strarray);
    }

Please note that the Arrays.toString(strarray) returns a string representation of the contents of the specified array (strarray in your scenario). The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (a comma followed by a space). If you are looking to iterate over the list & concatenating the elements you could try something like this:

 public String LookUpTransaction() {
        List list=new ArrayList();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/bank", "root", "root");
            PreparedStatement ps = con
                    .prepareStatement("select accountno,details,amount from transaction");
            rs = ps.executeQuery();
            while(rs.next()){
                list.add(rs.getString(1));
                list.add(rs.getString(2));
                list.add(rs.getString(3));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (String str : list) {
            stringBuilder.append(str);
        }

        return stringBuilder.toString();
    }
于 2013-03-20T05:06:29.760 回答
0

Use List.toArray(), followed by Arrays.toString()

String[] y = x.toArray(new String[0]);
Arrays.toString(y)
于 2013-03-20T05:07:26.050 回答