当前位置: 代码迷 >> J2SE >> java连mysql抛空指针错误
  详细解决方案

java连mysql抛空指针错误

热度:222   发布时间:2016-04-24 01:34:21.0
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);            }        }    }}
  相关解决方案