1

I'm a beginner and have done multiple successful queries to my database in JAVA but one of my queries is returning something that I don't understand. What's printing is: [Ljava.lang.String;@6b081032 And what actually should be printing are names of conditions. So if my query is suppose to return 7 condition names, it will print "[Ljava.lang.String;@6b081032" seven times.

Below is my code where I'm testing out a method that I've been having a problem with. What does "[Ljava.lang.String;@6b081032" mean? Thanks.

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;

}
}
1
  • Why make the computer go to all of the trouble of building an ArrayList, and then just return an array? Commented Apr 28, 2013 at 21:18

2 Answers 2

1

You are printing the array itself, not the contents of the array. Use Arrays.toString():

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

That gives the nicest looking output IMHO (example: [something, something, something]).


You could also iterate through all the values and print them one at a time:

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

The output is a little less nice though, since each content is printed on it's own line.

Sign up to request clarification or add additional context in comments.

Comments

1
[Ljava.lang.String;@6b081032

this means that the printed variable type is String[]

In order to output the result of the query, change your code to the following:

System.out.println(test[i]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.