Simple CRUD with Netbeans
Rizky Tahara Shita
Requirements
●
MySQL Database Server
– XAMPP
– Manual Install
●
Windows:
– [Link]
– [Link]
●
Linux:
– Install dari Repository
●
Download Connector
– [Link]
– MySQL JDBC Driver
– MySQL Connector
2
Requirements
●
Download Netbeans
– [Link]
●
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-
[Link]
●
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([Link] evt) { // Show to Table
// Connect to Database Server while([Link]()) {
try {
String id = [Link]([Link]("id"));
Connection conn = [Link](
String name = [Link]([Link]("name"));
"jdbc:mysql://localhost/libraryDB",
"user",
String tblData[] = {id, name};
"password"
);
[Link](tblData);
Statement state = [Link](); }
// Get Query } catch(Exception ex) {
PreparedStatement pst = [Link](
[Link](
"select * from categories order by name"
null,
);
"Error : " + ex,
ResultSet rs = [Link]();
"Pesan",
DefaultTableModel tblModel = (DefaultTableModel)[Link](); JOptionPane.ERROR_MESSAGE
);
// Clear the Table Data }
[Link](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