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

Appendix

The document contains code for a Java Swing application with two main components: a login page and a registration page. The login page allows users to enter their username and password, and it connects to a MySQL database to authenticate users. The registration page enables new users to create an account, with functionality to show passwords and navigate between the login and registration interfaces.

Uploaded by

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

Appendix

The document contains code for a Java Swing application with two main components: a login page and a registration page. The login page allows users to enter their username and password, and it connects to a MySQL database to authenticate users. The registration page enables new users to create an account, with functionality to show passwords and navigate between the login and registration interfaces.

Uploaded by

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

Appendix

The main structure of my program:


Login Page Code
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change
this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this
template
*/
package childrenform.java;

import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
*
* @author latifahdrupadi
*/
public class loginpage extends javax.swing.JFrame {

/**
* Creates new form loginpage
*/
public loginpage() {
initComponents();

}
public void close(){
WindowEvent closeWindow=new
WindowEvent(this,WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(closeWindow);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();


user = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
showpass = new javax.swing.JCheckBox();
password = new javax.swing.JPasswordField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBackground(new java.awt.Color(0, 153, 153));

jButton1.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N


jButton1.setText("Login");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N


jButton2.setText("Sign Up");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton3.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N


jButton3.setText("Close");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

showpass.setFont(new java.awt.Font(".AppleSystemUIFont", 0, 18)); // NOI18N


showpass.setForeground(new java.awt.Color(255, 255, 255));
showpass.setText("Show Password");
showpass.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
showpassActionPerformed(evt);
}
});

jLabel1.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/childrenform/java/newpackage/mom (1)
(1).png"))); // NOI18N

jLabel2.setFont(new java.awt.Font(".AppleSystemUIFont", 0, 18)); // NOI18N


jLabel2.setForeground(new java.awt.Color(255, 255, 255));
jLabel2.setText("Username");
jLabel3.setFont(new java.awt.Font(".AppleSystemUIFont", 0, 18)); // NOI18N
jLabel3.setForeground(new java.awt.Color(255, 255, 255));
jLabel3.setText("Password");

jLabel4.setFont(new java.awt.Font("Sinhala MN", 1, 36)); // NOI18N


jLabel4.setForeground(new java.awt.Color(255, 255, 255));
jLabel4.setText("Posyandu Data Program");

jLabel5.setFont(new java.awt.Font("Sinhala MN", 0, 24)); // NOI18N


jLabel5.setForeground(new java.awt.Color(255, 255, 255));
jLabel5.setText("Login");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);


jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addGap(36, 36, 36)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILI
NG)
.addComponent(jButton3)
.addGroup(jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILI
NG)
.addComponent(jLabel3)
.addComponent(jLabel2))
.addGap(71, 71, 71)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILI
NG)
.addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE,
150, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(user, javax.swing.GroupLayout.PREFERRED_SIZE, 150,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(showpass)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton2)
.addGap(145, 145, 145)
.addComponent(jButton1)))
.addGap(77, 77, 77))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(jLabel4)
.addGap(62, 62, 62))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(247, 247, 247)
.addComponent(jLabel5)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel5)
.addGap(24, 24, 24)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addComponent(jLabel1)
.addGroup(jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(jLabel2)
.addComponent(user, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(jLabel3)
.addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(showpass)
.addGap(18, 18, 18)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(jButton1)
.addComponent(jButton2))))
.addGap(18, 18, 18)
.addComponent(jButton3)
.addContainerGap(30, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);

pack();
setLocationRelativeTo(null);
}// </editor-fold>

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {


this.dispose();
registpage reg=new registpage();
reg.setVisible(true);
}

private void showpassActionPerformed(java.awt.event.ActionEvent evt) {


if(showpass.isSelected()){
password.setEchoChar((char)0);
}
else{
password.setEchoChar('*');
}
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {


String Password, query, usr = null, passDb = null;
String SUrl, SUser, SPass;
SUrl = "jdbc:MySQL://localhost:3306/test";
SUser = "root";
SPass = "";
int notFound = 0;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection(SUrl, SUser, SPass);
Statement st = con.createStatement();
if("".equals(user.getText())){
JOptionPane.showMessageDialog(new JFrame(), "Username is required", "Error",
JOptionPane.ERROR_MESSAGE);
}else if("".equals(password.getText())){
JOptionPane.showMessageDialog(new JFrame(), "Password is required", "Error",
JOptionPane.ERROR_MESSAGE);
}else {
usr = user.getText();
Password = password.getText();

query = "SELECT * FROM userpass WHERE Username= '"+usr+"'";

ResultSet rs = st.executeQuery(query);
while(rs.next()){
passDb = rs.getString("Password");
usr = rs.getString("Username");
notFound = 1;
}
if(notFound == 1 && Password.equals(passDb)){
this.dispose();
homepage home=new homepage(user.getText());
home.setVisible(true);
}else{
JOptionPane.showMessageDialog(new JFrame(), "Incorrect username or
password", "Error",
JOptionPane.ERROR_MESSAGE);
}
password.setText("");
}
}catch(Exception e){
System.out.println("Error!" + e.getMessage());
}
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {


close();
}

/**
* @param args the command line arguments
*/
public static void loginpage(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and
feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(loginpage.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(loginpage.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(loginpage.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(loginpage.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new loginpage().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JPasswordField password;
private javax.swing.JCheckBox showpass;
private javax.swing.JTextField user;
// End of variables declaration
}
Registration Page Code
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change
this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this
template
*/
package childrenform.java;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import static javax.swing.JOptionPane.showMessageDialog;

/**
*
* @author latifahdrupadi
*/
public class registpage extends javax.swing.JFrame {

/**
* Creates new form registpage
*/
public registpage() {
initComponents();

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();


create = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
showpass = new javax.swing.JCheckBox();
user = new javax.swing.JTextField();
password = new javax.swing.JPasswordField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBackground(new java.awt.Color(0, 153, 153));

create.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N


create.setText("Create");
create.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
createActionPerformed(evt);
}
});

jButton2.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N


jButton2.setText("Login");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

showpass.setFont(new java.awt.Font(".AppleSystemUIFont", 0, 18)); // NOI18N


showpass.setForeground(new java.awt.Color(255, 255, 255));
showpass.setText("Show Password");
showpass.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
showpassActionPerformed(evt);
}
});

jLabel1.setFont(new java.awt.Font(".AppleSystemUIFont", 0, 18)); // NOI18N


jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setText("Username");

jLabel2.setFont(new java.awt.Font(".AppleSystemUIFont", 0, 18)); // NOI18N


jLabel2.setForeground(new java.awt.Color(255, 255, 255));
jLabel2.setText("Password");

jLabel3.setBackground(new java.awt.Color(255, 255, 255));


jLabel3.setFont(new java.awt.Font("Sinhala MN", 1, 36)); // NOI18N
jLabel3.setForeground(new java.awt.Color(255, 255, 255));
jLabel3.setText("Posyandu Data Program");

jLabel4.setFont(new java.awt.Font("Sinhala MN", 1, 24)); // NOI18N


jLabel4.setForeground(new java.awt.Color(255, 255, 255));
jLabel4.setText("Sign Up");

jLabel5.setIcon(new
javax.swing.ImageIcon(getClass().getResource("/childrenform/java/newpackage/mom (1)
(1).png"))); // NOI18N
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILI
NG, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(39, 39, 39)
.addComponent(jLabel5)
.addGap(20, 20, 20)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILI
NG)
.addComponent(showpass)
.addGroup(jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addGap(62, 62, 62)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG, false)
.addComponent(user, javax.swing.GroupLayout.DEFAULT_SIZE,
150, Short.MAX_VALUE)
.addComponent(password)))))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(97, 97, 97)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(create)
.addGap(21, 21, 21)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addGap(0, 53, Short.MAX_VALUE)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(53, 53, 53))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addComponent(jLabel4)
.addGap(216, 216, 216))))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel4)
.addGap(18, 18, 18)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILI
NG)
.addComponent(jLabel5)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(user, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(showpass)))
.addGap(29, 29, 29)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(create)
.addComponent(jButton2))
.addGap(40, 40, 40))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);

pack();
setLocationRelativeTo(null);
}// </editor-fold>

private void showpassActionPerformed(java.awt.event.ActionEvent evt) {


if(showpass.isSelected()){
password.setEchoChar((char)0);
}
else{
password.setEchoChar('*');
}
}

private void createActionPerformed(java.awt.event.ActionEvent evt) {


String usr, pass, query;
String SUrl, SUser, SPass;
SUrl = "jdbc:mysql://localhost:3306/test";
SUser = "root";
SPass = "";
String sql="insert into userpass values (?,?)";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection(SUrl, SUser, SPass);
Statement st = con.createStatement();
if("".equals(user.getText())){
JOptionPane.showMessageDialog(new JFrame(), "Full Name is required", "Error",
JOptionPane.ERROR_MESSAGE);
}else if("".equals(password.getText())){
JOptionPane.showMessageDialog(new JFrame(), "Password is required", "Error",
JOptionPane.ERROR_MESSAGE);
}else {
usr = user.getText();

pass = password.getText();
System.out.println(password);
String checkQuery = "SELECT * FROM userpass WHERE Username = '" + usr + "'";
try (ResultSet resultSet = st.executeQuery(checkQuery)) {
if (resultSet.next()) {
JOptionPane.showMessageDialog(new JFrame(), "Username already exists",
"Error",
JOptionPane.ERROR_MESSAGE);
} else {
query = "INSERT INTO userpass (Username, Password)"+
"VALUES('"+usr+"', '"+pass+"')";

st.execute(query);
user.setText("");
password.setText("");
showMessageDialog(null, "New account has been CREATED successfully!");
}}}
}catch(Exception e){
System.out.println("Error!" + e.getMessage());
}
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {


this.dispose();
loginpage log=new loginpage();
log.setVisible(true);
}

/**
* @param args the command line arguments
*/
public static void registpage(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and
feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(registpage.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(registpage.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(registpage.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(registpage.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new registpage().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JButton create;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JPasswordField password;
private javax.swing.JCheckBox showpass;
private javax.swing.JTextField user;
// End of variables declaration
}
Home Page Code
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change
this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this
template
*/
package childrenform.java;

/**
*
* @author latifahdrupadi
*/
public class homepage extends javax.swing.JFrame {

/**
* Creates new form homepage
*/
public homepage(String name) {
initComponents();
user.setText(name);
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();


user = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBackground(new java.awt.Color(0, 153, 153));

user.setFont(new java.awt.Font(".AppleSystemUIFont", 0, 18)); // NOI18N


user.setForeground(new java.awt.Color(255, 255, 0));

jButton1.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N


jButton1.setText("Next");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jLabel1.setFont(new java.awt.Font(".AppleSystemUIFont", 0, 18)); // NOI18N


jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setText("Welcome,");

jLabel2.setFont(new java.awt.Font(".AppleSystemUIFont", 0, 18)); // NOI18N


jLabel2.setForeground(new java.awt.Color(255, 255, 255));
jLabel2.setText("Welcome to Healthcare Unit Data Program!");

jLabel3.setFont(new java.awt.Font(".AppleSystemUIFont", 0, 18)); // NOI18N


jLabel3.setForeground(new java.awt.Color(255, 255, 255));
jLabel3.setText("This program will help you to record information of children that come
to Posyandu unit healthcare");
jLabel4.setFont(new java.awt.Font(".AppleSystemUIFont", 0, 18)); // NOI18N
jLabel4.setForeground(new java.awt.Color(255, 255, 255));
jLabel4.setText("No need to write down anymore! Save your paper and work
efficiently:)");

jLabel5.setFont(new java.awt.Font("Sinhala MN", 1, 36)); // NOI18N


jLabel5.setForeground(new java.awt.Color(255, 255, 255));
jLabel5.setText("Posyandu Data Program");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);


jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(jLabel5)
.addGap(256, 256, 256))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(33, 33, 33)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addComponent(jLabel4)
.addComponent(jLabel3)
.addComponent(jLabel2)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(user, javax.swing.GroupLayout.PREFERRED_SIZE, 50,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(51, 51, 51)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel5)
.addGap(39, 39, 39)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG, false)
.addComponent(user, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel4)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addContainerGap(36, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);

pack();
setLocationRelativeTo(null);
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {


this.dispose();
mainpage main=new mainpage();
main.setVisible(true);
}

/**
* @param args the command line arguments
*/
public static void homepage(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and
feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(homepage.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(homepage.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(homepage.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(homepage.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {

}
});
}

// Variables declaration - do not modify


private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JLabel user;
// End of variables declaration
}
Main Page Code
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change
this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this
template
*/
package childrenform.java;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
*
* @author latifahdrupadi
*/
public class mainpage extends javax.swing.JFrame {

/**
* Creates new form mainpage
*/

public mainpage() {
initComponents();
dob.getDateEditor().setEnabled(false);
}
public void close(){
WindowEvent closeWindow=new
WindowEvent(this,WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(closeWindow);
}
public void addData(){
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();


refresh = new javax.swing.JButton();
add = new javax.swing.JButton();
delete = new javax.swing.JButton();
update = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
recap = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
tbl_add_data = new javax.swing.JTable();
name = new javax.swing.JTextField();
id = new javax.swing.JTextField();
dob = new com.toedter.calendar.JDateChooser();
blood = new javax.swing.JComboBox<>();
weight = new javax.swing.JTextField();
height = new javax.swing.JTextField();
head = new javax.swing.JTextField();
wob = new javax.swing.JComboBox<>();
father = new javax.swing.JTextField();
mother = new javax.swing.JTextField();
address = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
jLabel11 = new javax.swing.JLabel();
jLabel12 = new javax.swing.JLabel();
jLabel13 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBackground(new java.awt.Color(0, 153, 153));

refresh.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N


refresh.setText("Show Data");
refresh.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
refreshActionPerformed(evt);
}
});

add.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N


add.setText("Add");
add.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addActionPerformed(evt);
}
});

delete.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N


delete.setText("Delete");
delete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
deleteActionPerformed(evt);
}
});

update.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N


update.setText("Update");
update.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
updateActionPerformed(evt);
}
});

jButton4.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N


jButton4.setText("Close");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});

recap.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N


recap.setText("Recap");
recap.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
recapActionPerformed(evt);
}
});

tbl_add_data.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null}
},
new String [] {
"Name", "ID", "Date of Birth", "Blood Type", "Weight (kg)", "Height (cm)", "Head
Circumference (cm)", "Delivery of Birth", "Father's Name", "Mother's Name", "Address"
}
){
Class[] types = new Class [] {
java.lang.Object.class, java.lang.Integer.class, java.lang.Object.class,
java.lang.Object.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class,
java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
};
boolean[] canEdit = new boolean [] {
false, false, false, false, false, false, false, false, false, false, false
};

public Class getColumnClass(int columnIndex) {


return types [columnIndex];
}

public boolean isCellEditable(int rowIndex, int columnIndex) {


return canEdit [columnIndex];
}
});
tbl_add_data.setColumnSelectionAllowed(true);
jScrollPane1.setViewportView(tbl_add_data);

id.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
idActionPerformed(evt);
}
});

dob.setDateFormatString("YYYY-MM-dd");

blood.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "O", "A",


"B", "AB" }));
blood.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bloodActionPerformed(evt);
}
});

weight.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
weightActionPerformed(evt);
}
});

height.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
heightActionPerformed(evt);
}
});

wob.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Normal",


"Caesar" }));

father.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fatherActionPerformed(evt);
}
});

jLabel1.setFont(new java.awt.Font("Sinhala MN", 1, 36)); // NOI18N


jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setText("Posyandu Data Program");

jLabel2.setFont(new java.awt.Font("Sinhala MN", 1, 24)); // NOI18N


jLabel2.setForeground(new java.awt.Color(255, 255, 255));
jLabel2.setText("Input Data");

jLabel3.setFont(new java.awt.Font(".AppleSystemUIFont", 0, 18)); // NOI18N


jLabel3.setForeground(new java.awt.Color(255, 255, 255));
jLabel3.setText("Name");

jLabel4.setFont(new java.awt.Font(".AppleSystemUIFont", 0, 18)); // NOI18N


jLabel4.setForeground(new java.awt.Color(255, 255, 255));
jLabel4.setText("ID (max. 10 numbers)");

jLabel5.setFont(new java.awt.Font(".AppleSystemUIFont", 0, 18)); // NOI18N


jLabel5.setForeground(new java.awt.Color(255, 255, 255));
jLabel5.setText("Date of Birth");

jLabel6.setFont(new java.awt.Font(".AppleSystemUIFont", 0, 18)); // NOI18N


jLabel6.setForeground(new java.awt.Color(255, 255, 255));
jLabel6.setText("Blood Type");

jLabel7.setFont(new java.awt.Font(".AppleSystemUIFont", 0, 18)); // NOI18N


jLabel7.setForeground(new java.awt.Color(255, 255, 255));
jLabel7.setText("Weight (kg)");

jLabel8.setFont(new java.awt.Font(".AppleSystemUIFont", 0, 18)); // NOI18N


jLabel8.setForeground(new java.awt.Color(255, 255, 255));
jLabel8.setText("Height (cm)");

jLabel9.setFont(new java.awt.Font(".AppleSystemUIFont", 0, 18)); // NOI18N


jLabel9.setForeground(new java.awt.Color(255, 255, 255));
jLabel9.setText("Head Circumference (cm)");

jLabel10.setFont(new java.awt.Font(".AppleSystemUIFont", 0, 18)); // NOI18N


jLabel10.setForeground(new java.awt.Color(255, 255, 255));
jLabel10.setText("Delivery of Birth");

jLabel11.setFont(new java.awt.Font(".AppleSystemUIFont", 0, 18)); // NOI18N


jLabel11.setForeground(new java.awt.Color(255, 255, 255));
jLabel11.setText("Father's Name");

jLabel12.setFont(new java.awt.Font(".AppleSystemUIFont", 0, 18)); // NOI18N


jLabel12.setForeground(new java.awt.Color(255, 255, 255));
jLabel12.setText("Mother's Name");

jLabel13.setFont(new java.awt.Font(".AppleSystemUIFont", 0, 18)); // NOI18N


jLabel13.setForeground(new java.awt.Color(255, 255, 255));
jLabel13.setText("Address");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);


jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(34, 34, 34)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jLabel2)
.addGap(493, 493, 493)
.addComponent(refresh))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addComponent(jLabel4)
.addComponent(jLabel5)
.addComponent(jLabel3)
.addComponent(jLabel6)
.addComponent(jLabel7)
.addComponent(jLabel8)
.addComponent(jLabel9)
.addComponent(jLabel10)
.addComponent(jLabel11)
.addComponent(jLabel12)
.addComponent(jLabel13))
.addGap(43, 43, 43)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addComponent(id)
.addComponent(dob, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(blood, 0, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(wob, 0, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(weight)
.addComponent(height, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(head, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(father, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(mother, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(address, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(name, javax.swing.GroupLayout.Alignment.TRAILING))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
864, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addComponent(add)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(delete)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(update)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(recap)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton4)))
.addGap(34, 34, 34))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addContainerGap(536, Short.MAX_VALUE)
.addComponent(jLabel1)
.addGap(536, 536, 536))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(74, 74, 74)
.addComponent(refresh))
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(12, 12, 12)
.addComponent(jLabel2)))
.addGap(30, 30, 30)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addGroup(jPanel1Layout.createSequentialGroup()

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(id, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addComponent(dob, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(blood,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6)))
.addComponent(jLabel3))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addComponent(weight, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(height, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel8))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(head, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel9))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(wob, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel10))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(father, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel11))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(mother, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel12))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(address, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel13)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
470, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(jButton4)
.addComponent(add)
.addComponent(delete)
.addComponent(update)
.addComponent(recap))))
.addContainerGap(21, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);

pack();
setLocationRelativeTo(null);
}// </editor-fold>
public boolean containsOnlyLetters(String str) {
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (!Character.isLetter(c)) {
return false;
}
}
return true;
}
public boolean containsOnlyNumbers(String str) {
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (!Character.isDigit(c)) {
return false;
}
}
return true;
}
private void addActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel model;

try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root", "");

// Insert data into the database


String sql = "INSERT INTO inputdata (Name, ID, Day, Blood, Weight, Height, Head,
Way, Mother, Father, Address) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
boolean isSuccess=true;

if("".equals(name.getText())){
JOptionPane.showMessageDialog(new JFrame(), "Name is required", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}

if ("".equals(id.getText())) {
JOptionPane.showMessageDialog(new JFrame(), "ID is required", "Error",
JOptionPane.ERROR_MESSAGE);
return;
} else {
String idText = id.getText().trim();
if (idText.length() > 10) {
isSuccess = false;
JOptionPane.showMessageDialog(new JFrame(), "ID can only be a maximum of
10 numbers", "Error", JOptionPane.ERROR_MESSAGE);
}
}

if (!isSuccess) {
return;
}

if ("".equals(((JTextField) dob.getDateEditor().getUiComponent()).getText())) {
JOptionPane.showMessageDialog(new JFrame(), "Date of Birth is required", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}

if (blood.getSelectedIndex() == -1) {
JOptionPane.showMessageDialog(new JFrame(), "Blood Type is required", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}

if ("".equals(weight.getText())) {
JOptionPane.showMessageDialog(new JFrame(), "Weight is required", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
if ("".equals(height.getText())) {
JOptionPane.showMessageDialog(new JFrame(), "Height is required", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}

if ("".equals(head.getText())) {
JOptionPane.showMessageDialog(new JFrame(), "Head Circumference is required",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}

if (father.getText().trim().isEmpty()) {
JOptionPane.showMessageDialog(new JFrame(), "Father's Name is required",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}

if (mother.getText().trim().isEmpty()) {
JOptionPane.showMessageDialog(new JFrame(), "Mother's Name is required",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}

if (address.getText().trim().isEmpty()) {
JOptionPane.showMessageDialog(new JFrame(), "Address is required", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}

if (wob.getSelectedIndex() == -1) {
JOptionPane.showMessageDialog(new JFrame(), "Delivery of Birth is required",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}

if (!containsOnlyLetters(name.getText().trim())) {
isSuccess = false;
JOptionPane.showMessageDialog(new JFrame(), "Name must contain only letters",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}

if (!containsOnlyNumbers(id.getText().trim())) {
isSuccess = false;
JOptionPane.showMessageDialog(new JFrame(), "ID must contain only numbers",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}

if (!containsOnlyNumbers(weight.getText().trim())) {
isSuccess = false;
JOptionPane.showMessageDialog(new JFrame(), "Weight must contain only
numbers", "Error", JOptionPane.ERROR_MESSAGE);
return;
}

if (!containsOnlyNumbers(height.getText().trim())) {
isSuccess = false;
JOptionPane.showMessageDialog(new JFrame(), "Height must contain only
numbers", "Error", JOptionPane.ERROR_MESSAGE);
return;
}

if (!containsOnlyNumbers(head.getText().trim())) {
isSuccess = false;
JOptionPane.showMessageDialog(new JFrame(), "Head Circumference must contain
only numbers", "Error", JOptionPane.ERROR_MESSAGE);
return;
}

if (!containsOnlyLetters(father.getText().trim())) {
isSuccess = false;
JOptionPane.showMessageDialog(new JFrame(), "Father's name must contain only
letters", "Error", JOptionPane.ERROR_MESSAGE);
return;
}

if (!containsOnlyLetters(mother.getText().trim())) {
isSuccess = false;
JOptionPane.showMessageDialog(new JFrame(), "Mother's name must contain only
letters", "Error", JOptionPane.ERROR_MESSAGE);
return;
}

// Set parameters and execute the query


ps.setString(3, ((JTextField) dob.getDateEditor().getUiComponent()).getText());
ps.setString(1, name.getText());
ps.setInt(2, Integer.parseInt(id.getText()));
ps.setString(4, blood.getSelectedItem().toString());
ps.setInt(5, Integer.parseInt(weight.getText()));
ps.setInt(6, Integer.parseInt(height.getText()));
ps.setInt(7, Integer.parseInt(head.getText()));
ps.setString(9, father.getText());
ps.setString(10, mother.getText());
ps.setString(11, address.getText());
ps.setString(8, wob.getSelectedItem().toString());

try {
ps.executeUpdate();
JOptionPane.showMessageDialog(null, "Data Saved!");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "ID must be unique.", "Error",
JOptionPane.ERROR_MESSAGE);
}
String selectSql = "SELECT * FROM inputdata";
try (Statement st = conn.createStatement(); ResultSet rs =
st.executeQuery(selectSql)) {
String[] columnNames = {"Name", "ID", "Date of Birth", "Blood Type",
"Weight (kg)", "Height (cm)", "Head Circumference (cm)", "Delivery of Birth", "Father's
Name", "Mother's Name", "Address"};
model = new DefaultTableModel(columnNames, 0);

while (rs.next()) {
Object[] obj = {
rs.getString("Name"),
rs.getString("ID"),
rs.getString("Day"),
rs.getString("Blood"),
rs.getString("Weight"),
rs.getString("Height"),
rs.getString("Head"),
rs.getString("Way"),
rs.getString("Father"),
rs.getString("Mother"),
rs.getString("Address")
};
model.addRow(obj);
}

tbl_add_data.setModel(model);
tbl_add_data.setDefaultEditor(Object.class, null);
tbl_add_data.setRowSelectionAllowed(true);
tbl_add_data.setColumnSelectionAllowed(false);
}

name.setText("");
id.setText("");
dob.setDate(null);
blood.setSelectedIndex(-1);
weight.setText("");
height.setText("");
head.setText("");
father.setText("");
mother.setText("");
address.setText("");
wob.setSelectedIndex(-1);

System.out.println("Text fields cleared after adding data.");


}
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
e.printStackTrace();
}

private void deleteActionPerformed(java.awt.event.ActionEvent evt) {


DefaultTableModel model = (DefaultTableModel) tbl_add_data.getModel();
int selectedRow = tbl_add_data.getSelectedRow();

if (selectedRow != -1) {
// Get the ID of the selected row
String selectedIDString = model.getValueAt(selectedRow, 1).toString();

try {
Class.forName("com.mysql.cj.jdbc.Driver");
try (Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "")) {
// Delete data from the database
String sql = "DELETE FROM inputdata WHERE ID=?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, selectedIDString);
int affectedRows = ps.executeUpdate();

if (affectedRows > 0) {
// Remove the selected row from the table
model.removeRow(selectedRow);

// Re-enable the delete button


delete.setEnabled(true);

JOptionPane.showMessageDialog(this, "Record deleted successfully",


"Success", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "Failed to delete record", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
} catch (SQLException | ClassNotFoundException e) {
JOptionPane.showMessageDialog(this, "Error: " + e.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
}
} else {
// If no row is selected, show a message
JOptionPane.showMessageDialog(this, "Please select a row to delete", "No Row
Selected", JOptionPane.WARNING_MESSAGE);
}

}
private void updateActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel tblModel = (DefaultTableModel) tbl_add_data.getModel();
int selectedRow = tbl_add_data.getSelectedRow();

if (selectedRow == -1) {
JOptionPane.showMessageDialog(this, "Please select a single row", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}

String n = name.getText();
String a = (String) blood.getSelectedItem();
String ad = address.getText();
String hea = head.getText();
String hei = height.getText();
String wei = weight.getText();
String fa = father.getText();
String mo = mother.getText();
String iid = id.getText();
String wobb = (String) wob.getSelectedItem();
String dobb = ((JTextField) dob.getDateEditor().getUiComponent()).getText();

// Perform validation checks


if (n.isEmpty() || a == null || ad.isEmpty() || hea.isEmpty() || hei.isEmpty() || wei.isEmpty()
|| fa.isEmpty() || mo.isEmpty() || iid.isEmpty() || wobb == null) {
JOptionPane.showMessageDialog(this, "Please fill in all fields", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}

if (iid.length() > 11 || !iid.matches("\\d+")) {


JOptionPane.showMessageDialog(this, "ID must be numeric and have a maximum
length of 10 numbers", "Error", JOptionPane.ERROR_MESSAGE);
return;
}

if (!containsOnlyLetters(n) || !containsOnlyLetters(fa) || !containsOnlyLetters(mo)) {


JOptionPane.showMessageDialog(this, "Name, Mother's Name, and Father's Name must
contain only letters", "Error", JOptionPane.ERROR_MESSAGE);
return;
}

try {
// Check if weight, height, and head circumference are numeric
double weightValue = Double.parseDouble(wei);
double heightValue = Double.parseDouble(hei);
double headValue = Double.parseDouble(hea);

// Get the ID of the selected row


String selectedID = tblModel.getValueAt(selectedRow, 1).toString();

// Execute SQL update query to update only the selected row in the database
String updateQuery = "UPDATE inputdata SET Name=?, ID=?, Day=?, Blood=?,
Weight=?, Height=?, Head=?, Way=?, Father=?, Mother=?, Address=? WHERE ID=?";
try (Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
PreparedStatement ps = conn.prepareStatement(updateQuery)) {
ps.setString(1, n);
ps.setString(2, iid);
ps.setString(3, dobb);
ps.setString(4, a);
ps.setDouble(5, Double.parseDouble(wei));
ps.setDouble(6, Double.parseDouble(hei));
ps.setDouble(7, Double.parseDouble(hea));
ps.setString(8, wobb);
ps.setString(9, fa);
ps.setString(10, mo);
ps.setString(11, ad);
ps.setString(12, selectedID); // Use ID to identify the record to update

int rowsAffected = ps.executeUpdate();

if (rowsAffected == 1) {
// Update the table model with the new values for the selected row
tblModel.setValueAt(n, selectedRow, 0);
tblModel.setValueAt(iid, selectedRow, 1);
tblModel.setValueAt(dobb, selectedRow, 2);
tblModel.setValueAt(a, selectedRow, 3);
tblModel.setValueAt(wei, selectedRow, 4);
tblModel.setValueAt(hei, selectedRow, 5);
tblModel.setValueAt(hea, selectedRow, 6);
tblModel.setValueAt(wobb, selectedRow, 7);
tblModel.setValueAt(fa, selectedRow, 8);
tblModel.setValueAt(mo, selectedRow, 9);
tblModel.setValueAt(ad, selectedRow, 10);
name.setText("");
id.setText("");
dob.setDate(null);
blood.setSelectedIndex(-1);
weight.setText("");
height.setText("");
head.setText("");
father.setText("");
mother.setText("");
address.setText("");
wob.setSelectedIndex(-1);

JOptionPane.showMessageDialog(this, "Data has been UPDATED");


} else if (rowsAffected > 1) {
JOptionPane.showMessageDialog(this, "Multiple rows updated. Ensure ID
uniqueness.", "Error", JOptionPane.ERROR_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "No rows updated. ID not found.", "Error",
JOptionPane.ERROR_MESSAGE);
}

}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "Please enter valid numeric values for weight,
height, and head circumference", "Error", JOptionPane.ERROR_MESSAGE);
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Error updating data: " + e.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}

private void refreshActionPerformed(java.awt.event.ActionEvent evt) {


DefaultTableModel model;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root", "");
// Retrieve data from the database
String selectSql = "SELECT * FROM inputdata";
try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(selectSql))
{
// Initialize table model
String[] columnNames = {"Name", "ID", "Date of Birth", "Blood Type", "Weight (kg)",
"Height (cm)", "Head Circumference (cm)", "Delivery of Birth", "Father's Name", "Mother's
Name", "Address"};
model = new DefaultTableModel(columnNames, 0);
// Add retrieved data to the table model
while (rs.next()) {
Object[] obj = {
rs.getString("Name"),
rs.getString("ID"),
rs.getString("Day"),
rs.getString("Blood"),
rs.getString("Weight"),
rs.getString("Height"),
rs.getString("Head"),
rs.getString("Way"),
rs.getString("Father"),
rs.getString("Mother"),
rs.getString("Address")
};
model.addRow(obj);
}

// Set the model to the table


tbl_add_data.setModel(model);
for(int i=0;i<tbl_add_data.getColumnCount();i++){
Class<?> column=tbl_add_data.getColumnClass(i);
tbl_add_data.setDefaultEditor(column,null);
}
}
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(mainpage.class.getName()).log(Level.SEVERE, null, ex);
}

}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
close();
}

private void weightActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
}

private void idActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
}

private void heightActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
}

private void fatherActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
}

private void bloodActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
}

private void recapActionPerformed(java.awt.event.ActionEvent evt) {


this.dispose();
endpage end=new endpage();
end.setVisible(true);
DefaultTableModel tblModel = (DefaultTableModel)tbl_add_data.getModel();

/**
* @param args the command line arguments
*/
public static void mainpage(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and
feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(mainpage.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(mainpage.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(mainpage.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(mainpage.class.getName()).log(java.util.logging.Level.S
EVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new mainpage().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JButton add;
private javax.swing.JTextField address;
private javax.swing.JComboBox<String> blood;
private javax.swing.JButton delete;
private com.toedter.calendar.JDateChooser dob;
private javax.swing.JTextField father;
private javax.swing.JTextField head;
private javax.swing.JTextField height;
private javax.swing.JTextField id;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel12;
private javax.swing.JLabel jLabel13;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField mother;
private javax.swing.JTextField name;
private javax.swing.JButton recap;
private javax.swing.JButton refresh;
private javax.swing.JTable tbl_add_data;
private javax.swing.JButton update;
private javax.swing.JTextField weight;
private javax.swing.JComboBox<String> wob;
// End of variables declaration

}
End Page Code
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change
this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this
template
*/
package childrenform.java;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import javax.swing.RowFilter;
import javax.swing.table.TableRowSorter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.awt.HeadlessException;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
/**
*
* @author latifahdrupadi
*/
public class endpage extends javax.swing.JFrame {
/**
* Creates new form endpage
*/
public endpage() {
initComponents();
}
public void close(){
WindowEvent closeWindow=new
WindowEvent(this,WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(closeWindow);
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();


jScrollPane1 = new javax.swing.JScrollPane();
tbl_add_data = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
pdf = new javax.swing.JButton();
close = new javax.swing.JButton();
search = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setBackground(new java.awt.Color(0, 153, 153));

tbl_add_data.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null, null, null}
},
new String [] {
"Name", "ID", "Date of Birth", "Blood Type", "Weight (kg)", "Height (cm)", "Head
Circumference", "Delivery of Birth", "Father's Name", "Mother's Name", "Address"
}
){
boolean[] canEdit = new boolean [] {
false, false, false, false, false, false, false, false, false, false, false
};

public boolean isCellEditable(int rowIndex, int columnIndex) {


return canEdit [columnIndex];
}
});
jScrollPane1.setViewportView(tbl_add_data);

jButton1.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N


jButton1.setText("Input Data");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
pdf.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N
pdf.setText("Generate PDF");
pdf.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
pdfActionPerformed(evt);
}
});

close.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N


close.setText("Close");
close.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
closeActionPerformed(evt);
}
});

search.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
searchKeyReleased(evt);
}
});

jLabel1.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N


jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setText("Search");

jButton3.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N


jButton3.setText("Show Data");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setFont(new java.awt.Font("Arial Black", 0, 18)); // NOI18N
jButton4.setText("Log Out");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});

jLabel2.setFont(new java.awt.Font("Sinhala MN", 1, 36)); // NOI18N


jLabel2.setForeground(new java.awt.Color(255, 255, 255));
jLabel2.setText("Posyandu Data Program");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);


jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(65, 65, 65)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILI
NG)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton1)
.addGap(44, 44, 44)
.addComponent(pdf)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton4)
.addGap(34, 34, 34)
.addComponent(close))
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING,
jPanel1Layout.createSequentialGroup()
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(search, javax.swing.GroupLayout.PREFERRED_SIZE, 176,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel1)))
.addGap(65, 65, 65))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(416, 416, 416)
.addComponent(jLabel2)
.addContainerGap(416, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2)
.addGap(48, 48, 48)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(jButton3)
.addComponent(search, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 487,
Short.MAX_VALUE)
.addGap(18, 18, 18)

.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(jButton1)
.addComponent(pdf)
.addComponent(close)
.addComponent(jButton4))
.addGap(27, 27, 27))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);

pack();
setLocationRelativeTo(null);
}// </editor-fold>

private void closeActionPerformed(java.awt.event.ActionEvent evt) {


close();
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {


DefaultTableModel model;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
"root", "");
// Retrieve data from the database
String selectSql = "SELECT * FROM inputdata";
try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(selectSql))
{
// Initialize table model
String[] columnNames = {"Name", "ID", "Date of Birth", "Blood Type", "Weight (kg)",
"Height (cm)", "Head Circumference (cm)", "Delivery of Birth", "Father's Name", "Mother's
Name", "Address"};
model = new DefaultTableModel(columnNames, 0);

// Add retrieved data to the table model


while (rs.next()) {
Object[] obj = {
rs.getString("Name"),
rs.getString("ID"),
rs.getString("Day"),
rs.getString("Blood"),
rs.getString("Weight"),
rs.getString("Height"),
rs.getString("Head"),
rs.getString("Way"),
rs.getString("Father"),
rs.getString("Mother"),
rs.getString("Address")
};
model.addRow(obj);
}

// Set the model to the table


tbl_add_data.setModel(model);
for(int i=0;i<tbl_add_data.getColumnCount();i++){
Class<?> column=tbl_add_data.getColumnClass(i);
tbl_add_data.setDefaultEditor(column,null);
}
}
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(mainpage.class.getName()).log(Level.SEVERE, null, ex);
}
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {


this.dispose();
loginpage log=new loginpage();
log.setVisible(true);
}

private void pdfActionPerformed(java.awt.event.ActionEvent evt) {


Document document = new Document();

try {
PdfWriter.getInstance(document, new FileOutputStream("Children Data.pdf"));
document.open();

PdfPTable table = new PdfPTable(11); // Adjust the number of columns accordingly

// Add column headers


table.addCell("Name");
table.addCell("ID");
table.addCell("Blood Type");
table.addCell("Date of Birth");
table.addCell("Weight");
table.addCell("Height");
table.addCell("Head Circumference");
table.addCell("Delivery of Birth");
table.addCell("Father's Name");
table.addCell("Mother's Name");
table.addCell("Address");

// Fetch data from the database


Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
String query = "SELECT * FROM inputdata";
PreparedStatement preparedStatement = connection.prepareStatement(query);
ResultSet resultSet = preparedStatement.executeQuery();

// Add data to the PDF table


while (resultSet.next()) {
table.addCell(resultSet.getString("Name"));
table.addCell(resultSet.getString("ID"));
table.addCell(resultSet.getString("Blood"));
table.addCell(resultSet.getString("Day"));
table.addCell(resultSet.getString("Weight"));
table.addCell(resultSet.getString("Height"));
table.addCell(resultSet.getString("Head"));
table.addCell(resultSet.getString("Way"));
table.addCell(resultSet.getString("Father"));
table.addCell(resultSet.getString("Mother"));
table.addCell(resultSet.getString("Address"));
}
document.add(table);
JOptionPane.showMessageDialog(this, "PDF has been SAVED");
} catch (DocumentException | HeadlessException | FileNotFoundException |
SQLException e) {
} finally {
document.close();
}
}
private void searchKeyReleased(java.awt.event.KeyEvent evt) {
DefaultTableModel model = (DefaultTableModel) tbl_add_data.getModel();
TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<>(model);
tbl_add_data.setRowSorter(sorter); // Set the sorter to the table
String text = search.getText().trim(); // Trim to remove leading/trailing spaces
try {
// Try to parse the input as a number
double number = Double.parseDouble(text);

// Use a custom RowFilter for numeric values


RowFilter<DefaultTableModel, Object> filter = new RowFilter<DefaultTableModel,
Object>() {
@Override
public boolean include(RowFilter.Entry<? extends DefaultTableModel, ? extends
Object> entry) {
for (int i = 0; i < entry.getValueCount(); i++) {
try {
// Try to parse the cell value as a number
double cellValue = Double.parseDouble(entry.getStringValue(i));

// Compare numeric values


if (cellValue == number) {
return true;
}
} catch (NumberFormatException e) {
// Ignore non-numeric values
}
}
return false;
}
};

sorter.setRowFilter(filter);
} catch (NumberFormatException e) {
// If the input is not a number, use RowFilter.regexFilter for text
sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text)); // (?i) for case-insensitive
matching
}
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {


this.dispose();
mainpage main=new mainpage();
main.setVisible(true);
}

/**
* @param args the command line arguments
*/
public static void endpage(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and
feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(endpage.class.getName()).log(java.util.logging.Level.SE
VERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(endpage.class.getName()).log(java.util.logging.Level.SE
VERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(endpage.class.getName()).log(java.util.logging.Level.SE
VERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(endpage.class.getName()).log(java.util.logging.Level.SE
VERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new endpage().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JButton close;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton pdf;
private javax.swing.JTextField search;
private javax.swing.JTable tbl_add_data;
// End of variables declaration
}

You might also like