我用servlet,用jdbc调用数据库的内容在jsp页面上显示,可是最后jsp页面没用将数据显示出来,下面是我的部分有关代码,急用,希望高手帮下忙! package com.org.vo; public class AirLine { private String airID; private String startCity; private String endCity; private String startTime; private String endTime; private String price; private String airType; public String getAirID() { return airID; } public void setAirID(String airID) { this.airID = airID; } public String getStartCity() { return startCity; } public void setStartCity(String startCity) { this.startCity = startCity; } public String getEndCity() { return endCity; } public void setEndCity(String endCity) { this.endCity = endCity; } public String getStartTime() { return startTime; } public void setStartTime(String startTime) { this.startTime = startTime; } public String getEndTime() { return endTime; } public void setEndTime(String endTime) { this.endTime = endTime; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getAirType() { return airType; } public void setAirType(String airType) { this.airType = airType; } } package com.org.dao; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import com.org.dbc.DatabaseConnection; import com.org.vo.AirLine; public class ShowLineDao { private PreparedStatement pstmt = null; private ResultSet rs = null; public void airDelete(String sql) { try { new DatabaseConnection().getConnection(); } catch (Exception e) { e.printStackTrace(); } try { int rows = pstmt.executeUpdate(sql); if (rows >= 1) { System.out.println("--成功删除--"); } else { System.out.println("--删除失败--"); } } catch (Exception e) { e.printStackTrace(); } } public ArrayList<AirLine> getAirLineList(String sql) { ArrayList<AirLine> list = new ArrayList<AirLine>(); try { new DatabaseConnection().getConnection(); } catch (Exception e) { e.printStackTrace(); } try { rs = pstmt.executeQuery(sql); while (rs.next()) { String airID = rs.getString(1); String startCity = rs.getString(2); String endCity = rs.getString(3); String startTime = rs.getString(4); String endTime = rs.getString(5); String price = rs.getString(6); String airType = rs.getString(7); AirLine airLine = new AirLine(); airLine.setAirID(airID); airLine.setStartCity(startCity); airLine.setEndCity(endCity); airLine.setStartTime(startTime); airLine.setEndTime(endTime); airLine.setPrice(price); airLine.setAirType(airType); list.add(airLine); } } catch (Exception e) { e.printStackTrace(); } return list; } } package com.org.servlet; import java.io.IOException; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.org.dao.ShowLineDao; import com.org.vo.AirLine; public class ShowLineServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ShowLineDao showLineDao = new ShowLineDao(); String sql = "select * from airline"; ArrayList<AirLine> list = showLineDao.getAirLineList(sql); request.setAttribute("list", list); request.getRequestDispatcher("/showline.jsp").forward(request, response); } } package com.org.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.org.dao.ShowLineDao; public class DelectAirServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String airID = request.getParameter("airID"); String sql = "delete from airline where airID="" " + airID + """"; ShowLineDao showLineDao = new ShowLineDao(); showLineDao.airDelete(sql); request.getRequestDispatcher("showLineServlet").forward(request, response); } } package com.org.dbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DatabaseConnection { private static final String DBDRIVER = "org.gjt.mm.mysql.Driver"; private static final String DBURL = "jdbc:mysql://localhost:3306/airticket"; private static final String DBUSER = "root"; private static final String DBPASSWORD = "123456"; private Connection conn = null; public DatabaseConnection() { try { Class.forName(DBDRIVER); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { this.conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD); } catch (SQLException e) { e.printStackTrace(); } } public Connection getConnection() { return this.conn; } public static void close(Connection conn) { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(PreparedStatement pstmt) { if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } } <%@page import="com.org.vo.AirLine"%> <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>查询预订机票信息</title> </head> <% request.setCharacterEncoding("GBK"); %> <body> <center> <h1>欢迎进入航空售票系统</h1> <hr> <table border="1" cellspacing="0" cellpadding="0" width="100%" style="align:center;"> <tr bgcolor="ff9900" style="font-weight:bold;"> <th>航班编号</th> <th>航班价格</th> <th>出发城市</th> <th>到达城市</th> <th>出发时间</th> <th>到达时间</th> <th>仓位等级</th> <th width="100" colspan="2">操作</th> </tr> <c:forEach items="${list}" var="airLine"> <tr> <td>${airLine.airID }</td> <td>${airLine.price }</td> <td>${airLine.startCity }</td> <td>${airLine.endCity }</td> <td>${airLine.startTime }</td> <td>${airLine.endTime }</td> <td>${airLine.airType }</td> <td><a href="deleteAirServlet?airID=${airLine.airID }">删除</a></td> </tr> </c:forEach> </table> </center> </body> </html> |
|
你好!你的jsp和servlet都没问题,问题在于你的数据库,没查到数据,或者出错.你先判断一下数据有没有连接上,打印一下我找到问题了,你连数据库都没连接
我找到问题了 package com.org.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import com.org.dbc.DatabaseConnection; import com.org.vo.AirLine; public class ShowLineDao { private PreparedStatement pstmt = null; private ResultSet rs = null; private Connection conn; public void airDelete(String sql) { try { new DatabaseConnection().getConnection(); } catch (Exception e) { e.printStackTrace(); } try { int rows = pstmt.executeUpdate(sql); if (rows >= 1) { System.out.println("--成功删除--"); } else { System.out.println("--删除失败--"); } } catch (Exception e) { e.printStackTrace(); } } public ArrayList<AirLine> getAirLineList(String sql) { ArrayList<AirLine> list = new ArrayList<AirLine>(); try { conn = new DatabaseConnection().getConnection();//加上conn } catch (Exception e) { e.printStackTrace(); } try { pstmt = conn.prepareStatement(sql);//这2行改成这样 rs = pstmt.executeQuery();// while (rs.next()) { String airID = rs.getString(1); String startCity = rs.getString(2); String endCity = rs.getString(3); String startTime = rs.getString(4); String endTime = rs.getString(5); String price = rs.getString(6); String airType = rs.getString(7); AirLine airLine = new AirLine(); airLine.setAirID(airID); airLine.setStartCity(startCity); airLine.setEndCity(endCity); airLine.setStartTime(startTime); airLine.setEndTime(endTime); airLine.setPrice(price); airLine.setAirType(airType); list.add(airLine); } } catch (Exception e) { e.printStackTrace(); } return list; } } |
|
楼主,跟你说个事吧,教你如何调试,从3个方面
1:servlet,先把连接数据库的dao接口注释,模拟数据,看时候能够正常显示, 2:是数据库,打印数据库的查询信息或者其他信息 3,是页面,页面的就不解释了 还有一种方法就是启动tomcat用debug模式启动,然后在servlet类里打断点,按F5,或者F6下一步,一步步找到问题所在,希望我能帮到你. |
|
30分 |
+1 |
String airID = rs.getString(1);
id还是用long型比较合适。 感觉你这工程应该就是拷贝的,然后稍微调试了一下就用了。 要不这种感觉应该不会发生这种失误。 |
|
我觉的 是这个样子的,你那个 这不是 用ssh开发 的 那些 并没有 值站 这样一个 容器,你那个用foreach 取值的时候 不对应给是${request.list} 你这样 在 看看 行不行啊。
|
|
你这个人学框架学懵了,该醒醒了 |
|
10分 |
看了下 代码没有什么问题 你在你的ShowLineServlet 类中测试一下dopost方法 输出list.size()看看有没有数据 如果有数据 可能是你jsp页面标签有问题
|