正在回答 回答被采纳积分+1
6回答
一叶知秋519
2018-01-30 10:50:50
你好,关闭顺序最优依次是ResultSet Statement Connection;因此建议将ConnectionUtil()方法中的release方法的关闭顺序调整一下:
public static void release(Connection con, Statement sta, ResultSet res) { try { if (res != null) { res.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (sta != null) { sta.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (con != null) { con.close(); } } catch (SQLException e) { e.printStackTrace(); } } } }
你修改关闭顺序后再试下,如果还有问题,可以再次提问。祝学习愉快~
慕容5410851
2018-01-29 19:27:48
ConnectionUtil类的代码如下,没发现什么问题:
package common; import java.sql.*; public final class ConnectionUtil { private ConnectionUtil(){} static { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } private static String url = "jdbc:mysql://localhost:3306/message_board"; private static String user = "root"; private static String passward = "lin123"; public static Connection getConnection(){ try { return DriverManager.getConnection(url, user, passward); } catch (SQLException e) { e.printStackTrace(); } return null; } public static void release(Connection con, Statement sta, ResultSet res){ try { if (res != null) { res.close(); } } catch (SQLException e) { e.printStackTrace(); }finally { try { if (con != null){ con.close(); } } catch (SQLException e) { e.printStackTrace(); }finally { try { if (sta != null){ sta.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } }
好帮手慕珊
2018-01-29 15:17:25
你好!下面这个类中,MessageDao只是声明了,没有创建对象,可以定义一个MessageService的构造方法,在该构造方法中创建MessageDao对象,然后赋值给messageDao。一般出现空指针异常都是因为调用方法的对象不存在造成的。所以可以看一下控制台的错误提示,会指出具体是哪一行代码出问题,然后进行原因排查,看看是否创建了对象,或者引用是否赋值的是一个非空的对象。祝学习愉快!
package service; import bean.Message; import dao.MessageDao; import java.util.List; public class MessageService { private MessageDao messageDao; public MessageService(){ messageDao = new MessageDao(); } public List<Message> getMessage(int page, int pageSize){ return messageDao.getMessage(page, pageSize); } public int count(){ return messageDao.count(); } }
慕容5410851
2018-01-29 14:04:30
package dao; import bean.Message; import common.ConnectionUtil; import java.sql.*; import java.util.ArrayList; import java.util.List; public class MessageDao { public List<Message> getMessage(int page, int pageSize){ Connection con = ConnectionUtil.getConnection(); PreparedStatement sta = null; ResultSet res = null; List<Message> list = new ArrayList<Message>(); String sql = "select * from message order by create_time desc limit ?, ?"; try { sta = con.prepareStatement(sql); sta.setInt(1,(page-1)*pageSize); sta.setInt(2,pageSize); res = sta.executeQuery(); while (res.next()){ Message message = new Message(res.getLong("id"), res.getLong("user_id"), res.getString("username"), res.getString("title"), res.getString("content"), res.getTimestamp("create_time")); list.add(message); } } catch (SQLException e) { System.out.println("连接失败"); e.printStackTrace(); }finally { ConnectionUtil.release(con, sta, res); } //System.out.println(list); return list; } public int count(){ Connection con = ConnectionUtil.getConnection(); PreparedStatement sta = null; ResultSet res = null; String sql = "select count(*) total from message"; try { sta = con.prepareStatement(sql); res = sta.executeQuery(); while (res.next()){ return res.getInt("count"); } } catch (SQLException e) { e.printStackTrace(); }finally { ConnectionUtil.release(con, sta, res); } return 0; } public static void main(String[] args) { List<Message> list = new ArrayList<Message>(); list = new MessageDao().getMessage(1,5); System.out.println(list.size()); } }
慕容5410851
2018-01-29 14:04:04
package service; import bean.Message; import dao.MessageDao; import java.util.List; public class MessageService { private MessageDao messageDao; public MessageService(){ messageDao = new MessageDao(); } public List<Message> getMessage(int page, int pageSize){ return messageDao.getMessage(page, pageSize); } public int count(){ return messageDao.count(); } }
慕容5410851
2018-01-29 14:03:37
package servlet; import bean.Message; import service.MessageService; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; public class MessageListServlet extends HttpServlet{ private MessageService messageService; @Override public void init() throws ServletException { super.init(); messageService = new MessageService(); } @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String pageStr = req.getParameter("page"); int page = 1; if (pageStr != null && !pageStr.equals("")){ try { page = Integer.parseInt(pageStr); } catch (NumberFormatException e) { e.printStackTrace(); } } List<Message> messages = messageService.getMessage(page, 5); int count = messageService.count(); req.setAttribute("messages", messages); req.setAttribute("page",page); int last = count % 5 == 0 ? count / 5 : count / 5 + 1; req.setAttribute("last", last); req.getRequestDispatcher("/WEB-INF/views/message_list.jsp").forward(req, resp); } @Override public void destroy() { super.destroy(); messageService = null; } public static void main(String[] args) { MessageService messageService = new MessageService(); List<Message> messages = messageService.getMessage(1, 5); for (Message message : messages){ System.out.println(message); } } }
Java数据库开发与实战应用2018版
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星