Advertisement
Guest User

poolConnection

a guest
Sep 15th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.30 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package programa_oficial;
  7.  
  8. import com.mchange.v2.c3p0.ComboPooledDataSource;
  9. import java.beans.PropertyVetoException;
  10. import java.io.FileInputStream;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.OutputStream;
  14. import java.sql.Connection;
  15. import java.sql.PreparedStatement;
  16. import java.sql.ResultSet;
  17. import java.sql.SQLException;
  18. import java.sql.Statement;
  19. import java.util.Properties;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22.  
  23. /**
  24.  *
  25.  * @author Luis Felipe
  26.  */
  27. public class DataSource {
  28.        
  29.       static Connection con;
  30.     private static PreparedStatement pstmt;
  31.     private static Statement stmt;
  32.     public static ResultSet rs;
  33.     private static final String DRIVER = "com.mysql.jdbc.Driver";
  34.      private static final String URL = "jdbc:mysql://sistema-fiado.mysql.uhserver.com/sistema_fiado";
  35.     private static final String user = "arthurdm01";
  36.     private static final String pass = "Passwd.5";
  37.     private static DataSource dataSource;
  38.     private static ComboPooledDataSource cpds;
  39.    
  40.     static{
  41.         try{
  42.            
  43.             cpds.setDriverClass(DRIVER);
  44.             cpds.setJdbcUrl(URL);
  45.             cpds.setUser(user);
  46.             cpds.setPassword(pass);
  47.            
  48.             cpds.setMinPoolSize(100);
  49.             cpds.setMaxPoolSize(1000);
  50.             cpds.setAcquireIncrement(5);
  51.             cpds.setMaxStatements(180);
  52.            
  53.         } catch (PropertyVetoException ex) {
  54.               Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex);
  55.           }
  56.     }
  57.    public static DataSource getInstance()
  58.        throws IOException,SQLException,PropertyVetoException{
  59.        if(dataSource == null){
  60.            dataSource = new DataSource();
  61.            return dataSource;
  62.        }else{
  63.            return dataSource;
  64.        }
  65.              
  66.    }
  67.         public static Connection getConnection() throws SQLException{
  68.            
  69.             return cpds.getConnection();
  70.         }
  71.         }
  72.  
  73.    
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92. /*
  93.  * To change this license header, choose License Headers in Project Properties.
  94.  * To change this template file, choose Tools | Templates
  95.  * and open the template in the editor.
  96.  */
  97. package programa_oficial;
  98.  
  99. import Interface.Movimentacao;
  100. import Interface.Vender_produto;
  101. import com.mchange.v2.c3p0.ComboPooledDataSource;
  102. import java.beans.PropertyVetoException;
  103. import java.io.IOException;
  104. import java.sql.Connection;
  105. import java.sql.Date;
  106. import java.sql.PreparedStatement;
  107. import java.sql.SQLException;
  108. import java.text.SimpleDateFormat;
  109. import java.util.ArrayList;
  110. import java.util.logging.Level;
  111. import java.util.logging.Logger;
  112. import javax.swing.JOptionPane;
  113. import static programa_oficial.Banco.con;
  114.  
  115. /**
  116.  *
  117.  * @author Luis Felipe
  118.  */
  119. public class Controle {
  120.    
  121.     Banco conex = new Banco();
  122.    
  123.    
  124.     public void salvar_Cliente(Cliente cc) throws IOException, PropertyVetoException {
  125.         try {
  126.          
  127.          
  128.            
  129.            
  130.            PreparedStatement pst = con.prepareStatement("insert into cliente (cpf,email,endereco,id_usuario,nome,telefone) values (?,?,?,?,?,?)");
  131.            pst.setString(1, cc.getCpf());
  132.          
  133.            pst.setString(2, cc.getEmail());
  134.            pst.setString(3, cc.getEndereco());
  135.             pst.setInt(4, cc.getId_usuario());
  136.            pst.setString(5, cc.getNome());
  137.            pst.setString(6, cc.getTelefone());
  138.          
  139.            pst.execute();
  140.             JOptionPane.showMessageDialog(null, "Cadastro Realizado");
  141.         } catch (SQLException ex) {
  142.             Logger.getLogger(Controle.class.getName()).log(Level.SEVERE, null, ex);
  143.             JOptionPane.showMessageDialog(null, ex);
  144.         }
  145.        
  146.     }
  147.    
  148.    
  149.     public void salvar_Usuario(Usuario usu) {
  150.         try {
  151.            conex.conectar();
  152.            PreparedStatement pst = conex.con.prepareStatement("insert into usuario (nome,cnpj,email,telefone) values (?,?,?,?)");
  153.            pst.setString(1, usu.getNome());
  154.            pst.setString(2, usu.getCnpj());
  155.            pst.setString(3, usu.getEmail());
  156.            pst.setString(4, usu.getTelefone());
  157.          
  158.            pst.execute();
  159.             JOptionPane.showMessageDialog(null, "Cadastro Realizado");
  160.         } catch (SQLException ex) {
  161.             Logger.getLogger(Controle.class.getName()).log(Level.SEVERE, null, ex);
  162.             JOptionPane.showMessageDialog(null, ex);
  163.         }
  164.        
  165.     }
  166.    
  167.      public void salvar_Funcionario(Funcionario FF) {
  168.         try {
  169.            conex.conectar();
  170.            PreparedStatement pst = conex.con.prepareStatement("insert into funcionario (nome,login,senha) values (?,?,?)");
  171.            pst.setString(1, FF.getNome());
  172.            pst.setString(2, FF.getLogin());
  173.            pst.setString(3, FF.getSenha());
  174.          
  175.          
  176.            pst.execute();
  177.             JOptionPane.showMessageDialog(null, "Cadastro Realizado");
  178.         } catch (SQLException ex) {
  179.             Logger.getLogger(Controle.class.getName()).log(Level.SEVERE, null, ex);
  180.             JOptionPane.showMessageDialog(null, ex);
  181.         }
  182.        
  183.     }
  184.      
  185.      public void Editar_Clientee(Editar cliente) {
  186.         try {
  187.            conex.conectar();
  188.            PreparedStatement pst = conex.con.prepareStatement("update cliente set nome=?,cpf=?,endereco=?,telefone=? where id ="+ cliente.getId());
  189.            pst.setString(1, cliente.getNome());
  190.            pst.setString(2, cliente.getCpf());
  191.            pst.setString(3, cliente.getEndereco());
  192.            pst.setString(4, cliente.getTelefone());
  193.            
  194.       //     pst.setInt(5, cliente.getId_usuario());
  195.          
  196.            pst.execute();
  197.             JOptionPane.showMessageDialog(null, "Alteração concluida");
  198.         } catch (SQLException ex) {
  199.             Logger.getLogger(Controle.class.getName()).log(Level.SEVERE, null, ex);
  200.             JOptionPane.showMessageDialog(null, ex);
  201.         }
  202.        
  203.     }
  204.      
  205.      public void Deletar_Cliente(Cliente cliente) {
  206.         try {
  207.            conex.conectar();
  208.            PreparedStatement pst = conex.con.prepareStatement("delete from cliente where id ="+ cliente.getId());
  209.          
  210.            
  211.       //     pst.setInt(5, cliente.getId_usuario());
  212.          
  213.            pst.execute();
  214.             JOptionPane.showMessageDialog(null, "Cliente excluido");
  215.         } catch (SQLException ex) {
  216.             Logger.getLogger(Controle.class.getName()).log(Level.SEVERE, null, ex);
  217.             JOptionPane.showMessageDialog(null, ex);
  218.         }
  219.        
  220.     }
  221.      
  222.      
  223.     public void Vender_Produto(Venda vender) throws SQLException{
  224.         conex.conectar();
  225.         PreparedStatement pst = conex.con.prepareStatement("insert into venda (data,descricao,em_aberto,id_cliente,id_vendedor,parcelas,total_recebido,valor) values (?,?,?,?,?,?,?,?)");
  226.         java.sql.Date datasql = new java.sql.Date(vender.getData().getTime());
  227.        
  228.         SimpleDateFormat formata = new SimpleDateFormat("dd/MM/yyyy");
  229.        
  230.        
  231.        
  232.         pst.setDate(1, (Date) datasql);
  233.         pst.setString(2, vender.getDescricao());
  234.         pst.setBoolean(3, vender.isEm_aberto());
  235.         pst.setInt(4, vender.getId_cliente());
  236.         pst.setInt(5, vender.getId_funcionario());
  237.         pst.setFloat(6, vender.getParcelas());
  238.         pst.setFloat(7, vender.getEntrada());
  239.         pst.setFloat(8, vender.getValor());
  240.         pst.execute();
  241.      
  242.             JOptionPane.showMessageDialog(null, "Venda realizada");
  243.        
  244.        
  245.     }
  246.    
  247.     public void Cadastrar_Funcionario(Funcionario funcionario) throws SQLException{
  248.         Banco.conectar();
  249.         PreparedStatement pst = conex.con.prepareStatement("insert into funcionario (id_usuario,login,nome,senha) values (?,?,?,?)");
  250.        pst.setInt(1,funcionario.getId_usuario() );
  251.        pst.setString(2, funcionario.getLogin());
  252.        pst.setString(3, funcionario.getNome());
  253.        pst.setString(4, funcionario.getSenha());
  254.        pst.execute();
  255.        JOptionPane.showConfirmDialog(null, "Funcionario Cadastrado");
  256.        Banco.desconectar();
  257.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement