A
A
Alexander Batula2019-05-05 15:45:13
PostgreSQL
Alexander Batula, 2019-05-05 15:45:13

Enum what's wrong?

good afternoon everyone have enum

public enum gendertype {
        F,
        M;
    }

And the fields in the gender database with the gender type
help me figure it out, I fill out the form
<div class="w3-card-4">
        <div class="w3-container w3-center w3-green">
            <h2>Add user</h2>
        </div>
        <form method="post" class="w3-selection w3-light-grey w3-padding">
            <label for="first-name">Имя:
                <input  type="text" id="first-name" name="first-name"  class="w3-input w3-animate-input w3-border w3-round-large" style="width: 30%"><br />
            </label><br />
            </label>

            <label for="last-name">Фамилия:
                <input type="text" id="last-name" name="last-name"  class="w3-input w3-animate-input w3-border w3-round-large" style="width: 30%"><br />
            </label><br />

            <label for="birthDate">Дата рождения
                <input class="input-field" type="text" id="birthDate" name="birthDate">
            </label>
            <select name="gender">
                <option value="F">жен.</option>
                <option value="M">муж.</option>
            </select>
            <button type="submit" class="w3-btn w3-green w3-round-large w3-margin-bottom">Submit</button>
        </form>
    </div>

and there is a servlet
@Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String firsName = req.getParameter("first-name");
        String lastName = req.getParameter("last-name");
       LocalDate birthDate = LocalDate.parse(req.getParameter("birthDate"));
       String genderUser = req.getParameter("gender");


        try {
            PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO data_user(firs_name, last_name, birth_date ,gender) VALUES (?,?,?,?)");
            preparedStatement.setString(1, firsName);
            preparedStatement.setString(2, lastName);
            preparedStatement.setDate(3,  java.sql.Date.valueOf(birthDate));

            preparedStatement.execute();

        }
        catch(SQLException e) {
            throw new IllegalStateException(e);
        }

    }

How to add a user through preparedStatement using enum specifying his gender, I get the following error
java.lang.IllegalStateException: org.postgresql.util.PSQLException: ERROR: column "gender" is of type gender but expression is of type character varying
Hint: You will need to rewrite or cast the expression.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2019-05-05
@tul6skiu

I don't know java, but where do you even pass the 4th parameter to the request?
The error says that java, using the extended protocol, declares that it transfers data of the varchar type, instead of the gender type. Yes, for the extended protocol these are different things and we will not automatically change the type.
Try like this:
VALUES (?,?,?,cast(? as gender))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question