Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

JDBC & Prepared Statements

Posted on 2004-10-14
14
175 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

856 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