Skip to content
Advertisement

PostgreSQL, Java : How to print jast the RAISE EXCEPTION message without info about the place in the code?

This is my java method:

    public static boolean addEmployee(Employee employee) {
        String sql = "SELECT addemployee(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";

        try (Connection conn = connect();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, employee.getFirstName());
            .
            .
            .
            ps.execute();
            return true;
        } catch (SQLException e) {
            showMessageDialog(null, e.getMessage());
            System.out.println(e.getMessage());
        }
        return false;
    }

This is my PostgreSQL function:

CREATE OR REPLACE FUNCTION addEmployee(. . .)
    RETURNS BOOLEAN AS
$$
DECLARE
    existsCheck BOOLEAN;
    iddd        INTEGER;
BEGIN
    SELECT EXISTS(SELECT ed.username
                  FROM employee_data ed
                  WHERE ed.username = usernameAdd)
    INTO existsCheck;
    IF (SELECT existsCheck = FALSE) THEN
       .
       .
       .
    ELSE
        RAISE EXCEPTION 'Nazwa użytkownika jest zajęta!';
    END IF;
    RETURN TRUE;
END ;
$$

When the exception is thrown, I get such a message dialog: [image]1

Error message is gotten when I try to register and the username I put already exists. So I get the alert “Username is taken by someone, try else” (1 line on the screenshot). And I want to print only these words, without this second line on the screenshot.

How can I get rid of this second line?

Advertisement

Answer

Use String.split() to split tne exception message and print what needed.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement