Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

SQL in Java, check if value in table column is NULL

Hi.
So let's take this table for example, we call it Messages
[Image: ab0253305a04481ed9d3806ccfaa51bc.png]

By default, receiver value is always NULL.
I am trying to display all Messages where receiver is NULL.
But how?

Since this wont work.
Code
System.out.println("1. Read messages 2. Send message 0. Quit");
            answer=reader.nextInt();
            reader.nextLine();
            if(answer==1)
            {
                try{
                    st = con.createStatement();
                    rs = st.executeQuery("SELECT Message FROM Messages WHERE Receiver = IS NULL");
                    while (rs.next()) {
                        System.out.println(rs.getString(1));
                    }
                }catch(SQLException ex){
                    System.out.println(ex.toString());

This is the error message
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IS NULL' at line 1
Now I know what you meant when you said you will lose something valuable.
You should type Receiver = NULL, not Receiver = IS NULL. That is the mistake.
Bazinga.
(Jun 8, 2015 03:01 PM)Kain_12 Wrote:  You should type Receiver = NULL, not Receiver = IS NULL. That is the mistake.
Tried it, does not display anything. That's the problem.

Just wondering, does it matter what type it is? It is int right now, but it should not matter since it's NULL by default right?

[Image: b0d566b8288ebf1a5714983ca99e8011.png]

E: Working now, it had to be RECEIVER IS NULL, just had to remove the =.
Now I know what you meant when you said you will lose something valuable.
(Jun 8, 2015 03:01 PM)Kain_12 Wrote:  You should type Receiver = NULL, not Receiver = IS NULL. That is the mistake.

No it's not.

(Jun 8, 2015 03:04 PM)Vanilja Wrote:  E: Working now, it had to be RECEIVER IS NULL, just had to remove the =.

There we go.

Alternatively, the notation <> usually works, too. That means "is different from", e.g. WHERE USER_ID <> 42.
[Image: TEGE4Zx.png]

Puzzle level: 2/5


Possibly Related Threads...
Thread / Author Replies Views Last Post
by lilsergsApr 26, 2023 10:15 AM 0 116 Apr 26, 2023 10:15 AM
Last Post: lilsergs
[.NET] by QU35TFeb 10, 2022 10:19 AM 2 126 Feb 10, 2022 10:19 AM
Last Post: QU35T
[.NET] by Haxor1974Oct 31, 2017 08:44 PM 2 159 Oct 31, 2017 08:44 PM
Last Post: ppaida