Java PreparedStatement using two single quotes for empty string parameter
I am using a PreparedStatement
with sql such as:
String sql = "insert into foo (a,b,c) values (?,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(psIndex++, a);
ps.setString(psIndex++, b);
ps.setString(psIndex++, c);
But if any of the variables is an empty string the resulting statement gets two single quotes. As in: VALUES ('foo','','')
Then I get an exception since two single quotes is an escape sequence.
I can't believe I couldn't find anything on this through searching, but I could not. What is going on here?
As the OP doesn't do what @Adam suggested in the comments, I'll do it. It's useful for future readers. Thanks to @user119179 for the idea.
It could be a bug in the JDBC driver we are using. The provider of the driver should know that ''
is an escape sequence.
Actually, updating the driver seems to solve the bug for the OP.
As in: VALUES ('foo','','') Then I get an exception since two single quotes is an escape sequence.
There is a misunderstanding here. The two single quotes is the empty string. There is no escape sequence happening. It is an escaped quote only if it is in another single quote. If you are getting an exception, it is probably elsewhere, such as a constraint on the column in the database.
The statement
insert into foo (a,b,c) values ('foo','','')
is very valid SQL.
@cyberkiwi is correct. May be your columns does not accept null values. What Exception are you getting ? If you have got any log reports then share it.
链接地址: http://www.djcxy.com/p/4776.html上一篇: 简单的病毒卸妆