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
servlet层调用biz业务层出现浏览器 500错误,解决方法 dao数据访问层 数据库Util工具类都可能出错 通过新建一个测试类复制代码逐步测试查找出最终出错原因的更多相关文章
- 随机获得MySQL数据库中100条数据方法 驾照题库项目 MVC架构 biz业务层的实现类 根据考试类型rand或order通过dao数据访问层接口得到数据库中100或全部数据
package com.swift.jztk.biz; import java.util.Collections; import java.util.Comparator; import java.u ...
- 微信公众号支付之坑:调用支付jsapi缺少参数 timeStamp等错误解决方法
这段时间一直比较忙,一忙起来真感觉自己就只是一台挣钱的机器了(说的好像能挣到多少钱似的,呵呵):这会儿难得有点儿空闲时间,想把前段时间开发微信公众号支付遇到问题及解决方法跟大家分享下,这些“暗坑”能不 ...
- idea运行Tomcat的servlet程序时报500错误解决方法
今天在测试使用Tomcat运行servlet小程序时,在传递参数时,出现了如上错误. 开始我以为是配置出了问题,就把项目删除了又建立了一遍,结果亦然. 经过仔细排查,发现问题,先说明问题原因:idea ...
- 在包a中新建一个类A,在类A中有一个int add(int m)方法,用来求1+2+…+m 的和。在包b中新建一个类B,在类B中有一个int cheng(int n)方法,用来求n! 的结果。在包c中新建一个主类C,调用A、B中的方法输出1+2+…+30的和, 以及5!的计算结果。
package a; public class A { public void add(int m) { int sum=0; for (int i = 1; i <=m; i++) { sum ...
- view是视图层+action是控制层+service是业务层+dao是数据访问层。
- 项目架构开发:数据访问层之Repository
接上文 项目架构开发:数据访问层之Logger 本章我们继续IRepository开发,这个仓储与领域模式里边的仓储有区别,更像一个工具类,也就是有些园友说的“伪仓储”, 这个仓储只实现单表的CURD ...
- 数据访问层 (DAO)
数据持久化 持久化:将程序中的数据在瞬间状态下和持久状态间转换的机制(JDBC) 主要持久化操作:保存.删除.读取.和查找. 采用面向接口编程,可以降低代码间的耦合性,提高代码的可扩展性和可维护性. ...
- 数据访问层之Repository
数据访问层之Repository 接上文 项目架构开发:数据访问层之Logger 本章我们继续IRepository开发,这个仓储与领域模式里边的仓储有区别,更像一个工具类,也就是有些园友说的“伪 ...
- ClownFish:比手写代码还快的通用数据访问层
http://www.cnblogs.com/fish-li/archive/2012/07/17/ClownFish.html 阅读目录 开始 ClownFish是什么? 比手写代码还快的执行速度 ...
随机推荐
- Shape Number (最小表示法)
题目链接 一个字符串,这个字符串的首尾是连在一起的,要求寻找一个位置,以该位置为起点的字符串的字典序在所有的字符串中中最小. #include <bits/stdc++.h> using ...
- 阿里云服务器 linux 怎么安装php(PHPSTUDY)开发环境
1.首先登录行云管家(https://yun.cloudbility.com/login.html) wget -c http://lamp.phpstudy.NET/phpstudy.bin //下 ...
- Vue 5 -- axios、vuex
一.内容回顾 1.webpack(前端中工作,项目上线之前对整个前端项目优化) - entry:整个项目的程序入口(main.js或index.js): - output:输出的出口: - loade ...
- linux中文件的时间属性atime/mtime/ctime
文件的时间属性的概念 上图第7-9是时间,默认是ctime(文件修改时间),有三种时间属性: modify time mtime (文件内容被修改的时间) change ti ...
- ERROR [org.apache.hadoop.util.Shell] - Failed to locate the winutils binary in the hadoop binary path
错误日志如下: -- ::, DEBUG [org.apache.hadoop.metrics2.lib.MutableMetricsFactory] - field org.apache.hadoo ...
- js、jquery报错
js.jquery一直报错,是myecplise的问题, 右键项目->属性->builder->js validate去掉单个文件出错 右击文件->myecplise-> ...
- 关于SQL Server数据库中的标识列
一.标识列的定义以及特点 SQL Server中的标识列又称标识符列,习惯上又叫自增列. 该种列具有以下三种特点: 1.列的数据类型为不带小数的数值类型 2.在进行插入(Insert)操作时,该列的值 ...
- 网页mp3不能获取,报404问题解决
js有些时候有些报错就是很莫名其妙 一 代码没错,js,html一点红都没有 然后上正式的时候,mp3不能播放音乐,报404 二 出这个错误,我第一反应是正式的配置有问题,毕竟开发测试都可以是不是? ...
- C#对INI文件读写
C#本身没有对INI格式文件的操作类,可以自定义一个IniFile类进行INI文件读写. using System; using System.Collections.Generic; using S ...
- 去除pycharm的波浪线
PyCharm使用了较为严格的PEP8的检查规则,如果代码命名不规范,甚至多出的空格都会被波浪线标识出来,导致整个编辑器里铺满了波浪线,右边的滚动条也全是黄色或灰色的标记线,很是影响编辑.这里给大家分 ...