Java - 从MySQL获取乱码结果(List)

时间:2014-04-17 04:24:58

标签: java mysql arraylist

结果应该像[1,2,3,4], 但我得到像这样的乱码结果

[com.test.db.Network@383c7b61, com.test.db.Network@7f87898,
 com.test.db.Network@6b93f47a, com.test.db.Network@50fb09cc]

以下是我的相关课程

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));

......
}

这是来自另一个班级

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;
}

有什么想法吗?请帮忙。

1 个答案:

答案 0 :(得分:1)

如果要直接打印对象列表,则需要覆盖toString()类中的Network方法,并指定在直接打印此类对象时如何打印值。

默认toString()方法如Object class docs.所示,查看this post about toString()