• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

JDBC Doubt

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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("jdbc:oracle: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.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
resultset would never be null , even if it doesn't have any record in it.
For more information related to this check javadoc.
 
I am not young enough to know everything. - Oscar Wilde This tiny ad thinks it knows more than Oscar:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!