This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes Doubt in JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Doubt in JDBC" Watch "Doubt in JDBC" New topic

Doubt in JDBC

ChakraPani Margani

Joined: Aug 19, 2007
Posts: 17
Hai to all members,
First of all thanks to krishnamoorthy for sending answer to my problem.

Today i got another doubt.

My aim is to insert a string in the data base.If that number is already
in the table then i have to display a message like number is already in the table.Otherwise number is to be inserted in the table.

I have developed code on my own.while running the code i got an exception.

I am sending the code with error message.please help me to solve this problem.

import java.sql.*;
class test
public void test1(String UIN)

ResultSet rs=null;
Statement st=null;
Connection con=null;
PreparedStatement ps=null;
String name="";
boolean flag1=false;
con=DriverManager.getConnection("jdbc racle:thin:@localhost:1521 racle","scott","tiger");
System.out.println("Connection established");
rs=st.executeQuery("select *from uin");


ps=con.prepareStatement("insert into uin values(?)");
int i=ps.executeUpdate();
System.out.println("Number is Successfully Inserted");
System.out.println("Number is already inserted");


catch(Exception e)

public static void main(String args[])
test t=new test();
String st="s7048223a";

at test.test1(
at test.main(
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14998

Line 27 is this line, right?


If you get a NullPointerException there, then name must be null. You can't call a method on a null reference. Since you are getting the name from the database, you are probably getting a NULL value from the database.

Check if name is null before calling a method on it:

if(name != null && name.equalsIgnoreCase(UIN))
[ August 24, 2007: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You might "select * from uin where number = newnumber" to see if a row already exists. Once that table has a hundred million rows, that will be a bit faster.

What if another thread inserts newnumber in the time between your select and your insert? If that's a primary key you'll get a duplicate key exception of some kind. Maybe that would solve the whole problem ... just catch that exception. Or if you don't like using exceptions to control flow, you might have to put the select and the update in a transaction. I"m not sure how to form that thing ... maybe with select for update?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Bert Bates

Joined: Oct 14, 2002
Posts: 8898
sliding this over to the jdbc forum

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
krishnamoorthy kitcha
Ranch Hand

Joined: Feb 04, 2006
Posts: 96
Hi ChakraPani Margani

Before inserting in the table first you should check the database

Suppose you will be inserting string like 'test' in the employee and field name like name

select * from employee where name='test'

boolean flag;
int i=0;

while ({
if the resultset moved , there is record already there
flag =false;


insert into table your record

return flag;

IN the client side , you can check with the flag

If false returns , record is already there or some exception

If the true returns , there is no such record, inserted successfully

Check and tell me your feedback.
I agree. Here's the link:
subject: Doubt in JDBC
It's not a secret anymore!