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
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of mrcoffee365
mrcoffee365
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Among other things, you cannot retrieve the same value from the resultset twice.
What makes you say that?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

??
Avatar of mrcoffee365
mrcoffee365
Flag of United States of America image

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.
Avatar of Tolgar
Tolgar

ASKER

@CEHJ: So how should it be then?
Avatar of mrcoffee365
mrcoffee365
Flag of United States of America image

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

ASKER

It is all set. Thanks!

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

https://www.experts-exchange.com/questions/28230910/Null-pointer-exception-in-unselecting-checkbox-in-Java.html

Thanks,
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of Tolgar
Tolgar

ASKER

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/questions/28230910/Null-pointer-exception-in-unselecting-checkbox-in-Java.html?anchorAnswerId=39465367#a39465367
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Can you please take a look at this question?
Sorry - i don't do SWT
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo