Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------------conexion------------------------
- package com.conexion;
- import java.sql.*;
- /**
- * Nombre de la clase: Conexion
- * @author Isaac
- * Copyright Isaac
- * Fecha: 09/08/2017
- *
- */
- public class Conexion {
- private Connection con;
- public Connection getCon() {
- return con;
- }
- public void setCon(Connection con) {
- this.con = con;
- }
- public void conectar() throws Exception
- {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bdBiblioteca?user=root&password=");
- } catch (Exception e) {
- throw e;
- }
- }
- public void desconectar() throws Exception
- {
- try
- {
- if(con!=null)
- {
- if(con.isClosed()==false)
- {
- con.close();
- }
- }
- } catch (Exception e) {
- }
- }
- }
- ------------------------daobiblioteca------------------------
- package com.dao;
- import com.conexion.Conexion;
- import com.modelo.Biblioteca;
- import java.util.*;
- import java.sql.*;
- /**
- * Nombre de la clase: DaoBiblioteca
- * @author Isaac
- * Copyright Isaac
- * Fecha: 09/08/2017
- *
- */
- public class DaoBiblioteca extends Conexion {
- public void insertarBiblioteca (Biblioteca bi) throws Exception
- {
- try
- {
- this.conectar();
- String sql="insert into biblioteca(titulo, autor,editorial,precio) values(?,?,?,?)";
- PreparedStatement pre=this.getCon().prepareStatement(sql);
- pre.setString(1,bi.getTitulo());
- pre.setString(2, bi.getAutor());
- pre.setString(3, bi.getEditorial());
- pre.setDouble(4, bi.getPrecio());
- pre.executeUpdate();
- } catch (Exception e) {
- throw e;
- }
- finally
- {
- this.desconectar();
- }
- }
- public List mostrarBiblioteca() throws Exception
- {
- List listaBiblioteca=new ArrayList();
- ResultSet res;
- try
- {
- this.conectar();
- String sql="select * from biblioteca";
- PreparedStatement pre=this.getCon().prepareCall(sql);
- res=pre.executeQuery();
- while(res.next())
- {
- Biblioteca bi=new Biblioteca();
- bi.setId(res.getInt("idLibro"));
- bi.setTitulo(res.getString("titulo"));
- bi.setAutor(res.getString("autor"));
- bi.setEditorial(res.getString("editorial"));
- bi.setPrecio(res.getDouble("precio"));
- listaBiblioteca.add(bi);
- }
- } catch (Exception e) {
- throw e;
- }
- finally
- {
- this.desconectar();
- }
- return listaBiblioteca;
- }
- public void modificarBiblioteca(Biblioteca bi) throws Exception
- {
- try {
- this.conectar();
- String sql="update biblioteca set titulo=?, autor=?, editorial=?,precio=? where idLibro=?";
- PreparedStatement pre=this.getCon().prepareStatement(sql);
- pre.setString(1, bi.getTitulo());
- pre.setString(2, bi.getAutor());
- pre.setString(3, bi.getEditorial());
- pre.setDouble(4, bi.getPrecio());
- pre.setInt(5, bi.getId());
- pre.executeUpdate();
- } catch (Exception e) {
- throw e;
- }
- finally{
- this.desconectar();
- }
- }
- public void eliminarBiblioteca (Biblioteca bi) throws Exception
- {
- try
- {
- this.conectar();
- String sql="delete from biblioteca where idLibro=?";
- PreparedStatement pre= this.getCon().prepareStatement(sql);
- pre.setInt(1, bi.getId());
- pre.executeUpdate();
- } catch (Exception e) {
- throw e;
- }
- finally{
- this.desconectar();
- }
- }
- }
- ------------------------------------biblioteca-------------------
- package com.modelo;
- /**
- * Nombre de la clase: Biblioteca
- * @author Isaac
- * Copyright Isaac
- * Fecha: 09/08/2017
- *
- */
- public class Biblioteca {
- private int id;
- private String titulo;
- private String autor;
- private String editorial;
- private double precio;
- public Biblioteca() {
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getTitulo() {
- return titulo;
- }
- public void setTitulo(String titulo) {
- this.titulo = titulo;
- }
- public String getAutor() {
- return autor;
- }
- public void setAutor(String autor) {
- this.autor = autor;
- }
- public String getEditorial() {
- return editorial;
- }
- public void setEditorial(String editorial) {
- this.editorial = editorial;
- }
- public double getPrecio() {
- return precio;
- }
- public void setPrecio(double precio) {
- this.precio = precio;
- }
- }
- --------------------------------formulario----------------------
- package com.vista;
- import com.dao.DaoBiblioteca;
- import com.modelo.Biblioteca;
- import java.text.*;
- import java.util.*;
- import javax.swing.JOptionPane;
- import javax.swing.table.DefaultTableModel;
- /**
- * Nombre de la clase: FrmBiblioteca
- * @author Isaac
- * Copyright Isaac
- * Fecha: 09/08/2017
- *
- */
- public class FrmBiblioteca extends javax.swing.JFrame {
- public FrmBiblioteca() {
- initComponents();
- tablaE();
- }
- Biblioteca bi=new Biblioteca();
- DaoBiblioteca daob=new DaoBiblioteca();
- public void tablaE(){
- String [] columnas={"Id","Titulo","Autor","Editorial","Precio"};
- Object[] obj=new Object[5];
- DefaultTableModel tabla=new DefaultTableModel(null, columnas);
- List ls;
- DecimalFormat df=new DecimalFormat("#,###.00");
- try
- {
- ls=daob.mostrarBiblioteca();
- for(int i=0;i<ls.size();i++)
- {
- bi=(Biblioteca)ls.get(i);
- obj[0]=bi.getId();
- obj[1]=bi.getTitulo();
- obj[2]=bi.getAutor();
- obj[3]=bi.getEditorial();
- obj[4]=df.format(bi.getPrecio());
- tabla.addRow(obj);
- }
- ls=daob.mostrarBiblioteca();
- this.jTbBiblioteca.setModel(tabla);
- }
- catch (Exception e)
- {
- JOptionPane.showMessageDialog(this, "Error al mostrar datos"+e.toString());
- }
- }
- public void insertar() throws Exception
- {
- bi.setId(ICONIFIED);
- bi.setTitulo(this.jTxtTitulo.getText());
- bi.setAutor(this.jTxtAutor.getText());
- bi.setEditorial(this.jTxtEditorial.getText());
- bi.setPrecio(Double.parseDouble(this.jTxtPrecio.getText()));
- daob.insertarBiblioteca(bi);
- JOptionPane.showMessageDialog(null, "Datos insertados correctamente");
- daob.mostrarBiblioteca();
- }
- public void limpiar()
- {
- this.jtxtId.setText("");
- this.jTxtAutor.setText("");
- this.jTxtEditorial.setText("");
- this.jTxtPrecio.setText("");
- this.jTxtTitulo.setText("");
- }
- public void modificar()
- {
- try
- {
- bi.setId(Integer.parseInt(this.jtxtId.getText()));
- bi.setTitulo(this.jTxtTitulo.getText());
- bi.setAutor(this.jTxtAutor.getText());
- bi.setEditorial(this.jTxtEditorial.getText());
- bi.setPrecio(Double.parseDouble(this.jTxtPrecio.getText()));
- int SiONo=JOptionPane.showConfirmDialog(this, "Desea modificar el libro", "Modificar Libro",JOptionPane.YES_NO_OPTION);
- if(SiONo==0)
- {
- daob.modificarBiblioteca(bi);
- JOptionPane.showMessageDialog(rootPane, "Libro modificado correctamente","Confirmacion", JOptionPane.INFORMATION_MESSAGE);
- tablaE();
- limpiar();
- }
- else
- {
- limpiar();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void eliminar()
- {
- try
- {
- bi.setId(Integer.parseInt(this.jtxtId.getText()));
- int SiONo=JOptionPane.showConfirmDialog(this, "Desea eliminar el libro", "Confirmacion", JOptionPane.YES_NO_OPTION);
- if(SiONo==0)
- {
- daob.eliminarBiblioteca(bi);
- JOptionPane.showMessageDialog(rootPane, "Eliminado con exito","Confirmacion",JOptionPane.INFORMATION_MESSAGE);
- tablaE();
- limpiar();
- }
- else
- {
- limpiar();
- }
- } catch (Exception e) {
- JOptionPane.showMessageDialog(rootPane, e.toString(),"Error",JOptionPane.ERROR_MESSAGE);
- }
- }
- public void llenaTabla()
- {
- int fila=this.jTbBiblioteca.getSelectedRow();
- this.jtxtId.setText(String.valueOf(this.jTbBiblioteca.getValueAt(fila, 0)));
- this.jTxtTitulo.setText(String.valueOf(this.jTbBiblioteca.getValueAt(fila, 1)));
- this.jTxtAutor.setText(String.valueOf(this.jTbBiblioteca.getValueAt(fila, 2)));
- this.jTxtEditorial.setText(String.valueOf(this.jTbBiblioteca.getValueAt(fila, 3)));
- this.jTxtPrecio.setText(String.valueOf(this.jTbBiblioteca.getValueAt(fila, 4)));
- }
- private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
- try {
- insertar();
- } catch (Exception e) {
- e.printStackTrace();
- }
- tablaE();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement