NEWBIE: How to return SQL boolean?

Dear Experts,

I want to use SQL just to determine the existence of a table.  Normally, I return a ResultSet object.  But here, it seems I should be returning a simple boolean.  How do I get the response in this case?  The syntax I want to use is SOMETHING liker this:

    boolean bool = false;
    String s = "if (exists(select * from sysobjects where name = MYTABLE))";
    try
    {
      Statement st = conn.createStatement();
      bool = st.executeQuery(s);
    }
    catch (SQLException e) { }

Thanks!
--BrianMc1958
BrianMc1958Asked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
String s = "select * from sysobjects where name = MYTABLE)";

boolean exists = false;

...

if(resultSet.first()) {
    exists = true;
}
0
 
CEHJCommented:
No, just get a boolean from the ResultSet as normal
0
 
CEHJCommented:
(IOW you don't even need to get anything from the ResultSet - just see if it has results)
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
CEHJCommented:
>> String s = "select * from sysobjects where name = MYTABLE)";

should be

 String s = "select * from sysobjects where name = MYTABLE";

of course. Better not * if possible - just one column
0
 
BrianMc1958Author Commented:
I've tried it.  I'm getting good results. Thanks!

--BrianMc1958
0
 
CEHJCommented:
No problem
0
 
limaidealCommented:
i think the SQL should be:

select name from sysobjects where name='MYTABLE'.

sometime you want to avoid select * because not all columns can be retrieved by normal user.
0
 
CEHJCommented:
>> i think the SQL should be ...

Only if it's a char type. There's no indication of that in the original
0
 
limaidealCommented:
i think name in sysobjects is char type :)
0
 
CEHJCommented:
There's also no indication of what 'sysobjects' is ;-)
0
 
limaidealCommented:
sysobjects normally is a system table in Sybase, which holds information of, well, system objects :) , including tables
0
 
BrianMc1958Author Commented:
Yikes! I forgot to award points!  Thanks again, everybody!

--BrianMc1958
0
 
CEHJCommented:
:-)
0
 
limaidealCommented:
To be generic, you can use:

select 1 [from xxx] where <boolean expression>

to return boolean expression in SQL.
0
All Courses

From novice to tech pro — start learning today.