Advertisement
Guest User

Untitled

a guest
Mar 11th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. public void setCodigo(int lib_codigo){
  2. this.lib_codigo = lib_codigo;
  3. }
  4.  
  5. public void setDescripcion(String lib_descripcion){
  6. this.lib_descripcion = lib_descripcion;
  7. }
  8.  
  9. public void setCantidad(int lib_cantidad){
  10. this.lib_cantidad = lib_cantidad;
  11. }
  12.  
  13. public void setEdicion(int lib_edicion){
  14. this.lib_edicion = lib_edicion;
  15. }
  16.  
  17. public void setObservacion(String lib_observacion){
  18. this.lib_observacion = lib_observacion;
  19. }
  20.  
  21. public int getCodigo(){ return lib_codigo; }
  22. public String getDescripcion(){ return lib_descripcion; }
  23. public int getCantidad() { return lib_cantidad; }
  24. public int getEdicion() { return lib_edicion; }
  25. public String getObservacion() { return lib_observacion; }
  26. }
  27.  
  28. /**
  29. * add Libro
  30. */
  31. public Boolean altaLibro(String lib_descripcion, Integer lib_cantidad, Integer lib_edicion, String lib_observacion){
  32. // sql Statement
  33. String sql = "INSERT INTO libros (lib_descripcion, lib_cantidad, lib_edicion, lib_observacion) VALUES (?, ?, ?, ?)";
  34. DbConnection conn = new DbConnection();
  35. try{
  36. // prepared statement
  37. PreparedStatement s = conn.getConnection().prepareStatement(sql);
  38. s.setString(1, lib_descripcion);
  39. s.setInt(2, lib_cantidad);
  40. s.setInt(3, lib_edicion);
  41. s.setString(4, lib_observacion);
  42. s.execute();
  43. return true;
  44. } catch(Exception ex) {
  45. ex.printStackTrace();
  46. }
  47. return false;
  48. }
  49.  
  50. /**
  51. * fill Table
  52. */
  53. public List<LibroVO> listLibros(){
  54. DbConnection conn = new DbConnection();
  55. String sql = "SELECT * FROM libros";
  56. try{
  57. Statement s = conn.getConnection().createStatement();
  58. ResultSet rs = s.executeQuery(sql);
  59. List<LibroVO> list = new ArrayList<LibroVO>();
  60. while (rs.next()){
  61. LibroVO l = new LibroVO();
  62. l.setCodigo(rs.getInt(1));
  63. l.setDescripcion(rs.getString(2));
  64. l.setCantidad(rs.getInt(3));
  65. l.setEdicion(rs.getInt(4));
  66. l.setObservacion(rs.getString(5));
  67. list.add(l);
  68. }
  69. return list;
  70. } catch(Exception e){
  71. e.printStackTrace();
  72. return null;
  73. }
  74. }
  75.  
  76. /**
  77. * update Libro
  78. */
  79. public Boolean altaLibro(Integer lib_codigo, String lib_descripcion, Integer lib_cantidad, Integer lib_edicion, String lib_observacion) {
  80. String sql = "UPDATE libros SET lib_descripcion = ?, lib_cantidad = ?, lib_edicion = ?, lib_observacion = ? WHERE lib_codigo = ? ";
  81. DbConnection conn = new DbConnection();
  82. try {
  83. PreparedStatement s = conn.getConnection().prepareStatement(sql);
  84. s.setString(1, lib_descripcion);
  85. s.setInt(2, lib_cantidad);
  86. s.setInt(3, lib_edicion);
  87. s.setString(4, lib_observacion);
  88. s.setInt(5, lib_codigo);
  89. s.executeUpdate();
  90. return true;
  91. } catch (SQLException ex) {
  92. ex.printStackTrace();
  93. return false;
  94. }
  95. }
  96.  
  97. /**
  98. * delete Libro
  99. */
  100. public Boolean bajaLibro(Integer lib_codigo){
  101. String sql = "DELETE FROM libros WHERE lib_codigo = ?";
  102. DbConnection conn = new DbConnection();
  103. try{
  104. PreparedStatement s = conn.getConnection().prepareStatement(sql);
  105. s.setInt(1, lib_codigo);
  106. s.execute();
  107. return true;
  108. } catch(SQLException ex){
  109. ex.printStackTrace();
  110. return false;
  111. }
  112. }
  113. }
  114.  
  115. private String colName[] = {"Código", "Descripción", "Cantidad",
  116. "Edición", "Observación"};
  117.  
  118. private List<LibroVO> list = null;
  119.  
  120. public TableModel(List<LibroVO> list){
  121. this.list = list;
  122. }
  123.  
  124. public String getColumnName(int col){
  125. return colName[col];
  126. }
  127.  
  128. public int getColumnCount(){
  129. return colName.length;
  130. }
  131.  
  132. public int getRowCount(){
  133. return list.size();
  134. }
  135.  
  136.  
  137. @Override
  138. public Object getValueAt(int rowIndex, int columnIndex){
  139. if(columnIndex == 0){
  140. return list.get(rowIndex).getCodigo();
  141. } else if(columnIndex == 1){
  142. return list.get(rowIndex).getDescripcion();
  143. } else if(columnIndex == 2){
  144. return list.get(rowIndex).getCantidad();
  145. } else if(columnIndex == 3){
  146. return list.get(rowIndex).getEdicion();
  147. } else {
  148. return list.get(rowIndex).getObservacion();
  149. }
  150. }
  151.  
  152.  
  153. public boolean isCellEditable(int rowIndex, int colIndex){
  154. return false;
  155. }
  156. }
  157.  
  158. public class DbConnection
  159. {
  160. // variables
  161. static String db = "biblio";
  162. static String login = "root";
  163. static String password = "masterkey";
  164. static String url = "jdbc:mysql://localhost/"+db;
  165.  
  166. Connection conn = null;
  167.  
  168. /**
  169. * Constructor de la clase
  170. */
  171. public DbConnection()
  172. {
  173. try{
  174. // Obtengo el driver para mysql
  175. Class.forName("com.mysql.jdbc.Driver");
  176. conn = DriverManager.getConnection(url, login, password);
  177. } catch(SQLException e){
  178. System.out.println(e);
  179. } catch(ClassNotFoundException e){
  180. System.out.println(e);
  181. } catch(Exception e){
  182. System.out.println(e);
  183. }
  184. }
  185.  
  186.  
  187.  
  188. /**
  189. * Método para conectarse
  190. */
  191. public Connection getConnection()
  192. {
  193. return conn;
  194. }
  195.  
  196. /**
  197. * Método para desconectarse
  198. */
  199. public void desconectar(){
  200. conn = null;
  201. }
  202. }
  203.  
  204.  
  205. import java.sql.*;
  206. import java.util.*;
  207. import javax.swing.table.DefaultTableModel;
  208. import javax.swing.*;
  209. public class LibroTable extends JFrame
  210. {
  211. // instance variables - replace the example below with your own
  212.  
  213. /**
  214. * Constructor for objects of class LibroTable
  215. */
  216. public LibroTable()
  217. {
  218. DbConnection conn = new DbConnection();
  219. LibroDAO l = new LibroDAO();
  220. try{
  221. TableModel tm = new TableModel(l.listLibros());
  222. tableLibro.setModel(tm);
  223. } catch(Exception e){
  224. e.printStackTrace();
  225. }
  226. }
  227.  
  228. public static void main(String args[]){
  229. java.awt.EventQueue.invokeLater(new Runnable(){
  230. public void run(){
  231. new LibroTable().setVisible(true);
  232. }
  233. });
  234. }
  235.  
  236. // Variables
  237. private JScrollPane jScrollPane1;
  238. private JTable tableLibro;
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement