You are on page 1of 22

Simple CRUD with Netbeans

Rizky Tahara Shita


Requirements


MySQL Database Server
– XAMPP
– Manual Install

Windows:
– https://www.mysql.com/downloads/
– https://dev.mysql.com/downloads/windows/installer/8.0.html

Linux:
– Install dari Repository

Download Connector
– https://dev.mysql.com/downloads/connector/j/
– MySQL JDBC Driver
– MySQL Connector
2
Requirements


Download Netbeans
– https://netbeans.apache.org/download/index.html

Install it!

3
Prepare Database & Table
Prepare Database & Table


Create Database
– create database libraryDB;

Create Table
– use libraryDB;
– create table categories (
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB;
5
Prepare Database & Table


Insert Sample Data
– insert into categories (name) values (‘Komputer’);
– insert into categories (name) values (‘Sastra’);

6
Setup Netbeans Connector for MySQL
Create New Project


File > New Project

Java with Maven > Java Application

Entry Project Name

Finish

8
Setup Dependencies


Right Click Dependencies

Add Dependencies

9
Setup Dependencies


Select mysql-connector-java-
8.0.27.jar

Right Click

Select Manual Install Artifact

Browse the MySQL
Connector .jar file

Click Install Locally

10
CRUD: List Form
Category List


Add New JFrame Form

Class Name: CategoryList

Finish

12
Category List

13
Refresh Button

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // Show to Table


// Connect to Database Server while(rs.next()) {
try {
String id = String.valueOf(rs.getInt("id"));
Connection conn = DriverManager.getConnection(
String name = String.valueOf(rs.getString("name"));
"jdbc:mysql://localhost/libraryDB",
"user",
String tblData[] = {id, name};
"password"
);
tblModel.addRow(tblData);
Statement state = conn.createStatement(); }

// Get Query } catch(Exception ex) {


PreparedStatement pst = conn.prepareStatement(
JOptionPane.showMessageDialog(
"select * from categories order by name"
null,
);
"Error : " + ex,
ResultSet rs = pst.executeQuery();
"Pesan",

DefaultTableModel tblModel = (DefaultTableModel)jTable1.getModel(); JOptionPane.ERROR_MESSAGE


);
// Clear the Table Data }
tblModel.setRowCount(0); }

14
CRUD: Master Form
Category Form


Add New JFrame Form

Class Name: CategoryForm

Finish

16
Category Form

17
Category List::New Button


Double Click the NEW Button at the Category List

Add the code to Open CategoryForm

18
Category Form::Save Button

19
Run the App


F6 to Run it!

Choose CategoryList as Main
Class

20
The App

21
Thank You
@wukongrita

22

You might also like