ClydeFrog
ClydeFrog

Reputation: 1012

Java, JDBC executeQuery with no result but gets result when running statement myself

I have a table named "statuses" in my MySQL database which looks like this:

| "id" (int) | "text" varchar(50) | "hex_color" varchar(10) |
-------------------------------------------------------------
| 1          | EJ PÅBÖRJAD        | #FF3300                 |
| 2          | ARBETE PÅGÅR       | #FFFF00                 |
| 3          | AVVAKTAR           | #80F0FF                 |
| 4          | ÅTGÄRDAD           | #6DE37A                 |
-------------------------------------------------------------

Here's my JDBC Java-code:

Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(statement);

while(rs.next()){
   Long pid = rs.getLong(1);
   String ptext = rs.getString(2);
   String hex_color = rs.getString(3);

   status = new Statuses(pid, ptext, hex_color);
}

When statement is:

SELECT id, text, hex_color FROM statuses WHERE text = 'EJ PÅBÖRJAD' - 1 result

SELECT id, text, hex_color FROM statuses WHERE text = 'ARBETE PÅGÅR' - 0 result

SELECT id, text, hex_color FROM statuses WHERE text = 'AVVAKTAR' - 1 result

SELECT id, text, hex_color FROM statuses WHERE text = 'ÅTGÄRDAD' - 1 result

why do i get 0 result on the second statement? when i'm running it myself in "mysql command line" i get 1 result! i've checked that i'm using the same charset (UTF-8) which rules out charset problem.

can someone please help me on this matter?

kind regards, Clyde

Upvotes: 0

Views: 744

Answers (2)

Ascendant
Ascendant

Reputation: 827

I know it's a 7 months old post but I thinks you could check if you have committed after you inserted those rows.
When you run the query yourself, you are probably using the same session that you used to insert those rows. That's why you can see them.
Until you commit, other users won't be able to see the effect(of the inserts).

Upvotes: 0

Anders R. Bystrup
Anders R. Bystrup

Reputation: 16060

Are you absoluty sure that

  • you haven't made a copy-paste error for 'ARBETE PÅGÅR' or
  • the database contains an extra space or other unprintable character in/after 'ARBETE PÅGÅR'

Try surrounding with wildcards, eg. SELECT * FROM statuses WHERE text LIKE '%ARBETE%PÅGÅR%' and see if that makes a difference?

Cheers,

Upvotes: 1

Related Questions