解决方案 »

  1.   

    cn.temple.db.UserTable.addUser 检查这里有没实例化的对象
      

  2.   

    at cn.temple.db.UserTable.addUser(UserTable.java:29)  调试看看。
      

  3.   

    at cn.temple.db.UserTable.addUser(UserTable.java:29)看下UserTable第29行代码
      

  4.   

    System.out.println(userName + " "+password);
    这行代码有没有打印出东西来?看看这里是不是空
      

  5.   

    可以打印出 userName和password的正确值
      

  6.   

    错在UserTable的29行,你应该把UserTable的代码贴出来。
      

  7.   

    package cn.temple.db;import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;/**
     * @author Administrator
     *
     */
    public class UserTable {

    /**
     * 添加新用户
     * @param userName:用户名
     * @param password:密码
     * @return
     */
    public boolean addUser(String userName, String password){
    String sql = "insert into userTable values (?,?)";

    Connection conn = new DB().getConnection();

    try {
    PreparedStatement pstmt = conn.prepareStatement(sql);
    pstmt.setString(1, userName);
    pstmt.setString(2, password);
    pstmt.executeUpdate();
    return true;
    } catch (SQLException e) {
    e.printStackTrace();
    } finally {
    //close(conn);
    }
    return false;
    }

    /**
     * @param userName:用户名
     * @param newPwd:新密码
     * @return 
     */
    public boolean changePassword(String userName, String newPwd){
    String sql = " update userTable set password = ? where username = ?";
    Connection conn = new DB().getConnection();
    try {
    PreparedStatement pstmt = conn.prepareStatement(sql);
    pstmt.setString(1, userName);
    pstmt.setString(2, newPwd);
    pstmt.executeUpdate();
    return true;
    } catch (SQLException e) {
    e.printStackTrace();
    } finally {
    close(conn);
    }

    return false;
    }

    /**
     * @param conn
     * 关闭数据库连接
     */
    private void close(Connection conn){
    try {
    conn.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }
      

  8.   

    看下getConnection是不是返回了null吧。
      

  9.   

    返回了,执行下面测试代码是成功的。
    @Test
    public void addTest(){
    new UserTable().addUser("zhangsan", "123");
    }
    只是在浏览器中添加就不行了
      

  10.   

    空指针最好找原因了 楼主用Debug模式调式一下,在这行打个断点Connection conn = new DB().getConnection();
      

  11.   

     request.getRequestDispatcher("login.jsp").forward(request, response);这句前面加个System.out.println(“here”);测试下判断执行没有,如果没有执行,基本就是判断处的问题
      

  12.   

    1主键是不是自动生成的 ,是不是第二次添加的时候 存在两个相同的对象所以添加失败。
    2拆开 if(new UserTable().addUser(userName, password))
    为bool flog=new UserTable().addUser(userName, password)
    if(flog)
    看flog有没有
      

  13.   

    我也遇到了类似的问题,代码测试可用通过,通过浏览器就报空指针异常,我是CachedRowSetImpl rowset = new CachedRowSetImpl() 这里空指针了(代码测试可以的),不知道楼主解决没?
      

  14.   

    你的insert语句应该写具体点
    insert into userTable(username,pasword) values (?,?) 
    在就是设个标志位不要直接返回true
    boolean flag = false ;