SQLite only supports TYPE_FORWARD_ONLY 不支持游标移动的问题

前端之家收集整理的这篇文章主要介绍了SQLite only supports TYPE_FORWARD_ONLY 不支持游标移动的问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

为了验证用户登录,检验用户名和密码是否匹配,写了下面的代码

String sql = "select * from t_user where name=?");
pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
pstmt.setString(1,name);
rs = pstmt.executeQuery();
rs.last();
int count = rs.getRow();
rs.beforeFirst();
while(rs.next()){
  u.setUserName(rs.getString("name"));
  u.setPwd(rs.getString("pwd"));
  u.setEmail(rs.getString("email"));
  u.setGroupId(rs.getInt("groupid"));
  u.setUserId(rs.getInt("rowid"));
}
 
 
 

其中count为统计resultset 记录行数,但是sqlite 报错:sqlite only supports TYPE_FORWARD_ONLY cursors

意思是sqlite 不支持游标移动,网上搜了一下,大致有下面两种解决办法:

1. 多执行一次 select count(*) as rowcount from t_user where name =? 来获取记录条数,显而易见效率比较低。

2.我们为何不在游标移动的过程中计数呢。

public User getByName(String name){
		User u = new User();
		Connection conn = null;
		PreparedStatement  pstmt = null;
		ResultSet rs = null;
		//result row count
		int count = 0;
		try {
			conn = DBUtil.getConnection();
			String sql="SELECT rowid,* FROM t_user WHERE name=?;";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,name);
		    rs = pstmt.executeQuery();
		    while(rs.next()){
		    	count ++;
		    	u.setUserName(rs.getString("name"));
		    	u.setPwd(rs.getString("pwd"));
		    	u.setEmail(rs.getString("email"));
		    	u.setGroupId(rs.getInt("groupid"));
		    	u.setUserId(rs.getInt("rowid"));
		    }
		} catch (sqlException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeConnection(rs,pstmt,conn);
		}
		if(count ==0){
	    	return null;
	    }
		return u;
	}
原文链接:https://www.f2er.com/sqlite/200339.html

猜你在找的Sqlite相关文章