结果应类似于[1,2,3,4]
,
但我得到这样的乱码结果
[com.test.db.Network@383c7b61, com.test.db.Network@7f87898, com.test.db.Network@6b93f47a, com.test.db.Network@50fb09cc]
下面是我的相关类(class)
private static void doLocationList(PrintWriter responseOut) throws Exception
{
//---testing show network ID list
int x=0;
Network network = new Network();
responseOut.println("this is I: " +network.getNetworkID(x));
......
}
这是来自另一个类(class)
public static List<Network> getNetworkID(int networkID) throws Exception
{
List<Network> idList = new ArrayList<Network>();
Connection conn = getConnection();
PreparedStatement Statement = conn.prepareStatement("Select id from network");
ResultSet result = Statement.executeQuery();
while(result.next()) {
Network network = new Network();
network.setId(result.getInt("id"));
idList.add(network);
}
return idList;
}
有什么想法吗?请帮忙。
最佳答案
如果要直接打印对象列表,则需要重写Network
类中的toString()
方法,并指定在直接打印该类的对象。
默认的toString()
方法如Object class docs.所示看看this post about toString()
关于Java - 从MySQL获取乱码结果(列表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23124576/