Solved

JDBC & Prepared Statements

Posted on 2004-10-14
14
176 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

680 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