Java with Mysql (JDBC) - Type YEAR

Asked

Viewed 190 times

1

I have a question on how to record a YEAR type in the MYSQL database using a Java application from a Textfield, I have tried using Date type, but I can’t take only the year to record in the YEAR field. tried with Simpledateformat but it ends up generating a new type of data that does not work with JDBC’s preparedStatement method.

Below is a diagram of the bank and the Entity and View class:

Diagrama MySQL

View:

 private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {                                          
    Livros livros = new Livros();
    LivrosDAO livrosDao = new LivrosDAO();
    livros.setTitulo(tfTitulo.getText());
    livros.setIsbn(Integer.parseInt(tfISBN.getText()));
    livros.setAno(Short.valueOf(tfAno.getText()));
    livros.setPaginas(Integer.parseInt(tfPaginas.getText()));
    livros.setGenerosEntity((Generos) cbGenero.getSelectedItem());
    livros.setAutoresEntity((Autores) cbAutor.getSelectedItem());
    livros.setEditorasEntity((Editoras) cbEditora.getSelectedItem());
    if (tfCodLivro.getText().isEmpty()) {
        livrosDao.inserir(livros);
        livrosTableModel.addLivro(pesquisar(livros));
    } else {
        livros.setCodLivro(Integer.parseInt(tfCodLivro.getText()));
        livrosDao.atualizar(livros);
        livrosTableModel.updateLivros(linhaSelecionada, livros);
    }

    this.setVisible(false);
}

DAO:

public boolean inserir(Livros livro) {
    try {
        super.abrirConnection();
        super.preparedStatement = super.connection.prepareStatement(INSERT);
        preparedStatement.setInt(1, livro.getIsbn());
        preparedStatement.setString(2, livro.getTitulo());
        preparedStatement.setInt(3, livro.getAutoresEntity().getCodAutor());
        preparedStatement.setInt(4, livro.getEditorasEntity().getCodEditora());
        preparedStatement.setInt(5, livro.getGenerosEntity().getCodGenero());
        preparedStatement.setShort(6, livro.getAno());
        preparedStatement.setInt(7, livro.getPaginas());
        return preparedStatement.executeUpdate() != 0;

    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, "Erro ao inserir o registro" + ex.getMessage());
        return false;
    } finally {
        fecharPreparedStatement();
        fecharConnection();
    }

}

Entity:

public short getAno() {
    return ano;
}

public void setAno(Short ano) {
    this.ano = ano;
}
  • According to the documentation, this field becomes the type java.sql.Short and not in the short primitive as you are using in the getAno method, are different types.

  • 1

    Thanks! It worked

1 answer

0

Your getter method getAno() returns a primitive type "short," which is different from the Short type of the java.sql.Short. You should use java.sql.Short, which is the type used by the driver.

YEAR[(2|4)] YEAR If yearIsDateType Configuration Property is set to false, then the returned Object type is java.sql.Short. If set to true (the default) , then the returned Object is of type java.sql.Date with the date set to January 1st, at Midnight.

Ref https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-type-conversions.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.