com.microsoft.sqlserver.jdbc.SQLServerException: 列名 n_id 无效。

告白气球 2012-07-31 03:12:46
在前台要显示某部门下的新闻,界面上没有报错,控制台里报com.microsoft.sqlserver.jdbc.SQLServerException: 列名 n_id 无效。
下面是报错的方法:
public ArrayList<Article> DepArticleList(int n, int page,int n_sid,int n_stid , boolean n_check)
{
ArrayList<Article> list = new ArrayList<Article>();
if(!n_check&&n_stid!=0)
{
sql = "select top "+n+" * from n_news inner join n_stype on n_news.n_stid=n_stype.s_tid where n_news.n_stid="+n_stid+" and n_id not in (select top "+(page-1)*n+" n_id from n_news inner join n_stype on n_news.n_stid=n_stype.s_tid where n_news.n_stid="+n_stid+" order by n_stype.s_tid desc) order by n_stype.s_tid desc";
}
if(n_check&&n_sid!=0)
{
sql="select top "+n+" * from n_news inner join n_sroom on n_news.n_sid=n_sroom.s_id where n_news.n_sid="+n_sid+" and n_check='1' and n_id not in " +
"(select top "+(page-1)*n+" n_id from n_news inner join n_sroom on n_news.n_sid=n_sroom.s_id where n_news.n_sid="+n_sid+" and n_check='1' order by n_news.n_sid desc)" +
" order by n_news.n_sid desc";
}

try
{
dbconn = new DBConn();
conn = dbconn.getDataSource().getConnection();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next())
{
Article article=new Article();
article.setN_id(rs.getInt("n_id"));
article.setN_author(rs.getString("n_author"));
article.setN_check(rs.getBoolean("n_check"));
article.setN_commend(rs.getBoolean("n_commend"));
article.setN_content(rs.getString("n_content"));
article.setN_date(rs.getString("n_date").substring(0, 10));
article.setN_fname(rs.getString("n_fname"));
article.setN_hit(rs.getInt("n_hit"));
article.setN_index(rs.getBoolean("n_index"));
article.setN_mid(rs.getInt("n_mid"));
article.setN_mpic(rs.getString("n_mpic"));
article.setN_rpic(rs.getString("n_rpic"));
article.setN_sid(rs.getInt("n_sid"));
article.setN_stid(rs.getInt("n_stid"));
article.setN_tid(rs.getInt("n_tid"));
article.setN_title(rs.getString("n_title"));
article.setUpsize_ts(rs.getString("upsize_ts"));
article.setWhjs_sortid(rs.getInt("whjs_sortid"));
article.setS_tname(rs.getString("s_tname"));
list.add(article);
}
}
catch(Exception e)
{
logger.error("ArticleList()方法出错:"+e.getMessage());
e.printStackTrace();
}
finally
{
dbconn.closeResultSet(rs);
dbconn.closePreparedStatement(pstmt);
dbconn.closeConnection(conn);
}
return list;
}

如果我把n_id注释掉,就报下一行的字段无效!!
请问各位是哪里的问题?
...全文
1237 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuyongedu342ji9 2012-07-31
  • 打赏
  • 举报
回复
哦 原来如此
告白气球 2012-07-31
  • 打赏
  • 举报
回复
谢谢大家帮忙 ,不过都不是你们说的错,可能是我的if有冲突,我把其他的删除了就留了一个,就OK了!
  • 打赏
  • 举报
回复
你先把你的sql打印出来,然后在数据库里直接执行以下,看看能不能通过。
判断是SQL的问题还是程序的问题。
liuyongedu342ji9 2012-07-31
  • 打赏
  • 举报
回复
或者是查询出来的结果 一条数据都没有
liuyongedu342ji9 2012-07-31
  • 打赏
  • 举报
回复
看是不是你的database..和数据库搞错了 或article.setN_id(rs.getInt("n_id"));n_id是不是真写错了
告白气球 2012-07-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

你应该在 SELECT 后用别名 把具体字段列出来,你的 n_id是不是在多个表中存在 ,如果是,则需要用别名

例如:
select a.n_id from tableA a inner join tableB b on a.n_id = b.n_id
[/Quote]
不是,n_id仅是一张表的字段,还要考虑一个问题,把n_id注掉,就报n_author无效!!!
五哥 2012-07-31
  • 打赏
  • 举报
回复
你应该在 SELECT 后用别名 把具体字段列出来,你的 n_id是不是在多个表中存在 ,如果是,则需要用别名

例如:
select a.n_id from tableA a inner join tableB b on a.n_id = b.n_id

本课程根据讲师十多年在世界500强外企的生产环境中的SQL Serer数据库管理和项目实施经验倾心打造。课程系统性强,知识体系完整,覆盖90%以上的企业环境下SQL Server高可用场景,课程中不仅演示详细的操作步骤,更加突出最常见的故障和问题,让学员少走“弯路”,不只是让学员学会“操作”更能让学员“操作”的规范,满满的干货分享,一些课程资料(架构图、部署规划表格等)不仅可以帮助学员掌握技能,也可以作为学员在企业生产环境中实施SQL Server高可用的配置文档、操作手册等。课程的实验环境介绍:1)全部基于微软域环境和企业版SQL Server AOAG - 95%以上的企业环境都是在域环境中,不介绍非域环境和标准版的SQL Server高可用性组,这的配置在企业中较罕见,没有实践意义,不浪费学员时间。2)相应域环境已提前部署和配置好 - 学员导入虚拟机即可开始实验,无需从零开始搭建域环境,所有实验中SQL Server均已加域,直入主题,节省大量时间。3)最新的Windows Server故障转移集群(WS2016、WS2019)和最新版本的SQL Server(SQL2017、SQL2019) -  WS2016-SQL2017与WS2019-SQL2019是目前大多数企业SQL Server高可用的主要平台,基于微软产品生命周期现在一些企业也在讲早期的AOAG向这两个版本迁移,掌握这两种组合不仅让学员学会,更能学有所用。本课程为后续SQL Server进阶课程铺垫,是通向SQL Server DBA 专家的必经之路,讲师每周答疑两次。所有课程资料包括:课程PPT、架构图、部署规划表格、各类脚本学员均可下载。     

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧