Skip to content

Instantly share code, notes, and snippets.

@jreyes
Created January 3, 2012 05:06
Show Gist options
  • Save jreyes/1553584 to your computer and use it in GitHub Desktop.
Save jreyes/1553584 to your computer and use it in GitHub Desktop.
public class ElementoQuimicoDAO
{
// -------------------------- OTHER METHODS --------------------------
public List<ElementoQuimico> encontrarTodos()
{
List<ElementoQuimico> lista = new ArrayList<ElementoQuimico>();
Connection c = null;
String sql = "SELECT * FROM elemento_quimicos ORDER BY numero_atomico";
try
{
c = ConnectionHelper.getConnection();
Statement s = c.createStatement();
ResultSet rs = s.executeQuery( sql );
while ( rs.next() )
{
lista.add( procesarElemento( rs ) );
}
}
catch ( SQLException e )
{
e.printStackTrace();
throw new RuntimeException( e );
}
finally
{
ConnectionHelper.close( c );
}
return lista;
}
public ElementoQuimico encontratPorNumeroAtomico( int numeroAtomico )
{
ElementoQuimico elemento = null;
Connection c = null;
String sql = "SELECT * FROM elemento_quimicos WHERE numero_atomico=?";
try
{
c = ConnectionHelper.getConnection();
PreparedStatement ps = c.prepareStatement( sql );
ps.setInt( 1, numeroAtomico );
ResultSet rs = ps.executeQuery();
if ( rs.next() )
{
elemento = procesarElemento( rs );
}
}
catch ( Exception e )
{
e.printStackTrace();
throw new RuntimeException( e );
}
finally
{
ConnectionHelper.close( c );
}
return elemento;
}
private ElementoQuimico procesarElemento( ResultSet rs )
throws SQLException
{
ElementoQuimico elemento = new ElementoQuimico();
elemento.setNumeroAtomico( rs.getInt( "numero_atomico" ) );
elemento.setNombre( rs.getString( "nombre" ) );
elemento.setSimbolo( rs.getString( "simbolo" ) );
return elemento;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment