0

例如,我的陈述如下:

dbConnection.update("insert into transaction (isrecovered) values(?)", new Object[]{"N"});

在上述陈述中,为什么我们不能将“N”作为“N”?

4

3 回答 3

2

In Java, a string is surrounded with double quotes.

A Java char is surrounded in single quotes.

A String is an Object (it is a sub-class) and the array must contain objects. A char is a primitive and not an Object.

Do not confuse the above, which concerns Java syntax only, with any of the columns in your data table. You may have columns that are CHAR or VARCHAR type. That's the database's way of specifying what's stored there.

Further, do not confuse the above with the content of the Java Strings containing SQL statements. SQL is a different language than Java and uses different conventions. Different DataBase engines use either single or double quotes to surround a string constant in SQL. Plus it depends on the options selected. Typical common sense advice says to use single quotes unless your particular database and its settings mean you don't.

于 2013-04-26T16:47:59.423 回答
1

此语句中混合了2 种语言( SQL 和 Java),每种语言都有不同的语法。

有些语言接受带单引号的字符串(如 PHP 和 SQL),有些则不接受(如 Java)。

我建议您使用更安全的解决方案,例如PreparedStatement可以调用的地方.setChar(...)

于 2013-04-26T16:54:26.737 回答
0

您的更新方法接受一个字符串参数,并且字符串在 Java 中用双引号括起来,而不是单引号。对于新的 Object[]{"N"} 事物,这里的“N”也是一个字符串对象,您仍然可以将“N”传递给对象数组,因为它们将自动装箱到字符对象。

于 2013-04-26T16:55:15.660 回答