troubleshooting Question

How to read data from a mysql database in Java?

Avatar of Tolgar
Tolgar asked on
Java
11 Comments1 Solution812 ViewsLast Modified:
Hi,
I use the following code to read data from a database:

    public List<HashMap<Integer, String>> getFullCheckList() throws ClassNotFoundException, SQLException{
        Class.forName("com.mysql.jdbc.Driver") ;
        Connection conn = DriverManager.getConnection("jdbc:mysql://servername/database", "database", "pwd") ;
        Statement stmt = conn.createStatement() ;
        String querycheckName = "SELECT `name` and `id` FROM `check`;" ;
        ResultSet rs = stmt.executeQuery(querycheckName) ;
        ArrayList<HashMap<Integer, String>> checkList = new ArrayList<HashMap<Integer, String>>();
        while(rs.next()) {
            System.out.println("rs:" + rs);
            HashMap<Integer, String> check = new HashMap<Integer, String>();
            System.out.println("rs.getInt(1): " + rs.getInt(1));
            System.out.println("rs.getString(1): " + rs.getString(1));
            check.put(rs.getInt(1), rs.getString(1));
            System.out.println("check:" + rs);
            checkList.add(check);
        }
        rs.close();
        stmt.close();
        conn.close();
        System.out.println("checkList:" + checkList);
        return checkList;
    }  

However, with this code when I print checkList in line 17, I only see the following:
checkList:[{0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}]
[{0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}, {0=0}]

When I print rs, check, rs.getInt(1) and  rs.getString(1) in lines 9, 11, 12 and 14, I get this:

rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78
rs.getInt(1): 0
rs.getString(1): 0
check: com.mysql.jdbc.JDBC4ResultSet@7cf1bb78

Why can't I read the "name" column from the "check" table in this database? Any ideas?

Thanks,
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 11 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros