您可以使用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();
}