你好 请问下java.sql.SQLException: 列名无效 这个怎么解决 我的字段和数据库是一样的
我把sql拿到数据库就能运行这里就后台报无效列名下面是报错那里/***分页,模糊查询*/@OverridepublicintRoleKey(Stringkey,intpa...
我把sql拿到数据库就能运行 这里就后台报 无效列名 下面是报错那里
/**
* 分页,模糊查询
*/
@Override
public int RoleKey(String key, int pageSize, int pageNo,
List<RoleVo> List) throws SQLException {
Connection conn =null;
int max=0;
try{
conn = DButil.getConnection();
if(key==null){
key="%";
}else{
key="%"+key.trim()+"%";
}
StringBuilder count=new StringBuilder();
count.append("select count(*) from sys_role where name like ? or icon like ? or remark like ?");
PreparedStatement sta=conn.prepareStatement(count.toString());
sta.setString(1, key);
sta.setString(2, key);
sta.setString(3, key);
ResultSet r=sta.executeQuery();
r.next();
max=r.getInt(1);
if(max==0){
return 0;
}
//执行分页查询
StringBuilder sql=new StringBuilder();
sql.append("select y.* from (")
.append("select x.*, rownum rn from (")
.append("select * from sys_role where name like ? or icon like ? or remark like ? order by name")
.append(") x");
int beginCount=(pageNo-1)*pageSize+1;
int endCount=pageNo*pageSize;
sql.append(") y").append(" where rn between ").append(beginCount).append(" and ").append(endCount);
sta = conn.prepareStatement(sql.toString());
sta.setString(1, key);
sta.setString(2, key);
sta.setString(3, key);
r = sta.executeQuery();
while (r.next()) {
RoleVo vo = new RoleVo();
String id=r.getString("id");
vo.setId(id);
String name = r.getString("name");
vo.setName(name);
String icon=r.getString("icon");
vo.setIcon(icon);
String remark = r.getString("remark");
vo.setRemark(remark);
int state = r.getInt("state");
vo.setState(state);
List.add(vo);
}
}
finally{
DButil.close(conn);
}
return max;
} 展开
/**
* 分页,模糊查询
*/
@Override
public int RoleKey(String key, int pageSize, int pageNo,
List<RoleVo> List) throws SQLException {
Connection conn =null;
int max=0;
try{
conn = DButil.getConnection();
if(key==null){
key="%";
}else{
key="%"+key.trim()+"%";
}
StringBuilder count=new StringBuilder();
count.append("select count(*) from sys_role where name like ? or icon like ? or remark like ?");
PreparedStatement sta=conn.prepareStatement(count.toString());
sta.setString(1, key);
sta.setString(2, key);
sta.setString(3, key);
ResultSet r=sta.executeQuery();
r.next();
max=r.getInt(1);
if(max==0){
return 0;
}
//执行分页查询
StringBuilder sql=new StringBuilder();
sql.append("select y.* from (")
.append("select x.*, rownum rn from (")
.append("select * from sys_role where name like ? or icon like ? or remark like ? order by name")
.append(") x");
int beginCount=(pageNo-1)*pageSize+1;
int endCount=pageNo*pageSize;
sql.append(") y").append(" where rn between ").append(beginCount).append(" and ").append(endCount);
sta = conn.prepareStatement(sql.toString());
sta.setString(1, key);
sta.setString(2, key);
sta.setString(3, key);
r = sta.executeQuery();
while (r.next()) {
RoleVo vo = new RoleVo();
String id=r.getString("id");
vo.setId(id);
String name = r.getString("name");
vo.setName(name);
String icon=r.getString("icon");
vo.setIcon(icon);
String remark = r.getString("remark");
vo.setRemark(remark);
int state = r.getInt("state");
vo.setState(state);
List.add(vo);
}
}
finally{
DButil.close(conn);
}
return max;
} 展开
展开全部
你确定字段是name、icon、remark、state?
另外应该把报错的那一行指出来,这样错误容易定位
另外应该把报错的那一行指出来,这样错误容易定位
更多追问追答
追问
int state = r.getInt("state"); 报的这行 我就纳闷了
追答
通过r.getMetadata()把所有列名打印出来比较一下就知道了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我耐着性子看了下, 下面这句的 y.* 我实在没找到y 难道是我眼神不好?
sql.append("select y.* from (")
sql.append("select y.* from (")
追问
sql.append("select y.* from (")
。。。。。。。。。。。。。。
sql.append(") y")
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询