Avatar of Tolgar
Tolgar asked on

How to read data from a mysql database in Java?

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

Open in new window


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

Open in new window


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

Open in new window


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

Thanks,
Java

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
SOLUTION
CEHJ

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
mrcoffee365

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
CEHJ

Among other things, you cannot retrieve the same value from the resultset twice.
What makes you say that?
CEHJ

??
mrcoffee365

CEHJ -- feel free to try it.  Once a column in a row in the resultset is retrieved with getString (or whatever) then you can't get it again with getString for that row.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER
Tolgar

@CEHJ: So how should it be then?
mrcoffee365

Tolgar:  if you're interested in fixing your code, try the sample I posted.
ASKER
Tolgar

It is all set. Thanks!

And I  would appreciate if you can take a look at this post:

https://www.experts-exchange.com/Programming/Languages/Java/Q_28230910.html

Thanks,
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
CEHJ

CEHJ -- feel free to try it.
I just did, with the following minor alteration to some existing code, with no difficulty whatever:

	    while(rs.next()) {
		//System.out.println(rs.getString(1));
		System.out.printf("%s\t%s%n", rs.getString(1), rs.getString(1));
	    }

Open in new window


@CEHJ: So how should it be then?
Well, my answer was quite specific about the (only) problems with your code
ASKER
Tolgar

ok. I am ending this post. Thanks a lot.

Can you please take a look at this question? I am struggling with it.

https://www.experts-exchange.com/Programming/Languages/Java/Q_28230910.html#a39465367
CEHJ

Can you please take a look at this question?
Sorry - i don't do SWT
Your help has saved me hundreds of hours of internet surfing.
fblack61