Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

preparedStatement not working with Oracle

843859Sep 20 2006 — edited Sep 21 2006
Hi All,

I am using preparedStatement in my JDBC code to fetch/insert values from oracle9i database.

I am checking condition like if a given record does not exist then insert it else update it.

First time it works when there is no row in database, however for subsequent run it's not able to return me the result though that row exist in database and this resulting in DuplicateKeyException becuase it try to create the row in db again.

The code is working fine for MySQL DB2 and SQLServer but doesn't work in case oracle 9i

Here is mycode

//problem is here 1st time it works next time it is not retunring true though record is there in DB.
if(isItemExist("1","CORP"))
updateItem("1","CORP","DESC1");
else
insertItem("1","CORP","DESC1");

public boolean isItemExist(String itemid, String storeid)
{
String FIND_SQL = "SELECT item_desc from item where item_id = ? and store_id = ? ";
c = utils.getConnection();
ps = c.prepareStatement();
int i = 1;
ps.setString(i++, storeid);
ps.setString(i++, itemid);
rs = ps.executeQuery();
if(rs.next()){
return true;
utils.close(c, ps, rs);
}
else{
return false;
utils.close(c, ps, rs);
}
}

public void createItem(String itemid, String storeid, String item_desc)
{
String INSERT_SQL = "INSERT INTO item(item_id,store_id,item_desc)values(?, ?, ?)";
c = utils.getConnection();
ps = c.prepareStatement();
int i = 1;
ps.setString(i++, itemid);
ps.setString(i++, storeid);
ps.setString(i++, item_desc);
ps.executeUpdate();
utils.close(c, ps, null);
}
public void updateItem(String itemid, String storeid, String item_desc)
{
String INSERT_SQL = "UPDATE item SET item_desc = ?, store_id=? WHERE item_id = ?";
c = utils.getConnection();
ps = c.prepareStatement();
int i = 1;
ps.setString(i++, item_desc);
ps.setString(i++, storeid);

ps.setString(i++, itemid);
ps.executeUpdate();
utils.close(c, ps, null);

}

Kindly suggest what's wrong with code. because same code works with other databse like SQL Server, MySQL but it is not working with oracle9i.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 19 2006
Added on Sep 20 2006
3 comments
415 views