Queria saber se tem como setar os parâmetros da query como no C#.
Em java eu faço assim e estou perdendo muito tempo pois preciso criar um método desse para cada query:
protected String getInsertQuery(){
return "Insert Into Pessoa (nome, idade) Values (?,?) ";
}
protected void setParameters(Pessoa value) throws Exception
{
try
{
this.getCommand().setString(1, value.getNome());
this.getCommand().setInt(2, value.getIdade());
}
catch(Exception ex)
{
throw new Exception("Erro ao preencher parametros.");
}
}
protected boolean insert(Pessoa value) throws Exception
{
try
{
this.setCommand(this.getConnection().prepareStatement(this.getInsertQuery()));
this.setParameters(value);
if(this.getCommand().executeUpdate() > 0)
return true;
}
catch(Exception ex)
{
throw new Exception("Erro ao inserir Pessoa.");
}
finally
{
this.getCommand().close();
this.getConnection().close();
}
return false;
}
Gostaria fazer desse jeito já que facilita bastante pois posso usar o mesmo método setParameters em todas as querys (código em C#):
protected override string InsertQuery
{
get { return "INSERT INTO Foto (id,nome,caminhoFoto,caminhoThumb,descricao,idProjeto) VALUES (?id,?nome,?caminhoFoto,?caminhoThumb,?descricao,?idProjeto)"; }
}
protected override void SetParameters(MySqlCommand command, Foto value)
{
command.Parameters.Add("?id", MySqlDbType.UInt32).Value = value.Id;
command.Parameters.Add("?nome", MySqlDbType.VarChar, 70).Value = value.Nome;
command.Parameters.Add("?caminhoFoto", MySqlDbType.VarChar, 100).Value = value.CaminhoFoto;
command.Parameters.Add("?caminhoThumb", MySqlDbType.VarChar, 100).Value = value.CaminhoThumb;
command.Parameters.Add("?descricao", MySqlDbType.VarChar, 300).Value = value.Descricao;
command.Parameters.Add("?idProjeto", MySqlDbType.UInt32).Value = value.IdProjeto;
}
protected override bool Insert(Foto value)
{
try
{
MySqlCommand command = new MySqlCommand(this.InsertQuery);
this.SetParameters(command, value);
if (base.ExecuteNonQuery(command) > 0)
return true;
return false;
}
catch (Exception e)
{
throw new DALException("Erro ao inserir foto", e);
}
}