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

CRUD NETBEANS With XAMPP

This document contains code for a Java class that creates a login page application. It includes code to connect to a MySQL database, retrieve username and password from a table, and check login credentials. If login is successful, it opens a menu page class. The menu page class code displays student data from the database in a table, and allows users to insert, update and delete student records by clicking buttons that call SQL queries.

Uploaded by

Lloydy Lloydy
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)
193 views

CRUD NETBEANS With XAMPP

This document contains code for a Java class that creates a login page application. It includes code to connect to a MySQL database, retrieve username and password from a table, and check login credentials. If login is successful, it opens a menu page class. The menu page class code displays student data from the database in a table, and allows users to insert, update and delete student records by clicking buttons that call SQL queries.

Uploaded by

Lloydy Lloydy
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/ 11

JAVA CLASS

package week10_cc103_demo;

import java.sql.*; //Importing java.sql package


public class Week10_CC103_Demo {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost/javaloginpage", "root", "");//Establishing
connection
System.out.println("Connected With the database successfully"); //Message after
successful connection

} catch (SQLException e) {

System.out.println("Error while connecting to the database"); //Message if something goes


wrong while conneting to the database

}
}
}
package week10_cc103_demo;

import java.sql.*;
import javax.swing.JOptionPane;

public class LoginPage extends javax.swing.JFrame {

public LoginPage() {
initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();


jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
un = new javax.swing.JTextField();
pass = new javax.swing.JPasswordField();
submit = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(new java.awt.Color(204, 204, 255));
getContentPane().setLayout(null);

jLabel1.setFont(new java.awt.Font("Gill Sans MT", 1, 24)); // NOI18N


jLabel1.setForeground(new java.awt.Color(51, 51, 255));
jLabel1.setText("WELCOME TO LOGIN PAGE");
getContentPane().add(jLabel1);
jLabel1.setBounds(100, 30, 410, 38);

jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N


jLabel2.setText("Username: ");
getContentPane().add(jLabel2);
jLabel2.setBounds(106, 118, 78, 17);

jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N


jLabel3.setText("Password:");
getContentPane().add(jLabel3);
jLabel3.setBounds(106, 156, 72, 17);
getContentPane().add(un);
un.setBounds(221, 118, 130, 20);
getContentPane().add(pass);
pass.setBounds(221, 156, 130, 20);

submit.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N


submit.setForeground(new java.awt.Color(204, 0, 51));
submit.setText("Submit");
submit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
submitActionPerformed(evt);
}
});
getContentPane().add(submit);
submit.setBounds(200, 220, 130, 40);

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

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


// TODO add your handling code here:

String user=un.getText();
String pa=pass.getText();

try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost/javaloginpage","root","");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("Select * from tbl_username");
while(rs.next()){
String username = rs.getString(2);
String password = rs.getString(3);

if (user.equals(username) && pa.equals(password)){


Menupage obj = new Menupage();
obj.setVisible(true);

}
else{
JOptionPane.showMessageDialog(null, "Username and password is incorrect" );
}
}con.close();
}
catch(Exception e ){
System.out.println(e);
}

}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {

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

// Variables declaration - do not modify


private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPasswordField pass;
private javax.swing.JButton submit;
private javax.swing.JTextField un;
// End of variables declaration
}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package week10_cc103_demo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
*
* @author QCU
*/
public class Menupage extends javax.swing.JFrame {

/**
* Creates new form Menupage
*/
public Menupage() {
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() {

jLabel1 = new javax.swing.JLabel();


jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
txtStudName = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
txtAge = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
btnShow = new javax.swing.JButton();
txtStudID = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);

jLabel1.setBackground(new java.awt.Color(153, 153, 255));


jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 0, 255));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Welcome to our Main Page");
getContentPane().add(jLabel1);
jLabel1.setBounds(200, 30, 324, 48);

jLabel2.setText("Student ID:");
getContentPane().add(jLabel2);
jLabel2.setBounds(202, 95, 56, 14);

jLabel3.setText("Student Name:");
getContentPane().add(jLabel3);
jLabel3.setBounds(202, 129, 72, 14);
getContentPane().add(txtStudName);
txtStudName.setBounds(292, 126, 143, 20);

jLabel4.setText("Age:");
getContentPane().add(jLabel4);
jLabel4.setBounds(202, 152, 23, 14);

txtAge.setName(""); // NOI18N
getContentPane().add(txtAge);
txtAge.setBounds(292, 157, 143, 20);

jButton1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N


jButton1.setText("INSERT");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(470, 90, 80, 23);

jButton2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N


jButton2.setText("UPDATE");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2);
jButton2.setBounds(470, 120, 81, 23);

jButton3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N


jButton3.setLabel("DELETE");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
getContentPane().add(jButton3);
jButton3.setBounds(470, 150, 80, 23);

jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {

},
new String [] {
"studentid", "studentname", "age"
}
));
jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jTable1MouseClicked(evt);
}
});
jScrollPane1.setViewportView(jTable1);

getContentPane().add(jScrollPane1);
jScrollPane1.setBounds(104, 224, 490, 181);

btnShow.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N


btnShow.setLabel("SHOW");
btnShow.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnShowActionPerformed(evt);
}
});
getContentPane().add(btnShow);
btnShow.setBounds(104, 195, 67, 23);

txtStudID.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtStudIDActionPerformed(evt);
}
});
getContentPane().add(txtStudID);
txtStudID.setBounds(292, 92, 143, 20);
txtStudID.getAccessibleContext().setAccessibleName("");

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

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

String name = txtStudName.getText();


int age = Integer.parseInt(txtAge.getText());

try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost/javaloginpage","root","");
Statement statement = con.createStatement();
String sql = ("INSERT INTO tbl_studrecord VALUES(" + txtStudID.getText() + ",'" +
txtStudName.getText() + "'," + txtAge.getText() + ")");
int rowsInserted = statement.executeUpdate(sql);
if (rowsInserted > 0) {

//display in table

DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); // for clear


model.setRowCount(0); // for clear

Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("Select * from tbl_studrecord");
while(rs.next()){
Object[] row = { rs.getString(1),rs.getString(2),rs.getString(3) };

model.addRow(row);
}

//message
JOptionPane.showMessageDialog(null, "Record inserted successfully...");

statement.close();
con.close();

txtStudID.setText("");
txtStudName.setText("");
txtAge.setText("");

} catch(Exception e ) {
System.out.println(e);
}
}

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {


DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel(); //create a model
int selectedRowIndex = jTable1.getSelectedRow(); //get selected row

String s_id = (String) dtm.getValueAt(selectedRowIndex, 0);


String s_name = (String) dtm.getValueAt(selectedRowIndex, 1);
String s_age = (String) dtm.getValueAt(selectedRowIndex, 2);

txtStudID.setText(s_id);
txtStudName.setText(s_name);
txtAge.setText(s_age);
}

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


DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.setRowCount(0);

try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost/javaloginpage","root","");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("Select * from tbl_studrecord");
while(rs.next()){
Object[] row = { rs.getString(1),rs.getString(2),rs.getString(3) };
model.addRow(row);
}
} catch(Exception x ) {
System.out.println(x);
}
}

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


try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost/javaloginpage","root","");
Statement statement = con.createStatement();

int id = Integer.parseInt(txtStudID.getText());
String name = txtStudName.getText();
int age = Integer.parseInt(txtAge.getText());
String sql2 = ("update tbl_studrecord set studentname='"+ name +"', age="+ age + " where
studentid="+id);
int rowsEdited = statement.executeUpdate(sql2);
if (rowsEdited > 0) {

//display in table
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.setRowCount(0);

Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("Select * from tbl_studrecord");
while(rs.next()){
Object[] row = { rs.getString(1),rs.getString(2),rs.getString(3) };
model.addRow(row);
}

//message
JOptionPane.showMessageDialog(null, "Record updated successfully...");

txtStudID.setText("");
txtStudName.setText("");
txtAge.setText("");

} catch(Exception x ) {
System.out.println(x);
}
}

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


try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost/javaloginpage","root","");
Statement statement = con.createStatement();

int id = Integer.parseInt(txtStudID.getText());
String sql3 = ("delete from tbl_studrecord where studentid="+id);
int rowsDel = statement.executeUpdate(sql3);
if (rowsDel > 0) {
//display in table
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.setRowCount(0);

Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("Select * from tbl_studrecord");
while(rs.next()){
Object[] row = { rs.getString(1),rs.getString(2),rs.getString(3) };
model.addRow(row);
}

txtStudID.setText(null);
txtStudName.setText(null);
txtAge.setText(null);
//message
JOptionPane.showMessageDialog(null, "Record deleted...");
}

} catch(Exception x ) {
System.out.println(x);
}
}

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


// TODO add your handling code here:
}

/**
* @param args the command line arguments
*/
public static void main(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(Menupage.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);
} catch (InstantiationException ex) {

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

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

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

/* Create and display the form */


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

// Variables declaration - do not modify


private javax.swing.JButton btnShow;
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.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField txtAge;
private javax.swing.JTextField txtStudID;
private javax.swing.JTextField txtStudName;
// End of variables declaration
}

You might also like