我在开发环境中使用MySQL,但我的应用程序可以在暂存环境中使用SQLServer。
在我的数据库中,布尔值(boolean
)记录为 VARCHAR ,整数(int
)也记录为 VARCHAR 。
方法record.getAttributeAsTYPE是否适合getAttributeAsBoolean(java.lang.String)适当/肯定使用?或者使用像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.Boolean
更接近java.lang.Integer
。所以我认为在这些例子中,“解决方案2”更加确定。
答案 0 :(得分:1)
这取决于。如果您完全确定属性的值是您需要的类型,则应使用getAttributeAsTYPE()。您的数据源(如果您使用的是ds.xml)如何定义该字段?它可能会被smartGWT隐式转换。
看起来您的值存储为String(例如:record.setAttribute(“COLUMN_NAME”,“12345”),调用record.getAttributeAsInt(“COLUMN_NAME”)可能会因类型异常而失败。在这种情况下,只有您可以做的事情是实例化Integer或静态转换它:Integer.parseInt(recrod.getAttribute())。
getAttributeAsTYPE很方便,我尽可能多地使用它,但你必须确保在设置时该值是正确的类型。