Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Funcionario:
- /*
- * 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 projetobanco;
- /**
- *
- * @author francisleide
- */
- public class Funcionario {
- private String nome;
- private int id;
- private String matricula;
- private int idade;
- public Funcionario(){
- }
- public String getNome() {
- return nome;
- }
- public void setNome(String nome) {
- this.nome = nome;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getMatricula() {
- return matricula;
- }
- public void setMatricula(String matricula) {
- this.matricula = matricula;
- }
- public int getIdade() {
- return idade;
- }
- public void setIdade(int idade) {
- this.idade = idade;
- }
- public Funcionario(String nome, String matricula, int idade) {
- this.nome = nome;
- this.matricula = matricula;
- this.idade = idade;
- }
- }
- Banco:
- /*
- * 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 projetobanco;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.util.ArrayList;
- /**
- *
- * @author francisleide
- */
- public class Conexao {
- public Connection conexao = null;
- public Statement declaracao = null;
- public ResultSet resultados = null;
- public void conectar(){
- String servidor = "jdbc:mysql://localhost:3306/empresa";
- String usuario = "root";
- String senha = "";
- String driver = "com.mysql.jdbc.Driver";
- try{
- Class.forName(driver);
- this.conexao = DriverManager.getConnection(servidor, usuario, senha);
- this.declaracao = conexao.createStatement();
- }catch (Exception e){
- System.out.println("Erro: "+e.getMessage());
- }
- }
- public boolean testaConexao(){
- if(this.conexao == null){
- return false;
- }
- return true;
- }
- public void insereFuncionario(Funcionario funcionario){
- String sql = "INSERT INTO FUNCIONARIO (NOME, MATRICULA, IDADE) VALUES ('"
- + funcionario.getNome()+"', '"+funcionario.getMatricula()+
- "', "+funcionario.getIdade()+");";
- try{
- declaracao.executeUpdate(sql);
- }catch(Exception e){
- System.out.println("Erro: "+e.getMessage());
- }
- }
- public ArrayList<Funcionario> listarFuncionarios(){
- ArrayList<Funcionario> funcionarios = new ArrayList<>();
- String sql = "SELECT * FROM FUNCIONARIO";
- try{
- this.resultados=this.declaracao.executeQuery(sql);
- Funcionario funcionario;
- while(resultados.next()==true){
- funcionario = new Funcionario();
- funcionario.setId(resultados.getInt("idfuncionario"));
- funcionario.setIdade(resultados.getInt("idade"));
- funcionario.setNome(resultados.getString("nome"));
- funcionario.setMatricula(resultados.getString("matricula"));
- funcionarios.add(funcionario);
- }
- }catch (Exception e){
- System.out.println("Erro: "+e.getMessage());
- }
- return funcionarios;
- }
- }
- Main:
- -----Classe main------------------------
- package projetobanco;
- import java.util.ArrayList;
- import javax.swing.JOptionPane;
- /**
- *
- * @author francisleide
- */
- public class ProjetoBanco {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Conexao con = new Conexao();
- con.conectar();
- if(con.testaConexao()){
- System.out.println("Conectou !! o/");
- }else{
- System.out.println("Deu ruim!");
- }
- /* String nome=JOptionPane.showInputDialog("Informe o nome");
- String matricula = JOptionPane.showInputDialog("Infome a matricula");
- int idade = Integer.parseInt(JOptionPane.showInputDialog("Informe a idade"));
- Funcionario f1 = new Funcionario(nome, matricula, idade);
- con.insereFuncionario(f1);*/
- ArrayList<Funcionario> funcionarios = con.listarFuncionarios();
- for(int i=0; i<funcionarios.size(); i++){
- System.out.println("Id: "+funcionarios.get(i).getId());
- System.out.println("Nome: "+funcionarios.get(i).getNome());
- System.out.println("Matricula: "+funcionarios.get(i).getMatricula());
- System.out.println("Idade: "+funcionarios.get(i).getIdade());
- }
- int idEditar = Integer.parseInt(JOptionPane.showInputDialog("Informe o id do objeto a ser editado no banco!"));
- Funcionario f1 = new Funcionario();
- f1.setId(idEditar);
- f1.setIdade(Integer.parseInt(JOptionPane.showInputDialog("Informe a nova idade")));
- f1.setNome(JOptionPane.showInputDialog("Informe o novo nome"));
- f1.setMatricula(JOptionPane.showInputDialog("Informe a nova matrícula"));
- con.editaFuncionario(f1);
- /*f1.setId(idExcluir);
- con.excluiFuncionario(f1);*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement