Advertisement
Guest User

Untitled

a guest
Jun 24th, 2016
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. public class AccesoDatos {
  2. private ResultSet rs = null;
  3. private Connection con = null;
  4. private Statement sentencia = null;
  5.  
  6. private ArrayList<Comuna> listC;
  7. private ArrayList<Libro> listL;
  8. private ArrayList<Pelicula> listP;
  9. private ArrayList<Vendedor> listV;
  10.  
  11. private Comuna com;
  12. private Libro lib;
  13. private Pelicula peli;
  14. private Vendedor vend;
  15.  
  16.  
  17. private void conectar (){
  18. try{
  19. String parametros = "jdbc:mysql://localhost:3306/libros?zeroDateTimeBehavior=convertToNull";
  20. Class.forName("com.mysql.jdbc.Driver");
  21. con = DriverManager.getConnection(parametros,"root","");
  22.  
  23. }catch(SQLException e){
  24. System.out.print("No se ha podido conectar");
  25. }catch(Exception e){
  26. System.out.print("No se ha podido conectar");
  27. }
  28. }
  29.  
  30. private void desconectar (){
  31. try{
  32. con.close();
  33. }catch(SQLException e){
  34. System.out.print("No se ha podido desconectar");
  35. }catch(Exception e){
  36. System.out.print("No se ha podido desconectar");
  37. }
  38. }
  39.  
  40. public ArrayList<Libro> listarLibros(){
  41. try{
  42. conectar();
  43. sentencia = con.createStatement();
  44. String sql = "select * from libro";
  45. sentencia.executeQuery(sql);
  46. listL = new ArrayList();
  47. while(rs.next()){
  48. int codigoL = rs.getInt("cod_lib");
  49. String autorL = rs.getString("aut_lib");
  50. String nombreL = rs.getString("nom_lib");
  51. String estadiL = rs.getString("est_lib");
  52. lib = new Libro(codigoL,autorL,nombreL,estadiL);
  53. listL.add(lib);
  54. }
  55. sentencia.close();
  56. desconectar();
  57. return listL;
  58.  
  59. }catch(SQLException e){
  60. return null;
  61. }catch(Exception e){
  62. return null;
  63. }
  64. }
  65.  
  66. catch(SQLException e){
  67. e.printStackTrace();
  68. System.out.println(e);
  69. return null;
  70. }catch(Exception e){
  71. e.printStackTrace();
  72. System.out.println(e);
  73. return null;
  74. }
  75.  
  76. public class AccesoDatos {
  77. private ResultSet rs = null;
  78. private Connection con = null;
  79. private Statement sentencia = null;
  80.  
  81. private ArrayList<Comuna> listC;
  82. private ArrayList<Libro> listL;
  83. private ArrayList<Pelicula> listP;
  84. private ArrayList<Vendedor> listV;
  85.  
  86. private Comuna com;
  87. private Libro lib;
  88. private Pelicula peli;
  89. private Vendedor vend;
  90.  
  91.  
  92. private void conectar (){
  93. try{
  94. String parametros = "jdbc:mysql://localhost:3306/libros?zeroDateTimeBehavior=convertToNull";
  95. Class.forName("com.mysql.jdbc.Driver");
  96. con = DriverManager.getConnection(parametros,"root","");
  97.  
  98. }catch(SQLException e){
  99. System.err.println("ERROR DE CONEXION A LA BD");
  100. }catch(Exception e){
  101. System.err.println("ERROR DE CONEXION ");
  102. }
  103. }
  104.  
  105. private void desconectar (){
  106. try{
  107. con.close();
  108. }catch(SQLException e){
  109. System.err.println("ERROR DE CONEXION A LA BD");
  110. }catch(Exception e){
  111. System.err.println("ERROR DE CONEXION ");
  112. }
  113. }
  114.  
  115.  
  116. public ArrayList<Libro> listarLibro(){
  117. try{
  118. conectar();
  119. String sql = "select * from libro";
  120. sentencia = con.createStatement();
  121. rs = sentencia.executeQuery(sql);
  122. listL = new ArrayList();
  123. while(rs.next()){
  124. int codigoL = rs.getInt("cod_lib");
  125. String autorL = rs.getString("aut_lib");
  126. String nombreL = rs.getString("nom_lib");
  127. String estadiL = rs.getString("est_lib");
  128.  
  129. lib = new Libro(codigoL,autorL,nombreL,estadiL);
  130. listL.add(lib);
  131.  
  132. }
  133. sentencia.close();
  134. desconectar();
  135. return listL;
  136. }catch(SQLException e){
  137. return null;
  138. }catch(Exception e){
  139. return null;
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement