servlet层调用biz业务层出现浏览器 500错误,解决方法 dao数据访问层 数据库Util工具类都可能出错 通过新建一个测试类复制代码逐步测试查找出最终出错原因
package com.swift.jztk.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.swift.jztk.biz.IQuestionBiz; import com.swift.jztk.biz.QuestionBizImpl; @WebServlet("/getQuestions") public class GetQuestions extends HttpServlet { private static final long serialVersionUID = 1L; IQuestionBiz biz=new QuestionBizImpl();//生成业务层对象 public GetQuestions() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); String testType=request.getParameter("testType"); String json=biz.getQuestions(testType);//业务层对象调用方法,方法中使用dao数据访问层访问数据库,代码问题执行失败 response.getWriter().println(json); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
biz.getQuestions(testType);//biz业务层调用的方法.getQuestions(testType)如下:
package com.swift.jztk.biz; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Random; import com.google.gson.Gson; import com.swift.jztk.bean.Result; import com.swift.jztk.bean.Root; import com.swift.jztk.dao.IQuestionDao; import com.swift.jztk.dao.QuestionDaoImpl; public class QuestionBizImpl implements IQuestionBiz { IQuestionDao dao = new QuestionDaoImpl(); @SuppressWarnings("null") @Override public String getQuestions(String testType) { List<Result> list = null; if (testType.equals("rand")) { HashSet<Integer> set = new HashSet<Integer>();//生成一个哈希集合,用于存放随机数 Random ran = new Random(); for (;;) {//无限循环 int n = ran.nextInt(200) + 1;// 1~200间的随机整数,不加1是0到199 set.add(n);// 随机数放到 整数类型的哈希集合中,保证没有相同的整数 if (set.size() == 100) { break; } } Iterator<Integer> it = set.iterator();//迭代器,用于获取集合中各条内容 while (it.hasNext()) { int id = it.next(); Result result = dao.getResultById(id); list.add(result);//加入对象列表集合 Collections.sort(list, new Comparator<Result>() {// 比较器 匿名内部类 @Override public int compare(Result o1, Result o2) { int id1 = o1.getId(); int id2 = o2.getId(); return id1 - id2;// 按照id大小从小到大排序 } }); } } else if (testType.equals("order")) { list = dao.getAll();//MVC架构,数据访问层用接口进行连接,得到数据库中全部数据 } String json=listToJson(list); return json; } //把得到的List<Result>对象列表集合转换成字符串 public String listToJson(List<Result> list) { Root root = new Root(); root.setResult(list); root.setStatusCode("000000"); root.setDesc("请求成功");//json实体类对象赋值 Gson gson = new Gson(); String json = gson.toJson(root);//json实体类对象用Gson解析成字符串 return json; } }
Result result = dao.getResultById(id);//biz层调用数据访问层的方法.getResultById();
list = dao.getAll();//biz层调用数据访问层的方法.getAll();
数据访问层代码如下:
package com.swift.jztk.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.swift.jztk.bean.Result; import com.swift.jztk.util.DBUtil; public class QuestionDaoImpl implements IQuestionDao { //@Override public List<Result> getAll() { Connection conn = DBUtil.getConn(); PreparedStatement ps = null; ResultSet rs = null; List<Result> list = null; try { ps = conn.prepareStatement("select * from sw_question"); rs = ps.executeQuery(); list = new ArrayList<Result>(); while (rs.next()) { int id = rs.getInt("id"); String question = rs.getString("question"); String answer = rs.getString("answer"); String item1 = rs.getString("item1"); String item2 = rs.getString("item2"); String item3 = rs.getString("item3"); String item4 = rs.getString("item4"); String expalins = rs.getString("expalins"); String url = rs.getString("url"); Result result = new Result(id, question, answer, item1, item2, item3, item4, expalins, url); list.add(result); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.closeAll(conn, ps, rs); } return list; } @Override public Result getResultById(int id) { Connection conn = DBUtil.getConn(); PreparedStatement ps = null; ResultSet rs = null; Result result = null; try { ps = conn.prepareStatement("select * from sw_question where id=?"); ps.setInt(1, id); rs = ps.executeQuery(); if(rs.next()) { String question = rs.getString("question"); String answer = rs.getString("answer"); String item1 = rs.getString("item1"); String item2 = rs.getString("item2"); String item3 = rs.getString("item3"); String item4 = rs.getString("item4"); String expalins = rs.getString("expalins"); String url = rs.getString("url"); result = new Result(id, question, answer, item1, item2, item3, item4, expalins, url); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.closeAll(conn, ps, rs); } return result; } }
提示com.mysql.jdbc.Driver找不到 NullClassException异常
导入的这些包都是正常的,没有红线,可是浏览器出现500错误:
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.swift.jztk.bean.Result; import com.swift.jztk.util.DBUtil;
Connection conn = DBUtil.getConn();
都已导入,没有问题
检查数据库工具类:
package com.swift.jztk.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBUtil { // 连接MySQL数据库工具 public static Connection getConn() { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); try { conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sw_database?user=root&password=root"); } catch (SQLException e) { e.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } return conn; } // 关闭数据库连接、sql连接、结果集 public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs) { if (conn != null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;
这些包都导入了
com.mysql.jdbc.Driver的驱动却找不到,原因有两个:
一个是数据库突然崩溃,MySQL数据库软件发生问题,重新安装解决
一个是光导包不出红线是没用的,还需要把mysql-connector-java-5.1.7-bin.jar这个jar包没有复制到lib
Never waste time any more, Never old man be a yong man