简体   繁体   中英

MySQL Database Query in JAVA

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;

}
}

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM