INTERFACE DO JAVA
package sistema.gui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import sistema.control.ClienteControl;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ClienteGui extends JFrame {
private JPanel contentPane;
private JTextField jtfNome;
private JTextField jtfEmail;
private JTextField jtfTelefone;
private JTextField jtfEndereco;
private JTextField jtfCidade;
private JTextField jtfEstado;
ClienteControl CliControl = new ClienteControl();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ClienteGui frame = new ClienteGui();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ClienteGui() {
setTitle("Cliente");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 491, 318);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("Cadastro De Clientes");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 17));
lblNewLabel.setBounds(10, 11, 242, 42);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Nome:");
lblNewLabel_1.setBounds(10, 82, 46, 14);
contentPane.add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("Email:");
lblNewLabel_2.setBounds(10, 107, 46, 14);
contentPane.add(lblNewLabel_2);
JLabel lblNewLabel_3 = new JLabel("Telefone:");
lblNewLabel_3.setBounds(10, 132, 46, 14);
contentPane.add(lblNewLabel_3);
JLabel lblNewLabel_4 = new JLabel("Endereco:");
lblNewLabel_4.setBounds(10, 157, 73, 14);
contentPane.add(lblNewLabel_4);
JLabel lblNewLabel_5 = new JLabel("Cidade:");
lblNewLabel_5.setBounds(10, 182, 46, 14);
contentPane.add(lblNewLabel_5);
JLabel lblNewLabel_6 = new JLabel("Estado:");
lblNewLabel_6.setBounds(10, 207, 46, 14);
contentPane.add(lblNewLabel_6);
jtfNome = new JTextField();
jtfNome.setBounds(97, 79, 350, 20);
contentPane.add(jtfNome);
jtfNome.setColumns(10);
jtfEmail = new JTextField();
jtfEmail.setColumns(10);
jtfEmail.setBounds(97, 104, 350, 20);
contentPane.add(jtfEmail);
jtfTelefone = new JTextField();
jtfTelefone.setColumns(10);
jtfTelefone.setBounds(97, 129, 350, 20);
contentPane.add(jtfTelefone);
jtfEndereco = new JTextField();
jtfEndereco.setColumns(10);
jtfEndereco.setBounds(97, 154, 350, 20);
contentPane.add(jtfEndereco);
jtfCidade = new JTextField();
jtfCidade.setColumns(10);
jtfCidade.setBounds(97, 179, 350, 20);
contentPane.add(jtfCidade);
jtfEstado = new JTextField();
jtfEstado.setColumns(10);
jtfEstado.setBounds(97, 204, 350, 20);
contentPane.add(jtfEstado);
JButton btnCadastrar = new JButton("Cadastrar");
btnCadastrar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String nome = jtfNome.getText();
String email = jtfEmail.getText();
String telefone = jtfTelefone.getText();
String endereco = jtfEndereco.getText();
String cidade = jtfCidade.getText();
String estado = jtfEstado.getText();
CliControl.InsereDados(nome,email,telefone,endereco,cidade,estado);
}
});
btnCadastrar.setBounds(358, 235, 89, 23);
contentPane.add(btnCadastrar);
JButton btnLimpar = new JButton("Limpar");
btnLimpar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jtfNome.setText("");
jtfEmail.setText("");
jtfTelefone.setText("");
jtfEndereco.setText("");
jtfCidade.setText("");
jtfEstado.setText("");
}
});
btnLimpar.setBounds(259, 235, 89, 23);
contentPane.add(btnLimpar);
}
}
CONEXAO
package sistema.conexao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class conexao {
Connection con;
private Connection oConn;
private Statement sStmt;
public conexao () {
}
public Connection abrirBDConn () {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/banco";
con = DriverManager.getConnection(url+"root");
System.out.println("Conexão efetuada com sucesso");
return con;
} catch (Exception e) {
System.out.println("Erro ao abrir conexão com o banco:");
e.printStackTrace();
return null;
}
}
public void fecharBDConn() {
try {
con.close();
System.out.println("Conexão finalizada com sucesso");
} catch (Exception e) {
System.out.println("Erro ao fechar conexão com o ba" +e.getMessage());
}
}
}
CLIENTECONTROL
package sistema.control;
import javax.swing.JOptionPane;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import sistema.conexao.conexao;
public class ClienteControl {
public void InsereDados(String nome, String email, String endereco, String telefone, String cidade, String estado) {
conexao banco = new conexao();
String retorno = "erro";
try {
Connection Exconn = (Connection) banco.abrirBDConn();
Statement stmt = (Statement) Exconn.createStatement();
String sSQL = "INSERT INTO banco.cliente VALUES (null,'"+nome+"','"+email+"','"+telefone+"','"+endereco+"','"+cidade+"','"+estado+"');";
System.out.println(sSQL);
boolean res = stmt.execute(sSQL);
JOptionPane.showMessageDialog(null,(!res)?"Dados inseridos com sucesso!!!":"" + "Os dados não puderam ser inseridos!!!");
stmt.close();
banco.fecharBDConn();
} catch (Exception e) {
JOptionPane.showMessageDialog(null,"Os dados não puderam ser inseridos!!!");
}
}
}
BANCO DE DADOS
CREATE DATABASE banco;
USE banco;
CREATE TABLE cliente (
idCliente INT (10) AUTO_INCREMENT,
nome VARCHAR (50) NULL,
email VARCHAR (50) NULL,
telefone VARCHAR(20) NULL,
endereco VARCHAR (20) NULL,
cidade VARCHAR (20) NULL,
estado VARCHAR (20) NULL,
PRIMARY KEY (idCliente)
);
SELECT * FROM cliente;
INSERT INTO banco.cliente VALUES(null,‘nome’,‘email’,‘telefone’,‘endereco’,‘cidade’,‘estado’);