Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8378609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:58:02+00:00 2026-06-09T15:58:02+00:00

MySQL uses IS NULL instead of NULL so how do i bind variable to

  • 0

MySQL uses “IS NULL” instead of “NULL” so how do i bind variable to achieve the same ?

Right now i tried with this

selectStmt.setNull( 2, java.sql.Types.VARCHAR );

when i printed “java.sql.Types.VARCHAR” shows value = 12 ? If so how does this set ” IS NULL” in preparedstatment ?

MORE DETAILS:

i.e I am interested in querying (MySQL) for null if i have string = ” ” in input. So logic goes like this,

Connection c = SQLCon.connect();
PreparedStatement selectStmt = c.prepareStatement( "select * from "
                                    + TableName
                                    + " where P = ? AND M = ? AND PR = ? AND DR = ? " );


if ( P.trim().equals( "" ) ) 
{
   selectStmt.setNull( 1, java.sql.Types.VARCHAR );
} else 
{
   selectStmt.setString( 1, P );
}

if ( M.trim().equals( "" ) ) 
{
   selectStmt.setNull( 2, java.sql.Types.VARCHAR );
} else 
{
   selectStmt.setString( 2, M );
}

Please advice

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:58:03+00:00Added an answer on June 9, 2026 at 3:58 pm

    If you want to match a column in a where clause that might be null with a parameter that might be null, you actually need to bind it twice, because in SQL a null doesn’t equal anything else, even another null.

    PreparedStatement stmt = conn.prepareStatement("select * from foo where (? is null and bar is null) or (bar = ?)");
    col = 1;
    setVarchar(stmt, col++, var);
    setVarchar(stmt, col++, var);
    
    void setVarchar(PreparedStatement stmt, int col, String var)
    {
      if (var == null)
        stmt.setNull(col, java.sql.Types.VARCHAR);
      else
        stmt.setString(col, var);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I have a C++ client application that uses mysql.h to connect to
This page uses $_GET to grab an asset id and query a mysql database
This script uses php and mysql to compute a one minute rolling average to
MySQL uses the USE database_name to change the active database. Does that work on
My application uses MySQL, on my server, as its database. My server is highly
I am using and working on software which uses MySQL as a backend engine
I have written a small chat app that uses mysql + php to facilitate
I have a vb.net program that uses mysql as its database. And it works
I am debugging an web application that uses MySQL. I am seeing lot of
I've made an app in C++ that uses mysql to connect to a server,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.