-1

Here is the connection class

public class ConnectionManager {

    Connection cn = null;

    public Connection getConnection() throws ClassNotFoundException, SQLException {
        String dbPath = "jdbc:mysql://localhost:3306/postjob";
        String username = "root";
        String password = "root";
        Class.forName("com.mysql.jdbc.Driver");

        cn = DriverManager.getConnection(dbPath, username, password);
        return cn;
    }

    public void closeConnection() throws SQLException{
        cn.close();


    }
}

Here is my service class

public class UserdetailsServices {

    public Connection connection;
    public ResultSet resultset;
//    private Object resultSet;

    public UserdetailsServices() {
        try {
            connection = new ConnectionManager().getConnection();
//here I am getting connection=null.
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(UserdetailsServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(UserdetailsServices.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     *
     * @param user
     * @return
     */
    public boolean registerUser(UserModal user) {
        try {
            java.sql.PreparedStatement psmt = connection.prepareStatement("insert into userdetails(role,name,gender,email,password,cpassword,contact) values(?,?,?,?,?,?,?)");
            psmt.setString(1, user.getRole());
            psmt.setString(2, user.getName());
            psmt.setString(3, user.getGender());
            psmt.setString(4, user.getEmail());
            psmt.setString(5, user.getPassword());
            psmt.setString(6, user.getCpassword());
            psmt.setString(6, user.getContact());
            int flag = psmt.executeUpdate();
            if (flag > 0) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException ex) {

            return false;
        }
    }
abarisone
  • 3,429
  • 11
  • 27
  • 49
  • 3
    Could you please provide more information about the problem you encountered ? (Exception, stacktrace, etc...) ? – Mickael Jun 21 '16 at 12:07
  • 1
    From what I remember `DriverManager.getConnection(dbPath, username, password);` can't return `null`. Are you sure that you are not getting exception instead? Consider using [edit] option and provide more information in your question (like error/exception description). – Pshemo Jun 21 '16 at 12:17
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. – abarisone Jun 23 '16 at 12:06

1 Answers1

0

Insert have 7 fields but preparaedstatement is setting only 6 fields. It is missing setting contact value. That's why it is giving null error.

Instead of this

psmt.setString(6, user.getCpassword());
psmt.setString(6, user.getContact());

use this

psmt.setString(6, user.getCpassword());
psmt.setString(7, user.getContact());
Gautam Savaliya
  • 1,209
  • 2
  • 17
  • 28