Classe generica para busca

2 respostas
K

Olá,

possuo a seguinte classe para busca dados no banco de dados,

public static String busca (String Select, int campo_busca) { String tipo = null; try { banco.init (); Connection conn = null; Statement stat = null; ResultSet res = null; res = stat.executeQuery (Select); conn = banco.getConnection (); stat = conn.createStatement (); while(res.next ()) { tipo = res.getString (campo_busca); } } catch (IOException ex) { ex.printStackTrace (); } catch (ClassNotFoundException ex) { ex.printStackTrace (); } catch (SQLException ex) { ex.printStackTrace (); } return tipo; }

Só que nessa classe ele me retorna Strings, teria como eu tornar esse codigo mais generico, como por exemplo, pegar o tipo de dado do banco de dados, se for Integer me retornar um inteiro, se for Text me retornar uma String, e assim por diante…

Att
Guilherme

2 Respostas

P

uso Object dai é so fazer um typecast onde vc usa o retorno…

public static Vector<Object> busca (String Select, int campo_busca) { Vector<Object> tipo = new Vector<Object>(); try { banco.init (); Connection conn = null; Statement stat = null; ResultSet res = null; res = stat.executeQuery (Select); conn = banco.getConnection (); stat = conn.createStatement (); while(res.next ()) { tipo.add( res.getObject (campo_busca) ); } } catch (IOException ex) { ex.printStackTrace (); } catch (ClassNotFoundException ex) { ex.printStackTrace (); } catch (SQLException ex) { ex.printStackTrace (); } return tipo; }

P

o q vc esta tentando fazer ja existe, eh conhecido como GenericDAO, segue abaixo alguns links q falam a respeito

http://www.google.com.br/search?source=ig&hl=pt-BR&q=generic+dao&btnG=Pesquisa+Google&meta=

http://www-128.ibm.com/developerworks/java/library/j-genericdao.html

http://www.portaljava.com/home/modules.php?name=Forums&file=viewtopic&t=36920

espero ter ajudado…

Criado 28 de fevereiro de 2007
Ultima resposta 28 de fev. de 2007
Respostas 2
Participantes 3