为了账号安全,请及时绑定邮箱和手机立即绑定

为什么不报错,但是就是查询不出值


public class servletList extends HttpServlet {

private static final String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //数据库驱动

//连接数据库的URL地址

private static final String url="jdbc:sqlserver://localhost:1433;DatabaseName=message"; 

private static final String username="sa";//数据库的用户名

private static final String password="123";//数据库的密码

private static Connection conn=null;

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

try {

Class.forName(driver);

conn = DriverManager.getConnection(url, username, password);

String sql = "select id,command,description,contents from messageTable";

PreparedStatement statement = conn.prepareStatement(sql);

ResultSet rs = statement.executeQuery();

List<messageItems> messageList = new ArrayList<messageItems>();

while(rs.next())

{

messageItems items = new messageItems();

messageList.add(items);

items.setId(rs.getInt("id"));

items.setCommand(rs.getString("command"));

items.setDescription(rs.getString("description"));

items.setContents(rs.getString("contents"));

}

req.setAttribute("messageList", messageList);

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

req.getRequestDispatcher("/WEB-INF/jsp/back/list.jsp").forward(req, resp);

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

this.doGet(req, resp);

}

}


正在回答

3 回答

劝你分层写吧,这样更能容易找到问题所在。(说实话你把数据库链接写在servlet中我也是挺醉的,servlet累死了啊!耦合度太高)

0 回复 有任何疑惑可以回复我~

这页代码没问题啊

0 回复 有任何疑惑可以回复我~

while(rs.next())

{

messageItems items = new messageItems();

messageList.add(items);//此句应移到while的最后

items.setId(rs.getInt("id"));

items.setCommand(rs.getString("command"));

items.setDescription(rs.getString("description"));

items.setContents(rs.getString("contents"));

}


0 回复 有任何疑惑可以回复我~
#1

294761325 提问者

移动了还是没用,我猜是list页面没接收到值?
2017-04-15 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

为什么不报错,但是就是查询不出值

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信