asunnysally
asunnysally
2016-10-30 07:15
采纳率: 100%
浏览 1.0k
已采纳

大神们指点一下:jsp用户注册,为什么我用test类测试显示插入成功,但是jsp运行出现错误500

图片说明

registerServlet

package 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 dao.Dao;

import bean.User;

public class registerservlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init() throws ServletException {

// Put your code here

}
public void destroy() {

super.destroy(); // Just puts "destroy" string in log

// Put your code here

}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");  
    response.setCharacterEncoding("utf-8");  
    String username = request.getParameter("username");  
    String pwd = request.getParameter("pwd");  
    String phone = request.getParameter("phone");  
    String email = request.getParameter("email");  
    String school = new String(request.getParameter("school").getBytes("ISO8859_1"),"utf-8");  
    User user = new User();  
    user.setUsername(username);  
    user.setPwd(pwd);  
    user.setPhone(phone);  
    user.setEmail(email);  
    user.setSchool(school);
    Dao dao = new Dao();  
    dao.addUser(user);  

    request.getRequestDispatcher("main.jsp").forward(request,response); 
}

}

Dao

package dao;

import getConnection.GetConnection;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import bean.User;

public class Dao {

private Connection conn;

private PreparedStatement pstat;

String sql = "";

/** 
 *  
 * 用户登录 
 */  
public boolean login(User user) throws SQLException {  
    conn = GetConnection.getConnection();  
    boolean i = false;  
    sql = "select * from user where username=? and pwd=?";  

    pstat = conn.prepareStatement(sql);  

    pstat.setString(1, user.getUsername());  
    pstat.setString(2, user.getPwd());  

    ResultSet rs1 = (ResultSet) pstat.executeQuery();  
    if (rs1.next()) {  
        i = true;  
        rs1.close();  
        pstat.close();  
    } else {  
        i = false;  
        rs1.close();  
        pstat.close();  
    }  
    conn.close();  
    return i;  
}  

/** 
 * 用户注册 
 */  
public boolean addUser(User user) {  
    conn = GetConnection.getConnection();  
    sql = "insert into user(username,pwd,phone,email,school) values(?,?,?,?,?)";  
    try {  
        pstat = conn.prepareStatement(sql);  
        pstat.setString(1, user.getUsername());  
        pstat.setString(2, user.getPwd());  
        pstat.setString(3, user.getPhone());  
        pstat.setString(4, user.getEmail());  
        pstat.setString(5, user.getSchool());  
        pstat.executeUpdate();  
        if(pstat.executeUpdate() > 0){
            return true;
        } 
        pstat.close();  
        conn.close();  


    } catch (SQLException e) {  
        e.printStackTrace();  
    }
    return false;  

}  

}

getconnection

package getConnection;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

/**

  • 用JDBC的方法获得数据库的连接
  • */
    public class GetConnection {
    // 通过静态方法注册驱动,获得连接
    public static Connection getConnection() {
    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost/user";
    Connection con = null;
    try {
    Class.forName(driver);
    try {
    con = DriverManager.getConnection(url, "root", "1234");
    } catch (SQLException e) {
    e.printStackTrace();
    }
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
    System.out.println("已获得数据库的连接");
    return con;
    } }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

9条回答 默认 最新

  • qq_22857293
    小祝特烦恼 2016-10-30 07:45
    已采纳

    在Dao找到52行,空指针异常了。把52行代码贴出来。看样子是没有获取的conn,获取连接失败了。

    点赞 评论
  • niaonao
    niaonao 2016-10-30 07:50

    空指针异常
    就是数据或者对象没有获取到或传送到,在Dao 文件的52行使用的某个对象是空对象null,检查一下为空对象的原因,找找它的上一步,是否实例化对象,传送对象是否接收到

    点赞 评论
  • oSanYeCao1234567
    RoidCoder 2016-10-30 07:59

    服务端异常,空指针错误,打下断点调试下

    点赞 评论
  • csdn9_14
    陈 俊 刚 2016-10-30 09:38

    空指针异常,Dao.java 52行。

    点赞 评论
  • ApatheCrazyFan
    ApacheCrazyFan 2016-10-30 11:02

    String url = "jdbc:mysql://localhost/user";
    这个对吗?

    点赞 评论
  • asunnysally
    asunnysally 2016-10-30 12:57

    毕业设计卡到这一步做不下去了,大家拜托帮帮忙啦,谢谢

    点赞 评论
  • asunnysally
    asunnysally 2016-10-30 12:57

    第52行是 pstat = conn.prepareStatement(sql);
    空指针异常要怎么改

    点赞 评论
  • asunnysally
    asunnysally 2016-10-30 13:19

    啊啊啊改了一天终于出来了,是jar包没有放对位置导致获取连接失败

    点赞 评论
  • niaonao
    niaonao 2016-11-01 05:01

    改好了就行
    楼主遇到错误自己解决错误是一种学习进步的过程

    点赞 评论

相关推荐