我在开发环境中使用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",在这些例子中,是更确定的。
发布于 2012-11-19 18:30:44
那得看情况。如果您完全确定属性的值是您所需的类型,则应该使用getAttributeAsTYPE()。您的数据源(如果使用ds.xml)是如何定义该字段的?它可能只是被smartGWT隐式转换。
看起来您的值存储为String (例如: record.setAttribute("COLUMN_NAME","12345"),调用record.getAttributeAsInt("COLUMN_NAME")可能会导致类型异常失败。在这种情况下,您唯一能做的就是实例化整数或静态地转换它: Integer.parseInt(recrod.getAttribute())。
getAttributeAsTYPE很方便,我尽可能多地使用它,但是您必须确保在设置时值的类型是正确的。
https://stackoverflow.com/questions/13458926
复制相似问题