java中数据库操作中空字段问题陈述:
表中某一行会有空字段,i初始值为1,利用结果集rs判断当前的字段是否为空,数据库如下
colName: a b c d
colValue: x y (空白表示空字段)
执行如下程序段(其他程序略)
while(!rs.getString(i).equals(null))
{
i++;
}
当i=1时,!rs.getString(i).equals(null)为true,i++,变为2
当i=2时,!rs.getString(i).equals(null)为true,i++,变为3
i=3时rs.getString(3)为空,就出现异常,也就是说这段程序要判断字段是否为空,就必定会出现异常;
怎么解决这个问题?谢谢解答!
表中某一行会有空字段,i初始值为1,利用结果集rs判断当前的字段是否为空,数据库如下
colName: a b c d
colValue: x y (空白表示空字段)
执行如下程序段(其他程序略)
while(!rs.getString(i).equals(null))
{
i++;
}
当i=1时,!rs.getString(i).equals(null)为true,i++,变为2
当i=2时,!rs.getString(i).equals(null)为true,i++,变为3
i=3时rs.getString(3)为空,就出现异常,也就是说这段程序要判断字段是否为空,就必定会出现异常;
怎么解决这个问题?谢谢解答!
[align=center]==== 思想重于技巧 ====
[/align]
[align=center]==== 思想重于技巧 ====
[/align]
try
{
// 连接数据库
dbc = new DataBaseConnection() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
// 进行数据库查询操作
ResultSet rs = pstmt.executeQuery() ;
while(rs.next())
{
// 查询出内容,之后将查询出的内容赋值给phone对象
Phone phone = new Phone() ;
phone.setId(rs.getInt(1)) ;
phone.setName(rs.getString(2)) ;
phone.setNumberGu(rs.getString(3)) ;
phone.setNumberYi(rs.getString(4)) ;
phone.setRe(rs.getString(5)) ; // 将查询出来的数据加入到List对象之中
all.add(phone) ;
System.out.print("rs不为空");
}
rs.close() ;
pstmt.close() ;
}
catch (Exception e)
{
throw new Exception("数据库操作出现异常") ;
}
finally
……
加入某条数据的第二条数据为空,一执行到那个循环的 、
phone.setName(rs.getString(2)) ;时。就又异常了。