String createTime = cursor.getString(cursor.getColumnIndex("create_time"));报错Value must be ≥ 0 but `getColumnIndex` can be -1
时间: 2024-03-20 16:41:03 浏览: 34
这个错误的原因是 `getColumnIndex()` 方法没有找到指定的列名,返回了 -1。这通常是由于列名拼写错误或查询结果集中确实没有该列名导致的。
你可以在调用 `getString()` 方法之前先检查一下 `getColumnIndex()` 方法的返回值是否为 -1,如果是,则说明查询结果集中没有该列名,需要检查一下列名拼写是否正确。示例代码如下:
```
int columnIndex = cursor.getColumnIndex("create_time");
if (columnIndex == -1) {
// 列名拼写错误或查询结果集中没有该列名
} else {
String createTime = cursor.getString(columnIndex);
// 处理 createTime
}
```
相关问题
public List<StudentBuy> getBuyCourseList(String studentNo) { Connection con = null; PreparedStatement pre = null; ResultSet resultSet = null; try { con = DBUtils.getConnection(); String sql = "select * from tb_student_buy where student_no = ?"; pre = con.prepareStatement(sql); pre.setString(1, studentNo); resultSet = pre.executeQuery(); List<StudentBuy> studentBuyList = new ArrayList<>(); while (resultSet.next()) { String studentNo1 = resultSet.getString("student_no"); String studentName = resultSet.getString("student_name"); String isbn = resultSet.getString("isbn"); String bookName = resultSet.getString("book_name"); Date createTime = resultSet.getDate("create_time"); StudentBuy buys = new StudentBuy(); buys.setStudentNo(studentNo1); buys.setStudentName(studentName); buys.setBookName(bookName); buys.setIsbn(isbn); buys.setCreateTime(createTime); studentBuyList.add(buys); } return studentBuyList; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtils.closeConnection(con, pre, resultSet); } return null; }
这是一个Java类,名为StudentBuyDao,其中包含一个getBuyCourseList(String studentNo)方法。这个方法从数据库中获取指定学生购买的所有课程的信息,并将它们存储在一个List<StudentBuy>对象中。在方法中,它首先获取一个数据库连接,然后通过执行一个带有参数的SQL查询语句来获取指定学生的购买记录。接着,它使用一个while循环来遍历所有的结果集,并将每个购买记录的信息存储在一个StudentBuy对象中。最后,它将每个StudentBuy对象添加到一个List<StudentBuy>对象中,并返回该对象。需要注意的是,在方法的最后,它关闭了所有的数据库连接和资源,以确保正确的释放资源。如果执行过程中出现异常,则会打印异常信息,并返回null值。
public class VMenuInfo : MenuInfo { public MenuInfo _menuInfo { get; set; } public VMenuInfo() { //_menuInfo = this; base.Mkey = _menuInfo.Mkey; base.MUrl = _menuInfo.MUrl; base.Create = _menuInfo.Create; base.MOrder = _menuInfo.MOrder; base.iconUrl = _menuInfo.iconUrl; base.ChangeId = _menuInfo.ChangeId; base.ChangeTime = _menuInfo.ChangeTime; base.IsDeleted = _menuInfo.IsDeleted; base.MId = _menuInfo.MId; base.ParentId = _menuInfo.ParentId; base.MName = _menuInfo.MName; base.ParentName = _menuInfo.ParentName; base.IsTop = _menuInfo.IsTop; base.CreateTime = _menuInfo.CreateTime; } public List<VMenuInfo> children { get; set; } }改进这段代码
首先,无论是继承类还是组合类,都需要在构造函数中对基类或者组合对象进行初始化。但是,在这个代码中,由于没有对_menuInfo进行初始化,所以会导致_nullReferenceException_异常,因此需要在构造函数中给_menuInfo对象赋值:
public class VMenuInfo : MenuInfo
{
public MenuInfo _menuInfo { get; set; }
public VMenuInfo()
{
_menuInfo = new MenuInfo(); //初始化_menuInfo对象
base.Mkey = _menuInfo.Mkey;
base.MUrl = _menuInfo.MUrl;
base.Create = _menuInfo.Create;
base.MOrder = _menuInfo.MOrder;
base.iconUrl = _menuInfo.iconUrl;
base.ChangeId = _menuInfo.ChangeId;
base.ChangeTime = _menuInfo.ChangeTime;
base.IsDeleted = _menuInfo.IsDeleted;
base.MId = _menuInfo.MId;
base.ParentId = _menuInfo.ParentId;
base.MName = _menuInfo.MName;
base.ParentName = _menuInfo.ParentName;
base.IsTop = _menuInfo.IsTop;
base.CreateTime = _menuInfo.CreateTime;
}
public List<VMenuInfo> children { get; set; }
}
另外,由于VMenuInfo及其子类会使用到MenuInfo中的所有属性,因此可以考虑将VMenuInfo中的属性和MenuInfo保持一致,避免不必要的数据冗余和查询更新操作。最终的代码如下:
public class MenuInfo
{
public string Mkey { get; set; }
public string MUrl { get; set; }
public string Create { get; set; }
public int MOrder { get; set; }
public string iconUrl { get; set; }
public long ChangeId { get; set; }
public DateTime ChangeTime { get; set; }
public bool IsDeleted { get; set; }
public int MId { get; set; }
public int ParentId { get; set; }
public string MName { get; set; }
public string ParentName { get; set; }
public bool IsTop { get; set; }
public DateTime CreateTime { get; set; }
}
public class VMenuInfo : MenuInfo
{
public List<VMenuInfo> Children { get; set; }
}