Skip to content

Instantly share code, notes, and snippets.

@Led-ds
Created October 10, 2014 12:55
Show Gist options
  • Save Led-ds/cf4d30cd9873c1134c7c to your computer and use it in GitHub Desktop.
Save Led-ds/cf4d30cd9873c1134c7c to your computer and use it in GitHub Desktop.
package br.com.alexsoares;
import javax.swing.*;
import javax.swing.text.DateFormatter;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.awt.event.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class Cadastro extends JFrame implements ActionListener {
//Data_Nascimento,Cpf,Telefone
private JTextField Nome, Sobrenome, Endereco, Cidade;
//private JFormattedTextField Data_Nascimento;
private MaskFormatter Data_Nascimento, Cpf, Telefone = null;
private JFormattedTextField jFormattedTextData = null;
private JFormattedTextField jFormattedTextTele = null;
private JFormattedTextField jFormattedTextCpf = null;
private JLabel lbNome, lbSobrenome, lbCpf, lbData_nasc, lbEnd, lbCity, lbFone;
private JComboBox cmbCidade;
private JButton jbCadastrar, jbConsultar, jbLimpar;
public Pessoa user[] = new Pessoa[10];
Scanner sc = new Scanner(System.in);
private String n1, n2, n3;
int cout = 0;
public Cadastro(){
super("Cadastro de Contatos");
setSize(600,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
//Botões
jbCadastrar = new JButton("Cadastrar");
jbConsultar = new JButton("Consultar");
jbLimpar = new JButton("Limpar");
//Label's
lbNome = new JLabel("Nome:");
lbSobrenome = new JLabel("Sobrenome:");
lbCpf = new JLabel("CPF:");
lbData_nasc = new JLabel("Data de Nascimento:");
lbEnd = new JLabel("Endereço:");
lbCity = new JLabel("Cidade:");
lbFone = new JLabel("Telefone:");
//TextField
Nome = new JTextField();
Sobrenome = new JTextField();
//Cpf = new JTextField();
//Data_Nascimento = new JTextField();
try {
Data_Nascimento = new MaskFormatter("##/##/####");
Cpf = new MaskFormatter("###.###.###-##");
Telefone = new MaskFormatter("(##)####-####");
} catch (ParseException e) {
e.printStackTrace();
}
Endereco = new JTextField();
//Telefone = new JTextField();
cmbCidade = new JComboBox(); //ComboBox
cmbCidade.addItem("São Gonçalo");
cmbCidade.addItem("São Fidelis");
cmbCidade.addItem("Campus");
cmbCidade.addItem("Itaboraí");
getContentPane().add(jbCadastrar);
getContentPane().add(jbConsultar);
getContentPane().add(jbLimpar);
getContentPane().add(lbNome);
getContentPane().add(Nome);
getContentPane().add(lbSobrenome);
getContentPane().add(Sobrenome);
getContentPane().add(lbCpf);
jFormattedTextCpf = new JFormattedTextField(Cpf);
getContentPane().add(jFormattedTextCpf);
getContentPane().add(lbData_nasc);
jFormattedTextData = new JFormattedTextField(Data_Nascimento);
getContentPane().add(jFormattedTextData);
getContentPane().add(lbEnd);
getContentPane().add(Endereco);
getContentPane().add(lbCity);
getContentPane().add(cmbCidade);
jFormattedTextTele = new JFormattedTextField(Telefone);
getContentPane().add(jFormattedTextTele);
getContentPane().add(lbFone);
//Tamanho
Nome.setBounds(46,60,180,20);
lbNome.setBounds(2,60,120,20);
Sobrenome.setBounds(322,60,180,20); //caixa de texto
lbSobrenome.setBounds(240,60,120,20); //rotulo
jFormattedTextCpf.setBounds(46,100,120,20);
lbCpf.setBounds(2,100,60,20);
jFormattedTextData.setBounds(322,100,120,20);
//Data_Nascimento.setBounds(322,100,120,20);
lbData_nasc.setBounds (193,100,120,20);
Endereco.setBounds(67,140,350,20);
lbEnd.setBounds (2,140,120,20);
cmbCidade.setBounds(67,180,120,20);
lbCity.setBounds (2,180,100,20);
jFormattedTextTele.setBounds(67,220,120,20);
lbFone.setBounds (2,220,120,20);
//Botões
jbCadastrar.setBounds(70,300,100,30);
jbConsultar.setBounds(180,300,100,30);
jbLimpar.setBounds (290,300,100,30);
//COR
lbNome.setForeground(Color.BLUE);
lbSobrenome.setForeground(Color.BLUE);
lbCpf.setForeground(Color.BLUE);
lbData_nasc.setForeground(Color.BLUE);
lbEnd.setForeground(Color.BLUE);
lbCity.setForeground(Color.BLUE);
lbFone.setForeground(Color.BLUE);
jbCadastrar.addActionListener(this);
jbConsultar.addActionListener(this);
Nome.addActionListener(this);
Sobrenome.addActionListener(this);
Endereco.addActionListener(this);
cmbCidade.addActionListener(this);
}
public static void main(String[] args) {
Cadastro obj = new Cadastro();
obj.setVisible(true);
}
public void actionPerformed(ActionEvent acao){
if(acao.getSource() == jbCadastrar){
Pessoa p = new Pessoa();
Dao dao = new Dao();
Date dataFormt = new Date();
p.setNome(Nome.getText());
p.setSobrenome(Sobrenome.getText());
p.setCpf(jFormattedTextCpf.getText());
try {
dataFormt = formataData(jFormattedTextData.getText());
} catch (Exception e) {
System.out.println("Erro na Formatação!");
e.printStackTrace();
}
p.setData_Nascimento(dataFormt);
p.setEndereco(Endereco.getText());
p.setCidade((String) cmbCidade.getSelectedItem());
p.setTelefone(jFormattedTextTele.getText());
dao.Salvar(p);
if(acao.getSource() == jbConsultar){
}
if(acao.getSource() == jbLimpar){
jbLimpar.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Nome.setText(null);
Sobrenome.setText(null);
jFormattedTextCpf.setText(null);
jFormattedTextData.setText(null);
Endereco.setText(null);
jFormattedTextTele.setText(null);
}
}
);
}
}
}
public static Date formataData(String data) throws Exception {
if (data == null || data.equals(""))
return null;
Date date = null;
try {
DateFormat formatter;
formatter = new SimpleDateFormat();
date = (java.util.Date)formatter.parse(data);
} catch (ParseException e) {
throw e;
}
return date;
}
}
package br.com.alexsoares;
import java.sql.*;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Driver;
import java.sql.SQLException;
/**
* Created by Alex Soares on 07/10/2014.
*/
public class Conexao {
private Connection conn;
public String user;
public String password;
public String url;
public void AbrirConexao(){
try {
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("Não foi possível encontrar a classe do DRIVER" + e);
} catch (SQLException ex){
ex.printStackTrace();
System.out.println("Não foi possível conectar ao servidor" + ex);
}
}
public void FecharConexao(){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Não Fechou conexão" + e);
}
}
}
package br.com.alexsoares;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import java.sql.SQLException;
/**
* Created by Alex Soares on 07/10/2014.
*/
public class Dao extends Conexao {
private Connection conn = null;
private Statement stmt = null;
private PreparedStatement ptmt = null;
public ResultSet rs = null;
public Conexao cx = new Conexao();
public void Salvar(Pessoa p){
if(cx != null) {
cx.AbrirConexao();
}
try {
String query = "INSERT INTO pessoa(nome, sobrenome, cpf, data_nasc, end, cidade, telefone)"+
"VALUE ('" + p.getNome() + "','" + p.getSobrenome() + "','" + p.getCpf() + "','" +
p.getData_Nascimento() + "','" + p.getEndereco() + "','" + p.getCidade() + "','" +
p.getTelefone() + "')";
stmt.execute(query);
}catch (SQLException e){
System.out.println("Query não excutada!" + e);
throw new RuntimeException(e);
}finally {
cx.FecharConexao();
System.out.println("Ok!");
}
}
public void alterar(Pessoa p){
if(cx != null) {
cx.AbrirConexao();
}
try {
String query = "UPDATE pessoa SET nome = '" + p.getNome() + "' sobrenome = '" + p.getSobrenome() + "'cpf = '" + p.getCpf() + "'data_nasc ='" +
p.getData_Nascimento() + "'end ='" + p.getEndereco() + "'cidade ='" + p.getCidade() + "'telefone ='" +
p.getTelefone() + "WHERE id = " + p.getId();
stmt.executeUpdate(query);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error ao Alterar..." + e);
}finally{
cx.FecharConexao();
}
}
public void deletar(Pessoa p){
if(cx != null) {
cx.AbrirConexao();
}
try {
stmt.execute("DELETE FROM pessoa WHERE id =" + p.getId());
} catch (Exception e) {
e.printStackTrace();
}finally{
cx.FecharConexao();
}
}
}
package br.com.alexsoares;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by Alex Soares on 06/10/2014.
*/
public class Pessoa {
private int id;
private String nome;
private String sobrenome;
private String cpf;
private Date data_Nascimento;
private String endereco;
private String cidade;
private String telefone;
public Pessoa(){
}
public Pessoa(String _nome, String _sobrenome, String _cpf, Date _data_Nascimento, String _endereco, String _cidade, String _telefone){
this.nome = _nome;
this.sobrenome = _sobrenome;
this.cpf = _cpf;
this.data_Nascimento = _data_Nascimento;
this.endereco = _endereco;
this.cidade = _cidade;
this.telefone = _telefone;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getSobrenome() {
return sobrenome;
}
public void setSobrenome(String sobrenome) {
this.sobrenome = sobrenome;
}
public String getCpf() {
return cpf;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public Date getData_Nascimento() {
return data_Nascimento;
}
public void setData_Nascimento(Date data_Nascimento) {
this.data_Nascimento = data_Nascimento;
}
public String getEndereco() {
return endereco;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
public String getCidade() {
return cidade;
}
public void setCidade(String cidade) {
this.cidade = cidade;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment