This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes JDBC and the fly likes JDBC Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC Doubt" Watch "JDBC Doubt" New topic
Author

JDBC Doubt

ChakraPani Margani
Greenhorn

Joined: Aug 19, 2007
Posts: 17
Hai members,
First of all thanking you to all of members to sending response to
my previous doubts.
I have some problem with the following code.

Though it is compiling and running, it gives false results.

First of all CODE is:
-----------------------

import java.sql.*;


public class sample
{
public void testing(String uin)
{

ResultSet rs=null;
Statement st=null;
Connection con=null;
PreparedStatement ps=null;

String UIN=uin;

int r=0;

try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("DriverLoaded");
con=DriverManager.getConnection("jdbcracle:thin:@localhost:1521:Oracle","scott","tiger");
System.out.println("Connection established");
st=con.createStatement();
rs=st.executeQuery("select * from uin where NUM='"+ UIN +"'");
if(rs!=null)
{
System.out.println("Record is already exist");
}

else
{
ps=con.prepareStatement("insert into uin values(?)");
ps.setString(1,UIN);
r=ps.executeUpdate();
}

if(r>0)
{
System.out.println("Number inserted Successfully:");
}

rs.close();
con.close();


}//try

catch(Exception e)
{
e.printStackTrace();
}//catch

}//testing()

public static void main(String args[])
{
sample s=new sample();
s.testing("s7048223a");
}//main()

}//class




The above code is working.It always displays "Record is already exist".

If i doesn't have any records in my database it always dispalys the above code.

But i doesn't know wheter it is coding problem or driver problem.

But remainig JDBC programs are working properly in my system.

please clarify my doubt.

Regards,
ChakraPani.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
resultset would never be null , even if it doesn't have any record in it.
For more information related to this check javadoc.


Rahul Bhattacharjee
LinkedIn - Blog
 
GeeCON Prague 2014
 
subject: JDBC Doubt