0
It’s the first time I’m working with connections, we’re doing a job for college on a social network, now we’re doing the method insert.
My doubt, when I run the test class it returns me an error. What causes this error?
Note: we cannot use any type of framework
ERROR
Exception in thread "main" java.sql.SQLException: Parâmetro IN ou OUT ausente do índice:: 10
at oracle.jdbc.driver.OraclePreparedStatement.processCompletedBindRow(OraclePreparedStatement.java:1844)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3608)
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)
Beans class
package br.com.younews.beans;
import java.util.ArrayList;
public class Usuario {
private String idLogin;
private String senha;
private String nome;
private String sobrenome;
private String email;
private String nmUsuario;
private char sexo;
private String dataNasc;
private String fotoPerfil;
private String localizacao;
private ArrayList<Usuario> amigos;
public Usuario() {
super();
}
public Usuario(String idLogin, String senha, String nome, String sobrenome,
String email, String nmUsuario, char sexo, String dataNasc,
String fotoPerfil, String localizacao, ArrayList<Usuario> amigos) {
super();
this.idLogin = idLogin;
this.senha = senha;
this.nome = nome;
this.sobrenome = sobrenome;
this.email = email;
this.nmUsuario = nmUsuario;
this.sexo = sexo;
this.dataNasc = dataNasc;
this.fotoPerfil = fotoPerfil;
this.localizacao = localizacao;
this.amigos = amigos;
}
public String getIdLogin() {
return idLogin;
}
public void setIdLogin(String idLogin) {
this.idLogin = idLogin;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getSobrenome() {
return sobrenome;
}
public void setSobrenome(String sobrenome) {
this.sobrenome = sobrenome;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNmUsuario() {
return nmUsuario;
}
public void setNmUsuario(String nmUsuario) {
this.nmUsuario = nmUsuario;
}
public char getSexo() {
return sexo;
}
public void setSexo(char sexo) {
this.sexo = sexo;
}
public String getDataNasc() {
return dataNasc;
}
public void setDataNasc(String dataNasc) {
this.dataNasc = dataNasc;
}
public String getFotoPerfil() {
return fotoPerfil;
}
public void setFotoPerfil(String fotoPerfil) {
this.fotoPerfil = fotoPerfil;
}
public String getLocalizacao() {
return localizacao;
}
public void setLocalizacao(String localizacao) {
this.localizacao = localizacao;
}
public ArrayList<Usuario> getAmigos() {
return amigos;
}
public void setAmigos(ArrayList<Usuario> amigos) {
this.amigos = amigos;
}
}
DAO class
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.setString(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;
}
}
My Test Class
package br.com.younews.teste;
import br.com.younews.beans.Usuario;
import br.com.younews.dao.UsuarioDao;
public class TesteUsuarioDao {
public static void main(String[] args) throws Exception {
Usuario user = new Usuario();
user.setEmail("[email protected]");
user.setNmUsuario("guuilh");
user.setSenha("123");
user.setSexo("M");
user.setDataNasc("03/05/1994");
user.setFotoPerfil("www.blabla.com");
user.setLocalizacao("SP");
user.setNome("Guilherme");
user.setSobrenome("almeida");
UsuarioDao userDAO = new UsuarioDao();
userDAO.adiciona(user);
}
}
AAAH man thank you very much, but you know how to step a char? now he gives this error in the sex column " ORA-12899: very large value for the column "SYSTEM"." USER"." TP_SEXO" (real: 2, max: 1)" I’m passing an int and the bank is like 1 I don’t understand
– Guilherme Almeida
I haven’t worked with Java for a long time, but I can help you. Open a new question that I’m looking for if there is something special to pass
char
to the bank.– Jéf Bueno
beauty guy, quarreled
– Guilherme Almeida