Solved

JDBC & Prepared Statements

Posted on 2004-10-14
14
172 Views
Last Modified: 2010-03-31
Hi there,

I have a strange problem ..iam unable to figure it out any help will be highly appreciated..I have a database with around 80 tables Iam doing something like

main()
{ PreparedStatement pStmt = null;
try
{
Connection conn = DatabaseConnectionPoolMgr.getConnection();
deleteTablesInternal(conn);
deleteSomeTables(conn);
String sSql = "Delete from demo where demoid=10";
pSmt = conn.prepareStatement(sSql);
pStmt.execute();
pStmt.Close();
System.out.println("After deleting from demo");
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
finally
{
if(conn!= null)
  conn.close();
if(pStmt != null)
  pStmt.close();
}

}
In deleteTablesInternal() iam deleting some tables using preparedStatement & in deleteSomeTables() also, iam doing the same thing, finally iam deleting from the demo table which is the last table... My problem is the statement "After deleting from demo " is being displayed but the data is not deleted from demo table, all other tables are working fine...Iam using oracle9i as the database, iam in a dilemma & unable to figur it out whether its a database problem or jdbc problem. Any help wil be highly appreciated. thanxs in advance.

*Satish
0
Comment
Question by:satishAnanthuni
14 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 125 total points
ID: 12316472
Sure you have entries for which demoid=10?
0
 
LVL 92

Expert Comment

by:objects
ID: 12316477
check that rows exist with demoid=10
the return value from an update will tell you how many rows were affected.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12316480
You could try executeUpdate() instead of execute()
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12316483
>>the return value from an update will tell you how many rows were affected.
If you use executeUpdate()
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12321606
Hi satishAnanthuni,

There is no need to use a PreparedStatement in this case, not that it should matter.

Try this:
Statement pStmt = null;
try
{
Connection conn = DatabaseConnectionPoolMgr.getConnection();
deleteTablesInternal(conn);
deleteSomeTables(conn);
String sSql = "Delete from demo where demoid=10";
pSmt = conn.createStatement();
pStmt.executeUpdate(sSql);
pStmt.Close();
System.out.println("After deleting from demo");
}


Cheers!

\tt
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:satishAnanthuni
ID: 12336349
Hi All,

Its still confusing, there is one record in the demo table with demoid = 10 & the executeUpdate() returns me 1 but somehow, the record still exists in the database..


*Satish
0
 
LVL 92

Assisted Solution

by:objects
objects earned 125 total points
ID: 12336358
If its returning 1 then it is deleting it.
Are you sure you're not inserting it again later, or checking the wrong database/table?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12336554
>> executeUpdate() returns me 1 but somehow, the record still exists in the database
Rather impossible.
>> Are you sure you're not inserting it again later, or checking the wrong database/table?
Better double check that
0
 
LVL 92

Expert Comment

by:objects
ID: 12336581
zzynx,

really no need to repeat everything I said.
0
 

Expert Comment

by:rickychow
ID: 12345124
i found that u haven't commit, did u do the commit?

conn.setAutoCommit(false);
deleteTablesInternal(conn);
deleteSomeTables(conn);
String sSql = "Delete from demo where demoid=10";
pSmt = conn.prepareStatement(sSql);
pStmt.execute();
pStmt.Close();
System.out.println("After deleting from demo");

conn.commit();
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12544842
Proposal: split objects - zzynx
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now