為什么不報(bào)錯(cuò),但是就是查詢不出值
public class servletList extends HttpServlet {
private static final String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //數(shù)據(jù)庫(kù)驅(qū)動(dòng)
//連接數(shù)據(jù)庫(kù)的URL地址
private static final String url="jdbc:sqlserver://localhost:1433;DatabaseName=message";?
private static final String username="sa";//數(shù)據(jù)庫(kù)的用戶名
private static final String password="123";//數(shù)據(jù)庫(kù)的密碼
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);
}
}
2017-06-27
勸你分層寫(xiě)吧,這樣更能容易找到問(wèn)題所在。(說(shuō)實(shí)話你把數(shù)據(jù)庫(kù)鏈接寫(xiě)在servlet中我也是挺醉的,servlet累死了?。●詈隙忍?
2017-06-18
這頁(yè)代碼沒(méi)問(wèn)題啊
2017-04-15
while(rs.next())
{
messageItems items = new messageItems();
messageList.add(items);//此句應(yīng)移到while的最后
items.setId(rs.getInt("id"));
items.setCommand(rs.getString("command"));
items.setDescription(rs.getString("description"));
items.setContents(rs.getString("contents"));
}