My ResultSet Query is
StrQry = "select SUM(isnull(prn_amount,0))as prn_amount,SUM(isnull(adv_prn,0))as adv_prn, SUM(isnull(prv_prn,0))as prv_prn from loan_transaction_mcg where loan_id='1117'";
It is giving the result as on Sql
prn_amount =NULL
adv_prn =NULL
prv_prn =NULL
when the loan id =1117
ResultSet RsPrincipalDetail = getPaidDetail(loan_id);
while(RsPrincipalDetail.next()){
prn1 = RsPrincipalDetail.getString("prn_amount");
prn2 = RsPrincipalDetail.getString("adv_prn");
prn3 = RsPrincipalDetail.getString("prv_prn");
if(prn1.equals("")){
prn1.equals("0");
}
if(prn2.equalsIgnoreCase("")){
prn2.equals("0");
}
if(prn3.equalsIgnoreCase("")){
prn3.equals("0");
}
I tried putting prn1.equals(null) but still the null pointer exception comes. I tried in debug mode on prn1, it is showing as null as its value.
The problem here is that since your values in database are NULL when you convert them to java values using getString they will also be null.
Since null is not the same as empty string you can not really use prn.equals("")
Also using prn.equals(null) is a bad idea as usually the way that equals is implemented ... it will return false if something that it is compared to is null
Your best bet is to use equality operator to check for null
if(prn == null)
What are the column names of the table?
For me this seems to be possibly cyclic:
SUM(isnull(prn_amount,0))as prn_amount
and there seems to be a group by missing in your statement on loan_id
Related
When we try to fetch data with Null values
field(TABLE_NAME.COLUMN_NAME.in(null))
with IN clause
getting null pointer exception.
Maybe because of this.
#Override
public final Condition in(Collection<?> values) {
Field<?>[] fields = new Field[values.size()];
Iterator<?> it = values.iterator();
for (int i = 0; it.hasNext(); i++)
fields[i] = Tools.field(it.next(), this);
return in(fields);
}
In the database, we can provide null in IN clause.
There is an existing "won't fix" issue in jooq https://github.com/jOOQ/jOOQ/issues/3867
There are some alternatives:
check null before IN(Cant do in my case its a really big select statement)
So if I want to make this possible is there any other workaround.
PS: On a similar note "eq" works perfectly fine:
#Override
public final Condition equal(Field<T> field) {
return compare(EQUALS, nullSafe(field, getDataType()));
}
Edit: 'field(TABLE_NAME.COLUMN_NAME.in(null))' here null is a collection.
Your example code doesn't compile:
TABLE_NAME.COLUMN_NAME.in(null)
There are 5 overloads of this in() method in jOOQ 3.14, and as such, you cannot pass the null literal to the in() method. Your real client code may be using a local variable like this:
Collection<?> collection = null;
TABLE_NAME.COLUMN_NAME.in(collection)
There might be a case for when this should behave the same as passing an empty collection, such as Collections.emptyList(), but this isn't what you seem to want. You probably want to pass actual null values inside of that collection, which you can do:
TABLE_NAME.COLUMN_NAME.in(1, null, 2)
But why would you do it? SQL implements three valued logic, meaning that NULL values have no effect in IN predicates, while they have an unintuitive, hardly desired effect in NOT IN predicates (the entire predicate becomes NULL)
I've got a MVC based Java application with three models: Room, Student and StudentRoom.
StudentRoom contains an object of Room and Student.
Now I've got the problem that if my SQL query returns no result and I check the value of student's name like this
if(studentRoom.student.name != null) {
}
I'll get a NullPointerException and I don't know how to handle it.
Should I set Student.name = ""; since my query has no result?
if(studentRoom != null && studentRoom.student != null && studentRoom.student.name != null){
//.. Access student
}
Above solution looks a bit weird. you should better use getter/setter methods instead of directly accessing the objects.
Apart from that you can define methods like isStudentAvailable() in studentRoom to check whether it has Student in it or not.
Should I set Student.name = ""; since my query has no result ?
It completely depends on your use case. But I must say better to keep it null as it will raise the exception instead of passing the null check validations.
You might need a try/catch statement for that. Something like this :
try {
// do process here
} catch (NullPointerException npe) {
//to do if student is null
}
But take note, if there are any object that is inside the try statement, a NullPointerException would still be thrown. Hope this helps.
i tryed this:
ResultSet existetabela = stm.executeQuery ("SELECT * FROM pessoajuridica WHERE protocolo =" + varConsult );
System.out.println(existetabela);
but it only return a strange String -> org.sqlite.RS#1f959518
i was expecting the value..
remembering, sql lite and java :S
i want to use the value that it return to compare, if it return any value, means that it exist, so it will not add to the sql, if dont return anything = can add!!!
("if exist" doesnt work for me, says that its a invalid argument in the sql command line --')
You can use ResultSet#next() method to test whether there was any result set returned:
if (existetabela.next()) {
// Result was fetched
// Assuming type of protocol is String (Can be anything)
String protocol = existetabela.getString("protocolo");
} else {
// No result
}
Now, let's move ahead to the major issue. You should use PreparedStatement, to save yourself from SQL Injection.
You need to iterate inside the result set to retrive the actual data that were found:
while (existetabela.next()){
System.out.println(existetabela.getObject("protocolo"));
}
Did you look at PreparedStatement ?
I want to know if HQL expressions are null-safe or not?
For example, consider this named query
SELECT a
FROM A a
where a.f=:f
in which f is a field of type String, Double, Date, etc.
Then I use it like:
session.getNamedQuery("myNamedQuery").setString("f", myFValue).uniqueResult();
If both a.f and myFValue are null I want the condition to be true and if just one of them is null I want it to be false.
If it is not null-safe, how can I handle that?
Regards
No, they aren't null-safe. They're translated directly to SQL, and obey the same rules. So if yo want to test for null, you must use is [not] null.
So if the f parameter can be null, you'll have to use two different HQL queries, or build it dynamically, or use a Criteria query to build a dynamic query.
They are not. Try these scenarios out to handle your specific case:
select a from A a where ((:f is null and a.f is null) or a.f = :f) and ...
If your parameter String is null then the query will check if the row's status is null as well. Otherwise it will resort to compare with the equals sign.
If you needed to skip the :status where_clause altogether; you can code like so:
select a from A a where (:f is null or a.f = :f) and ...
This second query is equivalent to:
if(status != null){
sql.append(" a.f = :f and ");
}
You should use is null or is not null.
See HQL "is null" And "!= null" on an Oracle column
They are not null safe.
HQL translates your HQL query to SQL and then it substitute your parameters.SO it won't rewrite query from param = ? to param is null.
Go for Criteria API and use Restrictions.isNull("f");
They are not null safe. Using criteria this would do the trick
public static void addNullSafeEqualsRestriction(Criteria criteria, String propertyName, Object object) {
if (object == null) {
criteria.add(Restrictions.isNull(propertyName));
} else {
criteria.add(Restrictions.eq(propertyName,object));
}
}
Edit: Subsequent checking of addrline2 (addrline2 == null) reveals that addrline2 is in fact null, printing out the null object resulting in the string "null" going to stdout.
I'm using the Oracle 11g thin client with an Oracle 11g database. When querying a column that contains null, getString("colname") is return a string "null" instead of a Java null value. Is that the expected behavior? Javadoc indicates I should be receiving a Java null.
ResultSet rs = descStatement.executeQuery();
rs.next();
String addrline2 = rs.getString("addressLine2");
System.out.println("addrLine2->"+addrline2+"<-");
boolean wasNull = rs.wasNull();
System.out.println("Wasnull: "+wasNull);
output:
addrLine2->null<-
Wasnull: true
Note that a null object, when converting to a string, prints out as "null". You're not actually checking for null-ness anywhere.
Try adding
if (addrline2 == null) {
System.out.println("It's null!")
}
rs.getString("addressLine2") is returning null only but when yo do
System.out.println("addrLine2->"+addrline2+"<-");
its printing null as string literal
do it like this
System.out.println("addrLine2->"+addrline2!=null ? addrline2 : "Its null"+"<-");