1

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.

4

3 回答 3

1

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.

于 2015-02-14T18:12:08.777 回答
0

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");
于 2013-03-04T17:38:20.877 回答
0

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.

于 2013-03-04T18:26:10.793 回答