query about resultset

how can i check that resultset object is null or not
[59 byte] By [rachna_arora82a] at [2007-11-26 13:34:16]
# 1
hey Rachna,please use the next().......... infact many ways to do that.Thanks n RegardsNaveen MMessage was edited by: Novice_inJAVA
Novice_inJAVAa at 2007-7-7 22:15:39 > top of Java-index,Java Essentials,New To Java...
# 2
hi by ueing next how can i know resultset is null or not can i m directly using NULL but its not workingrachna
rachna_arora82a at 2007-7-7 22:15:39 > top of Java-index,Java Essentials,New To Java...
# 3
hai u can use null to check that a result set object is null or notlike this ResultSet rs;if(rs==null)try thisbye
balaji.aspla at 2007-7-7 22:15:39 > top of Java-index,Java Essentials,New To Java...
# 4
already tried its not working
rachna_arora82a at 2007-7-7 22:15:39 > top of Java-index,Java Essentials,New To Java...
# 5
did not understand the question... Do you want to check if the resultset contains object or resultset is valid not-null object?
AjaySingh516a at 2007-7-7 22:15:39 > top of Java-index,Java Essentials,New To Java...
# 6
i ant to know that resultset have records or not
rachna_arora82a at 2007-7-7 22:15:39 > top of Java-index,Java Essentials,New To Java...
# 7

you can use:

if(result.next()) {

// Has record.

}

else {

// Doesn't have record.

}

AjaySingh516a at 2007-7-7 22:15:39 > top of Java-index,Java Essentials,New To Java...
# 8

try

{

String ans1=(request.getParameter("R1"));

//String name=(request.getParameter("T2"));

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:exam","","");

PreparedStatement s;

ResultSet rs;

String value = (session.getValue("s1")).toString();

String name=(request.getParameter("T1"));

s=con.prepareStatement("select emailid from check where emailid=?");

s.setString(1,value);

rs=s.executeQuery();

if (rs.next())

{

out.println("already reg");

}

else

{

PreparedStatement st=con.prepareStatement("update check set ans1=? where name Like ?");

st.setString(1,ans1);

st.setString(2,value);

st.executeUpdate();

out.println(value);

}

}catch(Exception e)

{

out.println(e);

}

now my code is this and its showing null pointer exception

rachna_arora82a at 2007-7-7 22:15:39 > top of Java-index,Java Essentials,New To Java...
# 9

Which line causing null pointer exception.

you should check/validate for the input parameter(s) null or not.

like

if(!(value == null || value.trim() == "" )){

// do your sql codes here

}else{

// print exception

}

cybervink2000a at 2007-7-7 22:15:39 > top of Java-index,Java Essentials,New To Java...
# 10
Us e.printStackTrace() so you see the complete message.When you post code, please use[code] and [/code] tags as described in [url= http://forum.java.sun.com/help.jspa?sec=formatting ]Formatting tips[/url] on the message entry page. It makes it much easier to
mlka at 2007-7-7 22:15:39 > top of Java-index,Java Essentials,New To Java...
# 11
where should i write thise.printStackTrace()
rachna_arora82a at 2007-7-7 22:15:39 > top of Java-index,Java Essentials,New To Java...
# 12
This is the same as this thread: http://forum.java.sun.com/thread.jspa?threadID=5119246Urk, database access mixed in with JSPs. Nasty stuff. Throw it away, split the database access out.
mlka at 2007-7-7 22:15:39 > top of Java-index,Java Essentials,New To Java...
# 13

Have added my comments inline, potential places where

NullPointerException can occur. run it and post the result.

try

{

String ans1=(request.getParameter("R1"));

//String name=(request.getParameter("T2"));

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:exam","","");

PreparedStatement s;

ResultSet rs;

String value = (session.getValue("s1")).toString(); //AJAY - column

value s1 may be null

String name=(request.getParameter("T1"));

s=con.prepareStatement("select emailid from check where emailid=?");

s.setString(1,value);

rs=s.executeQuery();

if (rs.next())

{

out.println("already reg");

}

else

{

PreparedStatement st=con.prepareStatement("update check set ans1=? where

name Like ?");

st.setString(1,ans1);

st.setString(2,value);

st.executeUpdate();

out.println(value);

}

}catch(Exception e)

{

e.printStackTrace(); // AJAY - will print out the source of error.

}

AjaySingh516a at 2007-7-7 22:15:39 > top of Java-index,Java Essentials,New To Java...
# 14
now its not showing errornot even nullpointerexception and alsi not saving data in databasenot showing already reg
rachna_arora82a at 2007-7-7 22:15:39 > top of Java-index,Java Essentials,New To Java...
# 15
mlk,hope rachna is new to java thats why rachna mixes the jdbc code with jsp. it may takes sometime to comeout.
cybervink2000a at 2007-7-21 15:52:40 > top of Java-index,Java Essentials,New To Java...
# 16
sourround the code that you suspect using try{ } and in the corresponding catch block call e.printStackTrace()
AnjanReddya at 2007-7-21 15:52:40 > top of Java-index,Java Essentials,New To Java...
# 17
ya true i m vb 6 developer and this is my first project in java
rachna_arora82a at 2007-7-21 15:52:40 > top of Java-index,Java Essentials,New To Java...
# 18
call con.commit() after st.executeUpdate();
AjaySingh516a at 2007-7-21 15:52:40 > top of Java-index,Java Essentials,New To Java...
# 19

If you're using Access as your database, your UPDATE won't show up until you close your resources properly. That means ResultSet, Statement, and Connection each closed in individual try/catch blocks in a finally block.

Externalize your connection parameters (e.g., driver, URL, etc.) Those can change, and you don't want to have to recompile your code to do it.

%

duffymoa at 2007-7-21 15:52:40 > top of Java-index,Java Essentials,New To Java...
# 20
OMG, I hope this isn't in a JSP.If it is, move it into a Java class that you can test off line. JDBC code does not belong in JSPs.%
duffymoa at 2007-7-21 15:52:40 > top of Java-index,Java Essentials,New To Java...
# 21
> mlk,> > hope rachna is new to java thats why rachna mixes the> jdbc code with jsp. it may takes sometime to comeout.Why get into that in the first place?
aniseeda at 2007-7-21 15:52:40 > top of Java-index,Java Essentials,New To Java...
# 22
oki will seggregate the jdbc and jspbut how can i call a class file in jsp with import statementor wat
rachna_arora82a at 2007-7-21 15:52:40 > top of Java-index,Java Essentials,New To Java...
# 23

> but how can i call a class file in jsp with import

> statement

It works the same way as in any class. You import the class, create an object and call methods on it. If you could add JDBC code directly in the JSP, I am pretty sure you could write the code with your custom class in the same manner (except that you achieve some simplicity by getting the JDBC code out from where it should not be).

aniseeda at 2007-7-21 15:52:40 > top of Java-index,Java Essentials,New To Java...
# 24

> Which line causing null pointer exception.

>

> you should check/validate for the input parameter(s)

> null or not.

>

> like

>

> if(!(value == null || value.trim() == "" )){

> // do your sql codes here

> else{

> // print exception

>

Don't compare String values using ==.

(value.trim() == "")

should be:

(value.trim().length() == 0)

or

(value.trim().equals(""))

doremifasollatidoa at 2007-7-21 15:52:40 > top of Java-index,Java Essentials,New To Java...
# 25
thanx allmy prob is solved now
rachna_arora82a at 2007-7-21 15:52:40 > top of Java-index,Java Essentials,New To Java...