Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void setCodigo(int lib_codigo){
- this.lib_codigo = lib_codigo;
- }
- public void setDescripcion(String lib_descripcion){
- this.lib_descripcion = lib_descripcion;
- }
- public void setCantidad(int lib_cantidad){
- this.lib_cantidad = lib_cantidad;
- }
- public void setEdicion(int lib_edicion){
- this.lib_edicion = lib_edicion;
- }
- public void setObservacion(String lib_observacion){
- this.lib_observacion = lib_observacion;
- }
- public int getCodigo(){ return lib_codigo; }
- public String getDescripcion(){ return lib_descripcion; }
- public int getCantidad() { return lib_cantidad; }
- public int getEdicion() { return lib_edicion; }
- public String getObservacion() { return lib_observacion; }
- }
- /**
- * add Libro
- */
- public Boolean altaLibro(String lib_descripcion, Integer lib_cantidad, Integer lib_edicion, String lib_observacion){
- // sql Statement
- String sql = "INSERT INTO libros (lib_descripcion, lib_cantidad, lib_edicion, lib_observacion) VALUES (?, ?, ?, ?)";
- DbConnection conn = new DbConnection();
- try{
- // prepared statement
- PreparedStatement s = conn.getConnection().prepareStatement(sql);
- s.setString(1, lib_descripcion);
- s.setInt(2, lib_cantidad);
- s.setInt(3, lib_edicion);
- s.setString(4, lib_observacion);
- s.execute();
- return true;
- } catch(Exception ex) {
- ex.printStackTrace();
- }
- return false;
- }
- /**
- * fill Table
- */
- public List<LibroVO> listLibros(){
- DbConnection conn = new DbConnection();
- String sql = "SELECT * FROM libros";
- try{
- Statement s = conn.getConnection().createStatement();
- ResultSet rs = s.executeQuery(sql);
- List<LibroVO> list = new ArrayList<LibroVO>();
- while (rs.next()){
- LibroVO l = new LibroVO();
- l.setCodigo(rs.getInt(1));
- l.setDescripcion(rs.getString(2));
- l.setCantidad(rs.getInt(3));
- l.setEdicion(rs.getInt(4));
- l.setObservacion(rs.getString(5));
- list.add(l);
- }
- return list;
- } catch(Exception e){
- e.printStackTrace();
- return null;
- }
- }
- /**
- * update Libro
- */
- public Boolean altaLibro(Integer lib_codigo, String lib_descripcion, Integer lib_cantidad, Integer lib_edicion, String lib_observacion) {
- String sql = "UPDATE libros SET lib_descripcion = ?, lib_cantidad = ?, lib_edicion = ?, lib_observacion = ? WHERE lib_codigo = ? ";
- DbConnection conn = new DbConnection();
- try {
- PreparedStatement s = conn.getConnection().prepareStatement(sql);
- s.setString(1, lib_descripcion);
- s.setInt(2, lib_cantidad);
- s.setInt(3, lib_edicion);
- s.setString(4, lib_observacion);
- s.setInt(5, lib_codigo);
- s.executeUpdate();
- return true;
- } catch (SQLException ex) {
- ex.printStackTrace();
- return false;
- }
- }
- /**
- * delete Libro
- */
- public Boolean bajaLibro(Integer lib_codigo){
- String sql = "DELETE FROM libros WHERE lib_codigo = ?";
- DbConnection conn = new DbConnection();
- try{
- PreparedStatement s = conn.getConnection().prepareStatement(sql);
- s.setInt(1, lib_codigo);
- s.execute();
- return true;
- } catch(SQLException ex){
- ex.printStackTrace();
- return false;
- }
- }
- }
- private String colName[] = {"Código", "Descripción", "Cantidad",
- "Edición", "Observación"};
- private List<LibroVO> list = null;
- public TableModel(List<LibroVO> list){
- this.list = list;
- }
- public String getColumnName(int col){
- return colName[col];
- }
- public int getColumnCount(){
- return colName.length;
- }
- public int getRowCount(){
- return list.size();
- }
- @Override
- public Object getValueAt(int rowIndex, int columnIndex){
- if(columnIndex == 0){
- return list.get(rowIndex).getCodigo();
- } else if(columnIndex == 1){
- return list.get(rowIndex).getDescripcion();
- } else if(columnIndex == 2){
- return list.get(rowIndex).getCantidad();
- } else if(columnIndex == 3){
- return list.get(rowIndex).getEdicion();
- } else {
- return list.get(rowIndex).getObservacion();
- }
- }
- public boolean isCellEditable(int rowIndex, int colIndex){
- return false;
- }
- }
- public class DbConnection
- {
- // variables
- static String db = "biblio";
- static String login = "root";
- static String password = "masterkey";
- static String url = "jdbc:mysql://localhost/"+db;
- Connection conn = null;
- /**
- * Constructor de la clase
- */
- public DbConnection()
- {
- try{
- // Obtengo el driver para mysql
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection(url, login, password);
- } catch(SQLException e){
- System.out.println(e);
- } catch(ClassNotFoundException e){
- System.out.println(e);
- } catch(Exception e){
- System.out.println(e);
- }
- }
- /**
- * Método para conectarse
- */
- public Connection getConnection()
- {
- return conn;
- }
- /**
- * Método para desconectarse
- */
- public void desconectar(){
- conn = null;
- }
- }
- import java.sql.*;
- import java.util.*;
- import javax.swing.table.DefaultTableModel;
- import javax.swing.*;
- public class LibroTable extends JFrame
- {
- // instance variables - replace the example below with your own
- /**
- * Constructor for objects of class LibroTable
- */
- public LibroTable()
- {
- DbConnection conn = new DbConnection();
- LibroDAO l = new LibroDAO();
- try{
- TableModel tm = new TableModel(l.listLibros());
- tableLibro.setModel(tm);
- } catch(Exception e){
- e.printStackTrace();
- }
- }
- public static void main(String args[]){
- java.awt.EventQueue.invokeLater(new Runnable(){
- public void run(){
- new LibroTable().setVisible(true);
- }
- });
- }
- // Variables
- private JScrollPane jScrollPane1;
- private JTable tableLibro;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement