Solved

JDBC & Prepared Statements

Posted on 2004-10-14
14
174 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 80
mockito example issue 8 72
Apps blocked by Java 9 79
map related example 6 47
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)
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

785 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