Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Model; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; /** * * @author Lucas */ public class Emprestimo { private int ra; private String Livros, dataR, dataD; public int getRa() { return ra; } public void setRa(int ra) { this.ra = ra; } public String getLivros() { return Livros; } public void setLivros(String Livros) { this.Livros = Livros; } public String getDataR() { return dataR; } public void setDataR(String dataR) { this.dataR = dataR; } public String getDataD() { return dataD; } public void setDataD(String dataD) { this.dataD = dataD; } Connection con; public void cadastrar() { con = new Conexao().conectar(); try { Statement stm = con.createStatement(); stm.execute("insert into emprestimo(ra,livros,dataRealizacao,dataDevolucao) values " + "(" + this.ra + ",'" + this.Livros + "','" + this.dataR + "','" + this.dataD + "')"); JOptionPane.showMessageDialog(null, "Cadastrado com Sucesso", "feito", JOptionPane.INFORMATION_MESSAGE); new Som().tocar("sound//CS.mp3"); resultado(); } catch (SQLException ex) { Logger.getLogger(Emprestimo.class.getName()).log(Level.SEVERE, null, ex); } } public void alterar(int id) { con = new Conexao().conectar(); try { Statement stm = con.createStatement(); stm.execute("update emprestimo set ra=" + this.ra + ",livros='" + this.Livros + "'," + "dataRealizacao='" + this.dataR + "',dataDevolucao='" + this.dataD + "' where id=" + id); JOptionPane.showMessageDialog(null, "Alterado com Sucesso", "feito", JOptionPane.INFORMATION_MESSAGE); new Som().tocar("sound//AS.mp3"); resultado(); } catch (SQLException ex) { Logger.getLogger(Emprestimo.class.getName()).log(Level.SEVERE, null, ex); } } public void deletar(int id) { con = new Conexao().conectar(); try { Statement stm = con.createStatement(); stm.execute("delete from emprestimo where id=" + id); JOptionPane.showMessageDialog(null, "Excluido com sucesso", "Excluido", JOptionPane.WARNING_MESSAGE); new Som().tocar("sound//ES.mp3"); } catch (SQLException ex) { Logger.getLogger(Emprestimo.class.getName()).log(Level.SEVERE, null, ex); } } public boolean consultar(int id) { con = new Conexao().conectar(); int u = 0; try { Statement stm = con.createStatement(); ResultSet rs = stm.executeQuery("select * from emprestimo where id=" + id); while (rs.next()) { u++; this.ra = rs.getInt("ra"); this.dataD = rs.getString("dataDevolucao"); this.dataR = rs.getString("dataRealizacao"); this.Livros = rs.getString("livros"); } } catch (SQLException ex) { Logger.getLogger(Emprestimo.class.getName()).log(Level.SEVERE, null, ex); } if (u != 0) { return true; } else { return false; } } private void resultado() { Document documento = new Document(); Livro l; try { PdfWriter.getInstance(documento, new FileOutputStream("Emprestimo" + id() + ".pdf")); } catch (DocumentException ex) { Logger.getLogger(Emprestimo.class.getName()).log(Level.SEVERE, null, ex); } catch (FileNotFoundException ex) { Logger.getLogger(Emprestimo.class.getName()).log(Level.SEVERE, null, ex); } documento.open(); try { Paragraph pa = new Paragraph(); Font fonte = new Font(Font.FontFamily.TIMES_ROMAN, 28, Font.BOLD, new BaseColor(0, 0, 225)); pa.setFont(fonte); pa.setAlignment(Element.ALIGN_CENTER); pa.setSpacingAfter(50); pa.add("Emprestimo"); documento.add(pa); fonte = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 0, 0)); pa = new Paragraph(); pa.setFont(fonte); pa.setAlignment(Element.ALIGN_LEFT); pa.add("id: " + Integer.toString(id())); documento.add(pa); pa = new Paragraph(); pa.setFont(fonte); pa.setAlignment(Element.ALIGN_LEFT); pa.add("ra: " + Integer.toString(this.getRa())); documento.add(pa); String lin = ""; String[] h = this.Livros.split("/"); for (int c = 0; c < h.length; c++) { l = new Livro(); l.consultar(Integer.parseInt(h[c])); lin = lin + l.getNome() + "\n"; } pa = new Paragraph(); pa.setFont(fonte); pa.setAlignment(Element.ALIGN_LEFT); pa.add("Livros: " + lin); documento.add(pa); pa = new Paragraph(); pa.setFont(fonte); pa.setAlignment(Element.ALIGN_LEFT); pa.add("Data de Realizao: " + this.dataR); documento.add(pa); pa = new Paragraph(); pa.setFont(fonte); pa.setAlignment(Element.ALIGN_LEFT); pa.add("Data de Devoluo: " + this.dataD); documento.add(pa); } catch (DocumentException ex) { Logger.getLogger(Emprestimo.class.getName()).log(Level.SEVERE, null, ex); } documento.close(); } private int id() { int u = 0; con = new Conexao().conectar(); Statement stm; try { stm = con.createStatement(); ResultSet rs = stm.executeQuery( "select id from emprestimo where ra=" + this.ra + " and " + "livros='" + this.Livros + "' and dataRealizacao='" + this.dataR + "' and dataDevolucao='" + this.dataD + "'"); while (rs.next()) { u = rs.getInt("id"); } } catch (SQLException ex) { Logger.getLogger(Emprestimo.class.getName()).log(Level.SEVERE, null, ex); } return u; } }