0% found this document useful (0 votes)
9 views

Import Javax

Uploaded by

lucia ferreira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Import Javax

Uploaded by

lucia ferreira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

import javax.swing.

*;

import javax.swing.table.DefaultTableModel;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.*;

public class ClienteApp {

private JFrame frame;

private JTable table;

private DefaultTableModel tableModel;

private JTextField idField, nameField, emailField;

private Connection connection;

public ClienteApp() {

// Configuração do banco de dados

try {

connection = DriverManager.getConnection("jdbc:sqlite:clientes.db");

Statement stmt = connection.createStatement();

stmt.execute("CREATE TABLE IF NOT EXISTS clientes (id INTEGER PRIMARY KEY


AUTOINCREMENT, nome TEXT, email TEXT)");

stmt.close();

} catch (SQLException e) {

e.printStackTrace();

// Configuração da interface gráfica

frame = new JFrame("Gerenciador de Clientes");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(500, 400);

frame.setLayout(new BorderLayout());
// Modelo da tabela

tableModel = new DefaultTableModel(new Object[]{"ID", "Nome", "Email"}, 0);

table = new JTable(tableModel);

loadClientes();

JScrollPane scrollPane = new JScrollPane(table);

frame.add(scrollPane, BorderLayout.CENTER);

// Painel de entrada

JPanel panel = new JPanel();

idField = new JTextField(5);

nameField = new JTextField(15);

emailField = new JTextField(15);

JButton addButton = new JButton("Adicionar");

JButton editButton = new JButton("Editar");

JButton deleteButton = new JButton("Apagar");

panel.add(new JLabel("ID:"));

panel.add(idField);

panel.add(new JLabel("Nome:"));

panel.add(nameField);

panel.add(new JLabel("Email:"));

panel.add(emailField);

panel.add(addButton);

panel.add(editButton);

panel.add(deleteButton);

frame.add(panel, BorderLayout.NORTH);

// Ação do botão "Adicionar"

addButton.addActionListener(e -> addCliente());


// Ação do botão "Editar"

editButton.addActionListener(e -> editCliente());

// Ação do botão "Apagar"

deleteButton.addActionListener(e -> deleteCliente());

frame.setVisible(true);

private void loadClientes() {

tableModel.setRowCount(0);

try {

Statement stmt = connection.createStatement();

ResultSet rs = stmt.executeQuery("SELECT * FROM clientes");

while (rs.next()) {

tableModel.addRow(new Object[]{rs.getInt("id"), rs.getString("nome"),


rs.getString("email")});

rs.close();

stmt.close();

} catch (SQLException e) {

e.printStackTrace();

private void addCliente() {

String nome = nameField.getText();

String email = emailField.getText();

if (!nome.isEmpty() && !email.isEmpty()) {

try {

PreparedStatement pstmt = connection.prepareStatement("INSERT INTO clientes


(nome, email) VALUES (?, ?)");
pstmt.setString(1, nome);

pstmt.setString(2, email);

pstmt.executeUpdate();

pstmt.close();

loadClientes();

clearFields();

} catch (SQLException e) {

e.printStackTrace();

private void editCliente() {

int id = Integer.parseInt(idField.getText());

String nome = nameField.getText();

String email = emailField.getText();

if (!nome.isEmpty() && !email.isEmpty()) {

try {

PreparedStatement pstmt = connection.prepareStatement("UPDATE clientes SET


nome = ?, email = ? WHERE id = ?");

pstmt.setString(1, nome);

pstmt.setString(2, email);

pstmt.setInt(3, id);

pstmt.executeUpdate();

pstmt.close();

loadClientes();

clearFields();

} catch (SQLException e) {

e.printStackTrace();

}
}

private void deleteCliente() {

int id = Integer.parseInt(idField.getText());

try {

PreparedStatement pstmt = connection.prepareStatement("DELETE FROM clientes


WHERE id = ?");

pstmt.setInt(1, id);

pstmt.executeUpdate();

pstmt.close();

loadClientes();

clearFields();

} catch (SQLException e) {

e.printStackTrace();

private void clearFields() {

idField.setText("");

nameField.setText("");

emailField.setText("");

public static void main(String[] args) {

SwingUtilities.invokeLater(ClienteApp::new);

You might also like