Hello Friends!

Want to Post something i know might be very weird but this is how it goes.

In Java we use

RS.getString("FirstName");

to get the value of the column FirstName from the database.

If the valueis not detected then it shows null in the Text Box.

RS.getInt("StudentID");

to get the value of the Column StudentID in integer from the Database.

If the value is not detected then it displays 0.

I want to ask is there a way such that if there is no value then nothing is displayed on the Text Box instead of showing null for string columns and 0 which is showed for integer columns.

All Help Appreciated.

Dani AI

Generated

Short summary: the database NULL becomes the Java null (and a primitive read like an int becomes 0), so when you write that value into an HTML input it prints the literal null or 0. Fix this at the display layer by converting database nulls to an empty string (or by using a nullable boxed type), or at the query layer with SQL functions—but be careful: SQL-side defaults hide the fact a value was NULL.

A safe ResultSet-side pattern is to read boxed objects and normalize them before printing. Example (uses getObject so SQL NULL maps to null instead of a primitive default):

String firstName = (String) rs.getObject("FirstName");
out.print(firstName == null || firstName.trim().length() == 0 ? "" : firstName);

Integer studentId = (Integer) rs.getObject("StudentID");
out.print(studentId == null ? "" : studentId.toString());

If you prefer JSP/JSTL/EL (cleaner in the view), let the tag/EL handle defaults:

<c:out value="${bean.firstName}" default=""/>

<input type="text" name="studentId"
       value="${empty bean.studentId ? '' : bean.studentId}" />

Notes and troubleshooting: was on the right track with an emptiness check—just avoid calling isEmpty() on a possibly null string (it throws NPE). If you must use a primitive getter that returns 0, call rs.wasNull() immediately afterward to tell whether the DB value was actually NULL. Using SQL COALESCE(column, '') is another option but will mask NULLs at the source; use it only if that behavior is acceptable.

Recommended Answers

All 2 Replies

String temp = RS.getString("FirstName");

if(temp.isEmpty()){
   out.println("....");
}else{
   out.println("....");
}

I'm a bit rusty, but give it a whack

hey tyson thanks im gonna be trying the code soon enuf!


Thanks For The Reply :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.