Advertisement
Guest User

Untitled

a guest
Oct 16th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. public class ComentariosDAO {
  2. private Connection conexion;
  3. private Statement stmt = null;
  4.  
  5. private void abrirConexion() throws SQLException{
  6. String dbURI = "jdbc:derby://localhost:1527/Comentarios";
  7. String username = "fcfm";
  8. String password = "lsti01";
  9. conexion = DriverManager.getConnection(dbURI, username, password);
  10. }
  11.  
  12. private void cerrarConexion() throws SQLException {
  13. conexion.close();
  14. }
  15.  
  16. public void insertar(ComentariosPOJO comentario) throws SQLException {
  17. try{
  18. abrirConexion();
  19. Statement stmt = conexion.createStatement();
  20. String sql = "insert into COMENTARIOS values ('Ana', 'nunca es tarde')";
  21. stmt.executeUpdate(sql);
  22. }finally{
  23. try{
  24. if(stmt !=null)
  25. conexion.close();
  26. }catch(SQLException se){
  27. }
  28. try{
  29. if(conexion!=null)
  30. conexion.close();
  31. }catch(SQLException se){
  32. se.printStackTrace();
  33. }
  34. }
  35. cerrarConexion();
  36. }
  37.  
  38. public List<ComentariosPOJO> buscar(ComentariosPOJO resultados) throws SQLException{
  39. ResultSet mensajes;
  40. List<ComentariosPOJO> beans = new ArrayList<ComentariosPOJO>();
  41. try{
  42. abrirConexion();
  43. String sql = "select * from COMENTARIOS where NOMBRE = 'Ana' and COMENTARIO like '%tarde%'";
  44. Statement stmt = conexion.createStatement();
  45. mensajes = stmt.executeQuery(sql);
  46. }finally{
  47. try{
  48. if(stmt !=null)
  49. conexion.close();
  50. }catch(SQLException se){
  51. }
  52. try{
  53. if(conexion!=null)
  54. conexion.close();
  55. }catch(SQLException se){
  56. se.printStackTrace();
  57. }
  58. }
  59. while (mensajes.next()) {
  60. String nombre = mensajes.getString ("NOMBRE");
  61. String comentario = mensajes.getString ("COMENTARIO");
  62.  
  63. ComentariosPOJO comment = new ComentariosPOJO(nombre,comentario);
  64. beans.add(comment);
  65. }
  66. cerrarConexion();
  67. return beans;
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement