这段代码有毒,如何破

wangxiu9527 2018-07-27 04:24:11
// 这个是错误
HTTP Status 500 – Internal Server Error


Type Exception Report

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception
java.lang.NullPointerException
com.lyq.bean.StudentDao.saveStudent(StudentDao.java:66)
com.lyq.servlet.AddStudent.doPost(AddStudent.java:42)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


Note The full stack trace of the root cause is available in the server logs


---------------------------------------------------
web.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>11_1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>AddStudent</display-name>
<servlet-name>AddStudent</servlet-name>
<servlet-class>com.lyq.servlet.AddStudent</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddStudent</servlet-name>
<url-pattern>/AddStudent</url-pattern>
</servlet-mapping>
</web-app>
-------------------------------------
//java bean
package com.lyq.bean;
/**
* 学生信息类
* @author Li YongQiang
*
*/
public class Student {
// 学号
private int id;
// 姓名
private String name;
// 年龄
private int age;
// 性别
private String sex;
// 班级
private String classes;
public Student(){
}
public Student(String name, int age, String sex, String classes) {
this.name = name;
this.age = age;
this.sex = sex;
this.classes = classes;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes;
}
}
----------------------------------
package com.lyq.bean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class StudentDao {
/**
* 获取数据库连接
* @return Connection对象
*/
public Connection getConnection(){
// 数据库连接
Connection conn = null;
try {
// 加载数据库驱动,注册到驱动管理器
Class.forName("com.mysql.jdbc.Driver");
// 数据库连接字符串
String url = "jdbc:mysql://localhost:3306/db_database11";
// 数据库用户名
String username = "root";
// 数据库密码
String password = "111";
// 创建Connection连接
conn = DriverManager.getConnection(url,username,password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
// 返回数据库连接
return conn;
}
/**
* 添加学生信息
* @param student
* @return 更新影响的行数
*/
public int saveStudent(Student student){
int row = 0;
// 数据库连接
Connection conn = getConnection();
try {
// 添加学生信息的SQL语句
String sql = "insert into tb_student_add(name,age,sex,classes) values(?,?,?,?)";
// 获取PreparedStatement
PreparedStatement ps = conn.prepareStatement(sql);
// 对SQL语句中的第1个参数赋值
ps.setString(1, student.getName());
// 对SQL语句中的第2个参数赋值
ps.setInt(2, student.getAge());
// 对SQL语句中的第3个参数赋值
ps.setString(3, student.getSex());
// 对SQL语句中的第4个参数赋值
ps.setString(4, student.getClasses());
// 执行更新操作
row = ps.executeUpdate();
// 关闭PreparedStatement
ps.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
// 关闭Connection
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return row;
}
}
--------------------------------------
//servlet
package com.lyq.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lyq.bean.Student;
import com.lyq.bean.StudentDao;

/**
* 添加学生信息的Servlet
* @author Li YongQiang
*
*/
public class AddStudent extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 设置response的编码
response.setCharacterEncoding("GB18030");
// 获取PrintWriter
PrintWriter out = response.getWriter();
// 设置request的编码
request.setCharacterEncoding("GB18030");
// 获取姓名
String name = request.getParameter("name");
// 获取性别
String sex = request.getParameter("sex");
// 获取年龄
String age = request.getParameter("age");
// 获取班级
String classes = request.getParameter("classes");
// 实例化student
Student student = new Student(name,Integer.parseInt(age),sex,classes);
// 实例化StudentDao
StudentDao dao = new StudentDao();
// 添加学生信息
int row = dao.saveStudent(student);
if(row > 0){
// 更新成输出信息
out.print("成功添加了 " + row + "条数据!");
}else{
out.print("添加失败!");
}
out.flush();
out.close();
}

}

不知道那个错误是什么情况,是配置有问题吗
http://www.szwangxiu.com/
...全文
1254 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_32040927 2018-09-06
  • 打赏
  • 举报
回复
com.lyq.bean.StudentDao.saveStudent(StudentDao.java:66)
com.lyq.servlet.AddStudent.doPost(AddStudent.java:42) 500是个常见的错误了 空指针异常 字段为空 数据为空
JoeBlackzqq 2018-08-09
  • 打赏
  • 举报
回复
500 服务器内部错误,不懂java

61,110

社区成员

发帖
与我相关
我的任务
社区描述
层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。
社区管理员
  • HTML(CSS)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧