首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

java连mysql抛空指针错误

2012-03-26 
java连mysql抛空指针异常我新手,主要想实现一个简单的注册、登陆功能。一个写了4个类:USer类:Java codepacka

java连mysql抛空指针异常
我新手,主要想实现一个简单的注册、登陆功能。
一个写了4个类:
USer类:

Java code
package cn.pzhu.domain;import java.util.Date;public class User {    private String id;    private String username;    private String email;    private String password;    private Date birthday;    private String nickname;    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getEmail() {        return email;    }    public void setEmail(String email) {        this.email = email;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public Date getBirthday() {        return birthday;    }    public void setBirthday(Date birthday) {        this.birthday = birthday;    }    public String getNickname() {        return nickname;    }    public void setNickname(String nickname) {        this.nickname = nickname;    }}



操纵数据库的类UserDaoImpl:
Java code
package cn.pzhu.dao.impl;import java.sql.*;import cn.pzhu.dao.UserDao;import cn.pzhu.domain.User;public class UserDaoImpl implements UserDao {    static Connection connection;    static Statement statement;    static PreparedStatement preparedStatement;    static String sql;    static{        try {            Class.forName("com.mysql.jdbc.Driver");            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1?user=root&password=pzhu");        } catch (ClassNotFoundException e1) {            throw new RuntimeException(e1);        }catch (SQLException e2) {            throw new RuntimeException(e2);        }    }    //数据库user表字段顺序:username password id email birthday nickname    public void add(User user) {        sql = "insert into user values(?,?,?,?,?,?)";        try {                        preparedStatement = connection.prepareStatement(sql);            preparedStatement.setString(1, user.getUsername());            preparedStatement.setString(2, user.getPassword());            preparedStatement.setString(3, user.getId());            preparedStatement.setString(4, user.getEmail());            preparedStatement.setDate(5,new java.sql.Date(user.getBirthday().getTime()));            preparedStatement.setString(6, user.getNickname());            preparedStatement.executeUpdate();        } catch (SQLException e3) {            throw new RuntimeException(e3);        }        try {            if(preparedStatement!=null){                preparedStatement.close();                preparedStatement = null;            }            if (connection!=null) {                connection.close();                connection = null;            }        } catch (Exception e4) {            throw new RuntimeException(e4);        }            }    //数据库user表字段顺序:username password id email birthday nicknameusername password id email birthday nickname    public User find(String username,String password ) {        sql =  "select * from user where username='"+username+"' and password='"+password+"'";        try {            statement = connection.createStatement();            ResultSet resultSet = statement.executeQuery(sql);            if(resultSet.next()){                User user = new User();                user.setBirthday(resultSet.getDate(5));                user.setEmail(resultSet.getString(4));                user.setId(resultSet.getString(3));                user.setNickname(resultSet.getString(6));                user.setPassword(resultSet.getString(2));                user.setUsername(resultSet.getString(1));                                return user;            }else {                return null;            }        } catch (SQLException e5) {            throw new RuntimeException(e5);        }        finally{            try {                if (statement!=null) {                    statement.close();                    statement=null;                }                if (connection!=null) {                    connection.close();                    connection=null;                }            } catch (SQLException e6) {                throw new RuntimeException(e6);            }        }    }            //用户注册时检测数据库中是否已经存在该用户名    public boolean find(String username) {        sql =  "select * from user where username='"+username+"'";        try {            statement = connection.createStatement();            ResultSet resultSet = statement.executeQuery(sql);            if(resultSet.next()){                return true;            }else {                return false;            }        } catch (SQLException e5) {            throw new RuntimeException(e5);        }        finally{            try {                if (statement!=null) {                    statement.close();                    statement=null;                }                if (connection!=null) {                    connection.close();                    connection=null;                }            } catch (SQLException e6) {                throw new RuntimeException(e6);            }        }    }} 



业务类BusinessServiceImpl:上面的类和方法都测试通过,但是写到这个类测试的时候,就有错误了
Java code
package cn.pzhu.service.impl;import cn.pzhu.dao.impl.UserDaoImpl;import cn.pzhu.domain.User;import cn.pzhu.exception.UserExistException;//对web层提供所有的服务public class BusinessServiceImpl {        UserDaoImpl userDaoImpl = new UserDaoImpl();    //提供注册服务    public void register(User user) throws UserExistException {        //判断用户是否已经存在        boolean b = userDaoImpl.find(user.getUsername());        if (b) {            throw new UserExistException();//用户已经存在,向web层抛一个用户已存在异常(测试的时候,如果用户存在,整个程序到这里可以正常执行)        }else {            userDaoImpl.add(user);//但是如果不存在用户,向数据库添加的时候,就有异常了        }    }    //提供登陆服务    public User login(String username,String password ) {        return userDaoImpl.find(username, password);    }}



定义了一个异常UserExistException:
Java code
package cn.pzhu.exception;public class UserExistException extends Exception {    public UserExistException() {        // TODO Auto-generated constructor stub    }    public UserExistException(String message) {        super(message);        // TODO Auto-generated constructor stub    }    public UserExistException(Throwable cause) {        super(cause);        // TODO Auto-generated constructor stub    }    public UserExistException(String message, Throwable cause) {        super(message, cause);        // TODO Auto-generated constructor stub    }}


异常是UserDaoImpl类的add()方法抛空指针异常


[解决办法]
常规做法,其实是专门封装函数来获取 connection,并负责关闭。

另外,也不要将这些定义为static:
static Connection connection;
static Statement statement;
static PreparedStatement preparedStatement;
static String sql;


比如:
Java code
   public boolean find(String username) {        String sql =  "select * from user where username='"+username+"'";        Connection cn = getConnection(); // 这里负责返回一个连接;        try {            Statement statement = cn.createStatement();            ResultSet resultSet = statement.executeQuery(sql);            if(resultSet.next()){                return true;            }else {                return false;            }        } catch (SQLException e5) {            throw new RuntimeException(e5);        }        finally{            closeConnection(cn);        }   } 

热点排行