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;
        }
    }
 
     
    