这程序有值返回吗
public Collection getAllMessages(int pagesize, int page) {// 创建数据库连接对象:
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){}
try { Connection con=DriverManager.getConnection("jdbc:odbc:MyDB","","");
Statement stmt = null;
ResultSet rs = null;
// 创建数据记录集对象:
stmt = con.createStatement();
// sql语句:
String sql = "use blog select * from blogmsg order by msg_ID desc limit "
+ (page - 1) * pagesize + "," + pagesize;
// 执行sql语句:
// 执行sql语句,返回一个记录集到rs:
rs = stmt.executeQuery(sql);
Collection c = new ArrayList();
BlogMsg msg = null;
while (rs.next()) {
msg = new BlogMsg();
msg.setmsg_ID(rs.getInt("msg_ID"));
msg.setmsg_title(rs.getString("msg_title"));
msg.setmsg_content(rs.getString("msg_content"));
msg.setauthor(rs.getString("author"));
msg.setmsg_date(rs.getDate("msg_date"));
c.add(msg);
msg = null;
}
return c;
} catch (SQLException sqlExc) {
sqlExc.printStackTrace();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
[[it] 本帖最后由 随便来一记 于 2008-4-10 00:07 编辑 [/it]]
----------------解决方案--------------------------------------------------------
use blog select * from blogmsg order by msg_ID desc
这段在SQL SERVER中运行有信息返回的,是正确的。
[[it] 本帖最后由 随便来一记 于 2008-4-10 00:08 编辑 [/it]]
----------------解决方案--------------------------------------------------------
回复 2# 的帖子
在查询分析器中运行正确,并不一定在java程序中是对的!因为要传递参数!你的数据库源访问用户名和密码是空的吗?
一般都是推荐用专用JDBC驱动连接数据库的,很少用jdbc-odbc桥连接,我不是特别了解这种连接方式的细节。
关于Java连接SQL Server 2K 我写了一篇文章在我的博客了,你可以去参考一下
----------------解决方案--------------------------------------------------------