package swing;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
public class ClienteJFrame extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JLabel lblCodcliente;
private JTextField textField_2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ClienteJFrame frame = new ClienteJFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ClienteJFrame() {
setTitle("Cliente");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNome = new JLabel("Nome:");
lblNome.setBounds(25, 59, 46, 14);
contentPane.add(lblNome);
textField = new JTextField();
textField.setBounds(134, 56, 86, 20);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(134, 98, 86, 20);
contentPane.add(textField_1);
textField_1.setColumns(10);
JLabel lblTelefone = new JLabel("Telefone:");
lblTelefone.setBounds(25, 101, 63, 14);
contentPane.add(lblTelefone);
JButton btnOk = new JButton("OK");
btnOk.setForeground(Color.RED);
btnOk.setBounds(23, 151, 89, 23);
contentPane.add(btnOk);
lblCodcliente = new JLabel("CodCliente:");
lblCodcliente.setBounds(25, 22, 87, 14);
contentPane.add(lblCodcliente);
textField_2 = new JTextField();
textField_2.setBounds(134, 19, 86, 20);
contentPane.add(textField_2);
textField_2.setColumns(10);
}
}
package sql;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.Cliente;
public class AcessoClienteDAO implements ClienteDAO{
public void inserir(Cliente c) throws AcessoClienteException
{
Connection conexao = null;
PreparedStatement comando = null;
try
{
conexao = Conexao.getConexao();
String sql = "INSERT INTO cliente VALUES (?, ?, ?)";
comando = conexao.prepareStatement(sql);
comando.setInt(1, c.getCodc());
comando.setString(2, c.getNomec());
comando.setString(3, c.getFonec());
comando.executeUpdate();
System.out.println(sql);
}
catch(SQLException e)
{
throw new AcessoClienteException("erro de inserção de cliente", e);
}
finally
{
if(conexao != null)
{
try
{
conexao.close();
comando.close();
}
catch(SQLException e2)
{
}
}
}
}
public List<Cliente> buscarTodos() {
Connection conexao = null;
PreparedStatement comando = null;
ResultSet resultado = null;
try {
try {
conexao = Conexao.getConexao();
String sql = "SELECT * FROM cliente";
comando = conexao.prepareStatement(sql);
resultado = comando.executeQuery();
List<Cliente> lista = new ArrayList<Cliente>();
while(resultado.next()) {
int codc = resultado.getInt("codc");
String nomec = resultado.getString("nomec");
String fonec = resultado.getString("fonec");
lista.add(new Cliente(codc, nomec, fonec));
}
return lista;
} finally {
if (resultado != null) resultado.close();
if (comando != null) comando.close();
if (conexao != null) conexao.close();
}
} catch(SQLException e) {
e.printStackTrace();
throw new AcessoClienteException("erro de busca de cliente", e);
}
}
}
muito obrigada pela atenção!
beijokas!

