0

我将 MySQL 用于开发环境,但我的应用程序可能位于暂存环境的 SQLServer 上。

在我的数据库中,布尔值 ( boolean) 记录为VARCHAR,整数 ( int) 也记录为VARCHAR

像getAttributeAsBoolean(java.lang.String)这样的方法 record.getAttributeAsTYPE 是否合适/确定使用?还是使用类似的东西更好type myvar = new TYPE(record.getAttribute("COLUNM_NAME"))

在解决方案 1 和 2 之间使用什么更好?

示例 1:

boolean mybool = record.getAttributeAsBoolean("COLUNM_NAME"); // Solution 1

boolean mybool = new Boolean(record.getAttribute("COLUNM_NAME")); // Solution 2

示例 2:

int myint = record.getAttributeAsInt("COLUNM_NAME"); // Solution 1

int myint = new Interger(record.getAttribute("COLUNM_NAME")); // Solution 2

对我来说,VARCHAR可能更java.lang.String接近于java.lang.Booleanjava.lang.Integer。因此,我认为在这些示例中,“解决方案 2”更加确定。

4

1 回答 1

1

这取决于。如果您绝对确定属性的值是您需要的类型,您应该使用 getAttributeAsTYPE()。您的数据源(如果您使用 ds.xml)如何定义该字段?它可能会被 smartGWT 隐式转换。

看起来您的值存储为字符串(例如:record.setAttribute("COLUMN_NAME", "12345"),调用 record.getAttributeAsInt("COLUMN_NAME") 可能会因类型异常而失败。在这种情况下,您唯一能做的就是要做的是实例化 Integer 或静态转换它:Integer.parseInt(recrod.getAttribute())。

getAttributeAsTYPE 很方便,我尽可能多地使用它,但您必须确保在设置时该值是正确的类型。

于 2012-11-19T18:30:44.697 回答