1

我是一名初学者,并且在 JAVA 中对我的数据库进行了多次成功查询,但我的一个查询返回了我不理解的内容。打印的是: [Ljava.lang.String;@6b081032 实际上应该打印的是条件名称。因此,如果我的查询假设返回 7 个条件名称,它将打印“[Ljava.lang.String;@6b081032”七次。

下面是我正在测试我一直遇到问题的方法的代码。“[Ljava.lang.String;@6b081032”是什么意思?谢谢。

public class Test {
 static String url = "jdbc:mysql://localhost:3306/masters";  //providing the host, port and database name
    static String username = "christine";
    static String password = "password";

    static PreparedStatement pst = null;
    static ResultSet rs = null;

    public static void main(String[] args) throws IOException{

        String[] test = dxNameByName(2);
            for(int i=0; i<test.length; i++)
                {   
                    System.out.println(test);
                }

}


public static String[] dxNameByName(int num){

    Connection con = null;
    ArrayList<String> list= new ArrayList<String>();     
    String[] result = new String[list.size()];           

    try{
        con = DriverManager.getConnection(url, username, password);  //establishes a connection to the database
        pst = con.prepareStatement("SELECT * FROM diagnosis WHERE  diag_region_fk = '" + num+ "';");                                                                                                                                                
        rs = pst.executeQuery(); 

            while (rs.next())                   
            {                                   
                list.add(rs.getString("diagnosis_name"));
            }
         result = list.toArray(result);     
         }catch(SQLException e){     
             e.printStackTrace();    
         }
             finally{
        try { if (rs != null) rs.getStatement().close(); } catch (Exception e) {};
        try { if (pst != null) pst.close(); } catch (Exception e) {};
        try { if (con != null) con.close(); } catch (Exception e) {};
    }

return result;

}
}
4

2 回答 2

1

您正在打印数组本身,而不是数组的内容。使用Arrays.toString()

System.out.println(Arrays.toString(test));

这给出了最好看的输出恕我直言(例如:)[something, something, something]


您还可以遍历所有值并一次打印一个:

for(int i=0; i<test.length; i++)
{   
    System.out.println(test[i]);
}

虽然输出不太好,因为每个内容都打印在它自己的行上。

于 2013-04-28T21:15:56.610 回答
1
[Ljava.lang.String;@6b081032

这意味着打印的变量类型是String[]

为了输出查询结果,请将您的代码更改为以下内容:

System.out.println(test[i]);
于 2013-04-28T21:15:57.573 回答