日期:2014-05-17  浏览次数:21722 次

com.microsoft.sqlserver.jdbc.SQLServerException: 列名 n_id 无效。
在前台要显示某部门下的新闻,界面上没有报错,控制台里报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注释掉,就报下一行的字段无效!!
请问各位是哪里的问题?

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

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

 
------解决方案--------------------
看是不是你的database..和数据库搞错了 或article.setN_id(rs.getInt("n_id"));n_id是不是真写错了
------解决方案--------------------
或者是查询出来的结果 一条数据都没有
------解决方案--------------------
你先把你的sql打印出来,然后在数据库里直接执行以下,看看能不能通过。
判断是SQL的问题还是程序的问题。