Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ComentariosDAO {
- private Connection conexion;
- private Statement stmt = null;
- private void abrirConexion() throws SQLException{
- String dbURI = "jdbc:derby://localhost:1527/Comentarios";
- String username = "fcfm";
- String password = "lsti01";
- conexion = DriverManager.getConnection(dbURI, username, password);
- }
- private void cerrarConexion() throws SQLException {
- conexion.close();
- }
- public void insertar(ComentariosPOJO comentario) throws SQLException {
- try{
- abrirConexion();
- Statement stmt = conexion.createStatement();
- String sql = "insert into COMENTARIOS values ('Ana', 'nunca es tarde')";
- stmt.executeUpdate(sql);
- }finally{
- try{
- if(stmt !=null)
- conexion.close();
- }catch(SQLException se){
- }
- try{
- if(conexion!=null)
- conexion.close();
- }catch(SQLException se){
- se.printStackTrace();
- }
- }
- cerrarConexion();
- }
- public List<ComentariosPOJO> buscar(ComentariosPOJO resultados) throws SQLException{
- ResultSet mensajes;
- List<ComentariosPOJO> beans = new ArrayList<ComentariosPOJO>();
- try{
- abrirConexion();
- String sql = "select * from COMENTARIOS where NOMBRE = 'Ana' and COMENTARIO like '%tarde%'";
- Statement stmt = conexion.createStatement();
- mensajes = stmt.executeQuery(sql);
- }finally{
- try{
- if(stmt !=null)
- conexion.close();
- }catch(SQLException se){
- }
- try{
- if(conexion!=null)
- conexion.close();
- }catch(SQLException se){
- se.printStackTrace();
- }
- }
- while (mensajes.next()) {
- String nombre = mensajes.getString ("NOMBRE");
- String comentario = mensajes.getString ("COMENTARIO");
- ComentariosPOJO comment = new ComentariosPOJO(nombre,comentario);
- beans.add(comment);
- }
- cerrarConexion();
- return beans;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement