简体   繁体   中英

Update and delete in oracle using java

I have a table named bcfsite with a field named activestate.

update bcfsite set activestate = 1 where bscname like '%B361Z%'

When I run the above query SQLPlus or Oracle SQL Developer, it works fine without any problems, but when I run it in Java using this code:

String sqlstrString = "update bcfsite 
                       set activestate = 0 where bscname like '%B361Z%' ";
PreparedStatement ps = d.prepareStatement(sqlstrString);
ResultSet rs = ps.executeUpdate();

or this code :

ResultSet rs = DBConnection.RunStatement(sqlstrString, d);

My program pends and doesn't pass the update statement.

I changed the table and column and tried delete instead of update but still I see the same problem.

I am putting this is the answer box because there is not enough room in the comments...

String qString = "update bcfsite set activestate = 0 where bscname like ?";

That is the correct way to do this with a replaceable parameter. Remember when using the like operator everything passed to it is a varchar2 as far as oracle is concerned so:

ps.setString(1,"%B361Z%");

Again, this is the correct way to accomplish this.

The resulting string passed to oracle via jdbc will look like:

update bcfsite set activestate = 0 where bscname like '%B361Z%'

setString() applies the correct markup so the oracle query parser will see a correct query.

Queries with a "?" in them are not simply string replaced. The statement is parsed by the server, and then the parameter(s) are passed to the server.

BTW this was answered in several places on SO. Please learn how to search SO and it will shorten your response time considerably and not waste our time.

Try this approach:

String sqlstrString = "update bcfsite " +
                       "set activestate = 0 where bscname like '?'";
PreparedStatement ps = d.prepareStatement(sqlstrString);
ps.setString(1, "%"+"B361Z"+"%");
ResultSet rs = ps.executeUpdate();

And a second approach would be:

 ...set activestate=0 where bscname like CONCAT('%', ?, '%')"; 
 ...
 ps.setString(1, "B361Z");

Can you try following approach first to clear out it updates atleast one row through Java code, don't give wild characters try to update single row.

String sqlstrString = "update bcfsite 
                       set activestate = 0 where bscname=?";
PreparedStatement ps = d.prepareStatement(sqlstrString);
ps.setString(1, "B361Z");
ResultSet rs = ps.executeUpdate(); 

This may rule out other issues like connectivity/different schema ..

Please post the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM