BOm dia!
Como faço para preencher o "HashMap" exatamente com aa ordem de leitura.
OBRIGADO DESDE JÁ!
Meu código é este
public HashMap LeColunasTabela(final Object Tabela) {
HashMap hmCamposValores = new HashMap();
Class theClass;
theClass = Tabela.getClass();
//Campos
Field fields[] = theClass.getDeclaredFields();
//
for (int intCampos = 0; intCampos < fields.length; intCampos++) {
hmCamposValores.put(fields[intCampos].getName(), null);
}
//Métodos
Method methods[] = theClass.getDeclaredMethods();
//
for (int intValores = 0; intValores < methods.length; intValores++) {
if(!methods[intValores].getReturnType().equals(Void.class) && methods[intValores].getParameterTypes().length <= 0) {
try {
if (hmCamposValores.containsKey(methods[intValores].getName().substring(3, methods[intValores].getName().length()))) {
hmCamposValores.put(methods[intValores].getName().substring(3, methods[intValores].getName().length()), methods[intValores].invoke(Tabela, new Object[0]));
}
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
} catch (InvocationTargetException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
}
}
}
return hmCamposValores;
}
=======================================
Minha Objeto é este. (Para entener melhor)
/*
* Usuario.java
*
* Created on 16 de Junho de 2007, 21:58
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package tabelas;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;
import tabelas.ManutencaoTabela;
/**
*
* @author Paulo Roberto
*/
public class Usuario extends ManutencaoTabela {
protected String NomeTabela;
private Integer Matricula;
private String Nome;
private String NomeGuerra;
private GregorianCalendar DataCadastramento;
private String Senha;
private GregorianCalendar ValidadeSenha;
/** Creates a new instance of Usuario */
public Usuario() {
NomeTabela = "tb_Usuarios";
}
public String getNomeTabela() {
return NomeTabela;
}
public Integer getMatricula() {
return Matricula;
}
public void setMatricula(Integer Matricula) {
this.Matricula = Matricula;
}
public String getNome() {
return Nome;
}
public void setNome(String Nome) {
this.Nome = Nome;
}
public String getNomeGuerra() {
return NomeGuerra;
}
public void setNomeGuerra(String NomeGuerra) {
this.NomeGuerra = NomeGuerra;
}
public GregorianCalendar getDataCadastramento() {
return DataCadastramento;
}
public void setDataCadastramento(GregorianCalendar DataCadastramento) {
this.DataCadastramento = DataCadastramento;
}
public String getSenha() {
return Senha;
}
public void setSenha(String Senha) {
this.Senha = Senha;
}
public GregorianCalendar getValidadeSenha() {
return ValidadeSenha;
}
public void setValidadeSenha(GregorianCalendar ValidadeSenha) {
this.ValidadeSenha = ValidadeSenha;
}
public Usuario getUsuario() {
Usuario Usr = new Usuario();
return Usr;
}
public void setUsuario(Integer Matricula, Connection Conexao) {
StringBuffer sbSQL = new StringBuffer()
.append("SELECT Usuario.* ")
.append("FROM Tb_Usuarios Usuario ")
.append("WHERE Usuario.Matricula = " + Matricula.toString());
try {
//Prepara transação
PreparedStatement pstmt = Conexao.prepareStatement(sbSQL.toString());
//Atribui valores aos Parâmetros
//stmt.setString(1, "");
//Cria resultado
ResultSet rs = pstmt.executeQuery();
if (rs.isBeforeFirst()) {
while (rs.next()) {
System.out.println(rs.getString("Nome"));
//Atribui valores as campos da classe
this.setMatricula(rs.getInt("Matricula"));
this.setNome(rs.getString("Nome"));
this.setNomeGuerra(rs.getString("NomeGuerra"));
this.setDataCadastramento(new GregorianCalendar(2007, 3, 14));
this.setSenha(rs.getString("Senha"));
this.setValidadeSenha(new GregorianCalendar(2007, 3, 14));
}
}
//Consolida transação
Conexao.commit();
//Fecha conexão
pstmt.close();
//Mostra String SQL
System.out.println(sbSQL);
} catch (SQLException errorSQL) {
JOptionPane.showMessageDialog(null, errorSQL.getMessage(), "Erro SQL", JOptionPane.ERROR_MESSAGE);
errorSQL.printStackTrace();
}
}