Hello all,

I have the following code.

    Connection connection = getMySQLConnection();

    try {
        Statement st = connection.createStatement();
        String SQL = "SELECT ID, NAME FROM STUDENT WHERE ID=5";
        ResultSet rs = st.executeQuery(SQL);
        System.out.println("SQL =" + SQL);

        while (rs.next()) {
            // do something...
        }
    } catch (SQLException se) {
        se.pringStackTrace();
    }

From my J2EE appliation, the query doesn't return anything, and it doesn't enter the while() loop.

But, if I run the same query from within my MySQL, I get results alright. I don't know what I am doing wrong?

mysql> SELECT ID, NAME FROM MY_TABLE WHERE id=5;
+------------+-----------------+
| ID         | NAME            |
+------------+-----------------+
| 5          | JOHN DOE        |
+------------+-----------------+
1 row in set (0.01 sec)

Can someone please advise? Thanks in advance.

Recommended Answers

All 3 Replies

Member Avatar for diafol

Not a j2ee guy but isn.t.the place where you print the content just has a comment but no output?

// do something...

Thanks. Here's what happens:

        Connection connection = getMySQLConnection();
        try {
            Statement st = connection.createStatement();
            String SQL = "SELECT ID, NAME FROM STUDENT WHERE ID=5";
            ResultSet rs = st.executeQuery(SQL);
            System.out.println("SQL =" + SQL);
            while (rs.next()) {
                System.out.println("ResultSet has record...");
                // do something...
            }
            System.out.println("Exiting.");
        } catch (SQLException se) {
            se.pringStackTrace();
        }

So the output I get is this:

SQL = SELECT ID, NAME FROM STUDENT WHERE ID=5
Exiting.

Basically, it doesn't enter WHILE loop at all, meaning the ResultSet is empty. But, I know for fact that the record exists in my MySQL DB, and I can query it, using the same SELECT query from my MySQL DB.

Just one bit of information:

The Java code is compiled in Windows platform, using Eclipse Kepler. This works fine in my local DEV environment in Windows platform with local MySQL DB, App Server (Tomcat) etc...

But, when I deploy my code on the server (Centos7), then the query doesn't seem to be returning any value.
The queries are slightly different than the example I gave.

It is a SELECT statement, comparing an arabic text. i.e.

SELECT author_id, century FROM author_table WHERE author='انفسكم وقاتلوا'

Is there some sort of setup of something at DB level or OS level or Tomcat level that I need?

NOTE: If I take this exact same query, and run it within mysql, on the server (Centos7), then I get the result alright. But, not from my web application. I even tried with a standalone java program, writing this simple query, same behaviour.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.