0

I am having an issue getting values that are NULL in MySQL to actually return as NULL in a column that is of type Double. When running a simple select in clients such as HeidiSQL or DBeaver, the returned results are shown as (NULL) which is correct. However, when running the same statement in Java or via a custom SQL in Wordpress wpDataTables plugin, any NULL values are returning as 0.0.

My table looks like this:

CREATE TABLE `Test` (
    `ID` INT(11) NOT NULL,
    `Time` DECIMAL(20,1) NOT NULL DEFAULT '0.0',
    `Test1` DOUBLE NULL DEFAULT NULL
)
COLLATE='utf8mb4_unicode_ci'
ENGINE=InnoDB;
INSERT INTO Test (ID, Time, Test1) VALUES (1, 0.1, NULL)
select * FROM Test

Running that select, SQL Clients will correctly display NULL, but Java and Wordpress display NULL as 0.0

Is there something i am doing wrong on the table create or in the Select Statement that would cause this issue?

Java Code

String testSQL = "Select * from Test";
        ResultSet rs = null;
        Statement stmt = createDbStatement(conn);
        try {
            rs = stmt.executeQuery(testSQL);
            while (rs.next()) {
                System.out.println(rs.getDouble("Test1"));
            }
        } catch (Exception ex) {
            System.out.println(ex);
        }
public static Statement createDbStatement(Connection conn) {

        Statement stmt = null;
        try {
            stmt = conn.createStatement();
        } catch (SQLException ex) {
            // handle any errors
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        }
        return stmt;
    }
public static void createDbConnection() {

        //Connection conn = null;
        try {
            main.conn = DriverManager.getConnection("jdbc:mysql://xx.xx.xx.xx/DBName?user=xxxx&password=xxxx");
        } catch (SQLException ex) {
            // handle any errors
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        }

    }

Output:

0.0

  • 1
    Please provide a short java snippet which proves, that the value is 0.0 instead of NULL. – Georg Richter Aug 28 '23 at 10:09
  • Probably wordrpress and the java app do some conversion in the background. Wordpress intends to interface with users, so I'm not surprised it converts nulls to 0 as an average person without database background would not understand what nulls mean. – Shadow Aug 28 '23 at 10:19
  • @Shadow possibly, however i am not sure what i can change in my Java code to fix this, its not complex. For wordpress, i agree however the data is used for creating line graphs, so 0.0 has a very different meaning to NULL. – user3689706 Aug 28 '23 at 10:25
  • @GeorgRichter Updted question with Java code – user3689706 Aug 28 '23 at 10:25
  • 3
    `getDouble()` returns a primitive double which can not be null. If you intend to use nulls in those kinds of columns, you'll need to handle nulls yourself by calling `rs.wasNull();` after `getDouble()`. – Kayaman Aug 28 '23 at 10:27
  • @Kayaman Interesting thanks. I just tried rs.getObject("Test1") in Java and that seems to return NULL as expected so thats an easy fix. However my Java app is used only for staging the data and some admin stuff, the real issues lies in wordpress which i have limited to no control over (as its a plugin) beyond telling it the Select statement. is there anythig i could do DB side that would "fix" it? – user3689706 Aug 28 '23 at 10:38
  • 1
    Well if you control the select statement, you might be able to convert it to a string on the fly so you never get a `NULL` from the DB. Or you could tell the plugin developer to fix the plugin. – Kayaman Aug 28 '23 at 10:47
  • It seems that your wordpress plugin is not fit for purpose. – Shadow Aug 28 '23 at 10:56
  • 1
    https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getDouble(java.lang.String): "Returns: the column value; if the value is SQL NULL, the value returned is 0" – Georg Richter Aug 28 '23 at 11:36

0 Answers0