Like passing a Char to the bank?

Asked

Viewed 319 times

0

How do I pass one Char to the Bank?

Error

Exception in thread "main" java.sql.SQLException: ORA-12899: valor muito grande para a coluna "SYSTEM"."USUARIO"."TP_SEXO" (real: 2, máximo: 1)

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:447)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:951)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:513)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:227)
    at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:531)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:208)
    at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1046)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1336)
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3613)
    at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3714)
    at oracle.jdbc.driver.OraclePreparedStatementWrapper.execute(OraclePreparedStatementWrapper.java:1378)
    at br.com.younews.dao.UsuarioDao.adiciona(UsuarioDao.java:33)
    at br.com.younews.teste.TesteUsuarioDao.main(TesteUsuarioDao.java:20)

my code:

package br.com.younews.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.younews.beans.Usuario;
import br.com.younews.conexao.ConexaoFactory;

public class UsuarioDao {
    private Connection conn;
    private List<Usuario> user = new ArrayList<Usuario>();

    public UsuarioDao() throws Exception {
        this.conn = new ConexaoFactory().getConnection();
    }

    public Usuario adiciona(Usuario usuario) throws SQLException{
        String sql = "INSERT INTO USUARIO (EMAIL_USUARIO, NM_USUARIO, SENHA_USUARIO, TP_SEXO, DT_NASCIMENTO, FOTO_USUARIO, NM_LOCALIZACAO, NM_SOBRENOME, NM_NOME) VALUES (?,?,?,?,?,?,?,?,?)";
        PreparedStatement st = conn.prepareStatement(sql);
        st.setString(1, usuario.getEmail());
        st.setString(2, usuario.getNmUsuario());
        st.setString(3, usuario.getSenha());
        st.setInt(4, usuario.getSexo());
        st.setString(5, usuario.getDataNasc());
        st.setString(6, usuario.getFotoPerfil());
        st.setString(7, usuario.getLocalizacao());
        st.setString(8, usuario.getNome());
        st.setString(9, usuario.getSobrenome());
        st.execute();
        st.close();
        return usuario;
    }

    public List<Usuario> listarUsuario() throws Exception{
        List<Usuario> user = new ArrayList<Usuario>();
        PreparedStatement p = conn.prepareStatement("SELECT P.CD_USUARIO FROM USUARIO P LEFT JOIN USUARIO_AMIGO E ON E.USUARIO_CD_USUARIO1 = E.CD_AMIGO");
        ResultSet rs = p.executeQuery();
        while(rs.next()){
            Usuario usuario = new Usuario();
            usuario.setIdLogin(rs.getString("CD_USUARIO"));
            usuario.setNmUsuario(rs.getString("NM_USUARIO"));
        }
        rs.close();
        p.close();
        return user;
    }
}
  • What is the type of data in the database?

  • TP_SEXO CHAR(1 BYTE)

  • Pretty sure if you switch to CHAR(1 CHAR) it’ll work. you can try it?

  • To tag is SQL server, but the error is Oracle. Can fix the tag?

  • Jbueno, it worked, I switched to CHAR(2 CHAR), but my doubt, I passed as "int" because I do not know how to pass a char, but in the bank the correct one should not be 1?

  • fixed man :)

  • Ahhhhhh, the question is set of PreparedStatement?

  • that same guy

Show 3 more comments

2 answers

3

The real solution is to use a NCHAR in the database. This type is suitable for storing Unicode characters, as is the case with the one used in Java. If you choose not to do this you will have to make a conversion with possible loss of value.

I do not recommend it. I also do not recommend using CHAR(2). It will even solve by coincidence. It is recording 1 character that should occupy 2 bytes in a space for 2 characters. Byte and character are different concepts and to exchange them is to ask for trouble. One hour this will not work. Stick to the solution where you save 1 characters - which is what you want - which allows you to be multibyte, which is the case with NCHAR.

By the comment below I think the problem wasn’t even Unicode. Probably if using a setString() in place of a setInt() will solve the problem without changing the type in the database. It seems to me that you are writing ASCII even. Only the use of the wrong method is that it was forcing to have a larger text.

  • I did it man, but since I left "st.setInt" he’s registering with value "77" knows how to fix it?

3


The solution is to trade in your PreparedStatement of setInt() for setString().

st.setString(4, String.valueOf(usuario.getSexo()));

Using the setInt() the variable char will be converted to Integer when sending to the bank. And when converting a char for Integer the value sent is the equivalent of the code ASCII of char, that’s why I was being saved 77, instead of M at the bank.

Here’s a table of how JDBC treats Java types:

Tipo JDBC              Tipo Java
-------------------------------------------
CHAR                   String
VARCHAR                String
LONGVARCHAR            String
NUMERIC                java.math.BigDecimal
DECIMAL                java.math.BigDecimal
BIT                    boolean
BOOLEAN                boolean
TINYINT                byte
SMALLINT               short
  • but so I’ll have to change my class correct ? because there I left as CHAR

  • No, young man. I shall supplement the question with the explanation.

  • ok, I get it, sorry if it seems lengo in that, I’m very novice guy, first time I’m working with connection, now it returns me the following error "The method setString(int, String) in the type Preparedstatement is not applicable for the Arguments (int, char)"

  • No problem, no one was born knowing. Regarding the error, read again there in the answer the part of setString().

  • I just saw rs, thank you very much man!!!

Browser other questions tagged

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