Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC Doubt

 
ChakraPani Margani
Greenhorn
Posts: 19
  • 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("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
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic