0% found this document useful (0 votes)
16 views4 pages

Amelia

The document outlines a Java Swing application for a simple CRUD (Create, Read, Update, Delete) interface that connects to a MySQL database. It includes components for user input (name and email) and a table to display database entries, with functionality to add and delete users. The application initializes a database connection and sets up the user interface elements within a JFrame.
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)
16 views4 pages

Amelia

The document outlines a Java Swing application for a simple CRUD (Create, Read, Update, Delete) interface that connects to a MySQL database. It includes components for user input (name and email) and a table to display database entries, with functionality to add and delete users. The application initializes a database connection and sets up the user interface elements within a JFrame.
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/ 4

Package com.

example;

Import java.awt.EventQueue;

Import java.sql.Connection;

Import java.sql.DriverManager;

Import java.sql.PreparedStatement;

Import java.sql.ResultSet;

Import java.sql.SQLException;

Import java.util.logging.Level;

Import java.util.logging.Logger;

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 javax.swing.JScrollPane;

Import javax.swing.JTable;

Import javax.swing.table.DefaultTableModel;

Public class MainFrame extends JFrame {

Private JPanel contentPane;

Private JTextField textFieldName;

Private JTextField textFieldEmail;

Private JTable table;

Private static final String DB_URL = “jdbc:mysql://localhost:3306/test_db”;

Private static final String DB_USERNAME = “root”;

Private static final String DB_PASSWORD = “password”;


Private Connection connection;

Public static void main(String[] args) {

EventQueue.invokeLater(() -> {

Try {

MainFrame frame = new MainFrame();

Frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

});

Public MainFrame() {

Initialize();

initDatabase();

refreshTable();

Private void initialize() {

setTitle(“Simple CRUD App”);

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 lblName = new JLabel(“Name”);

lblName.setBounds(26, 23, 46, 14);

contentPane.add(lblName);
textFieldName = new JTextField();

textFieldName.setBounds(26, 48, 155, 20);

contentPane.add(textFieldName);

textFieldName.setColumns(10);

JLabel lblEmail = new JLabel(“Email”);

lblEmail.setBounds(26, 79, 46, 14);

contentPane.add(lblEmail);

textFieldEmail = new JTextField();

textFieldEmail.setBounds(26, 104, 155, 20);

contentPane.add(textFieldEmail);

textFieldEmail.setColumns(10);

JButton btnAdd = new JButton(“Add”);

btnAdd.addActionListener((e) -> {

String name = textFieldName.getText();

String email = textFieldEmail.getText();

Try {

PreparedStatement statement = connection.prepareStatement(“INSERT INTO users (name,


email) VALUES (?, ?)”);

Statement.setString(1, name);

Statement.setString(2, email);

Statement.executeUpdate();

Statement.close();

refreshTable();

textFieldName.setText(“”);

textFieldEmail.setText(“”);

} catch (SQLException ex) {

Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);


}

});

btnAdd.setBounds(26, 135, 89, 23);

contentPane.add(btnAdd);

JScrollPane scrollPane = new JScrollPane();

scrollPane.setBounds(225, 23, 199, 205);

contentPane.add(scrollPane);

table = new JTable();

scrollPane.setViewportView(table);

JButton btnDelete = new JButton(“Delete”);

btnDelete.addActionListener((e) -> {

int selectedRow = table.getSelectedRow();

if (selectedRow >= 0) {

String id = (String) table.getValueAt(selectedRow, 0);

Try {

PreparedStatement statement = connection.prepareStatement(“DELETE FROM

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy