0% found this document useful (0 votes)
2 views6 pages

Class Lab Week 3

The document provides a guide for creating a simple login GUI using Java Swing in NetBeans, including the necessary code for the LoginApp class. It also outlines steps for setting up a MySQL database connection and performing basic CRUD operations in a StudentsApplication class. The document includes code snippets for inserting, reading, updating, and deleting student records from the database.

Uploaded by

tuhafenijason05
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)
2 views6 pages

Class Lab Week 3

The document provides a guide for creating a simple login GUI using Java Swing in NetBeans, including the necessary code for the LoginApp class. It also outlines steps for setting up a MySQL database connection and performing basic CRUD operations in a StudentsApplication class. The document includes code snippets for inserting, reading, updating, and deleting student records from the database.

Uploaded by

tuhafenijason05
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/ 6

1.

Graphical User Interface (GUI) - Login Page using Java Swing

To create a simple login GUI in NetBeans using Java Swing, follow these steps:

Steps:

1. Open NetBeans and create a new Java project.


2. Inside the project, create a new Java class (LoginApp.java).
3. Use JFrame, JLabel, JTextField, JPasswordField, and JButton for the UI
components.

Project structure

Code for Login GUI:

//LoginApp.java class
package loginapplication;

/**
*
* @author jimmy
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginApp extends JFrame {


private JTextField usernameField;
private JPasswordField passwordField;

public LoginApp() {
setTitle("Login Page");
setSize(350, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(3, 2, 5, 5));

JLabel userLabel = new JLabel("Username:");


usernameField = new JTextField(15);

JLabel passLabel = new JLabel("Password:");


passwordField = new JPasswordField(15);

JButton loginButton = new JButton("Login");

// Login button action listener


loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());

// Hardcoded credentials
if (username.isEmpty() || password.isEmpty()) {
JOptionPane.showMessageDialog(null, "Fields cannot be
empty!", "Error", JOptionPane.ERROR_MESSAGE);
} else if (username.equals("user") &&
password.equals("pass")) {
JOptionPane.showMessageDialog(null, "Login
Successful!", "Success", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "Invalid
Credentials!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});

add(userLabel);
add(usernameField);
add(passLabel);
add(passwordField);
add(new JLabel()); // Placeholder
add(loginButton);

setLocationRelativeTo(null);
setVisible(true);
}
}

// LoginApplication.java main class (where main method is)


package loginapplication;

/**
*
* @author jimmy
*/
public class LoginApplication {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new LoginApp();
}
}
2. Database Programming - Java and MySQL

Steps to Set Up Database Connection

1. Install MySQL (if not installed).


2. Create a database and table in MySQL:

3. Download MySQL JDBC Driver and add it to your NetBeans project (mysql-
connector-java-x.x.x.jar).

Go to the official MySQL Connector/J download page:

https://dev.mysql.com/downloads/connector/j/

Select Platform Independent and download the zip or tar.gz file.

Extract the downloaded file and locate the mysql-connector-java-x.x.x.jar file.


Project Structure:

4. Create a Java project (StudentsApplication.java) to handle database


operations.

Code for Database Operations:

// StudentsApplication.java class with database


import java.sql.*;

public class StudentsApplication {


private static final String URL =
"jdbc:mysql://localhost:3306/student_db";
private static final String USER = "root"; // Change this if needed
private static final String PASSWORD = ""; // Change this if needed

// Establish connection
public static Connection connect() {
try {
return DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}

// 1. Insert Student
public static void insertStudent(String studentNumber, String name, int
mark) {
String query = "INSERT INTO student (student_number, name, mark)
VALUES (?, ?, ?)";
try (Connection conn = connect(); PreparedStatement pstmt =
conn.prepareStatement(query)) {
pstmt.setString(1, studentNumber);
pstmt.setString(2, name);
pstmt.setInt(3, mark);
pstmt.executeUpdate();
System.out.println("Student added successfully!");
} catch (SQLException e) {
e.printStackTrace();
}
}

// 2. Read Students
public static void readStudents() {
String query = "SELECT * FROM student";
try (Connection conn = connect(); Statement stmt =
conn.createStatement(); ResultSet rs = stmt.executeQuery(query)) {
while (rs.next()) {
System.out.println("ID: " + rs.getInt("id") + ", Student
Number: " + rs.getString("student_number") + ", Name: " +
rs.getString("name") + ", Mark: " + rs.getInt("mark"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}

// 3. Update Student
public static void updateStudent(String studentNumber, String newName,
int newMark) {
String query = "UPDATE student SET name = ?, mark = ? WHERE
student_number = ?";
try (Connection conn = connect(); PreparedStatement pstmt =
conn.prepareStatement(query)) {
pstmt.setString(1, newName);
pstmt.setInt(2, newMark);
pstmt.setString(3, studentNumber);
int updatedRows = pstmt.executeUpdate();
if (updatedRows > 0) {
System.out.println("Student updated successfully!");
} else {
System.out.println("Student not found!");
}
} catch (SQLException e) {
e.printStackTrace();
}
}

// 4. Delete Student
public static void deleteStudent(String studentNumber) {
String query = "DELETE FROM student WHERE student_number = ?";
try (Connection conn = connect(); PreparedStatement pstmt =
conn.prepareStatement(query)) {
pstmt.setString(1, studentNumber);
int deletedRows = pstmt.executeUpdate();
if (deletedRows > 0) {
System.out.println("Student deleted successfully!");
} else {
System.out.println("Student not found!");
}
} catch (SQLException e) {
e.printStackTrace();
}
}

// Run logic
public static void main(String[] args) {
insertStudent("S001", "John Doe", 83);
insertStudent("S002", "Jane Smith", 95);
readStudents();
updateStudent("S001", "Johnathan Doe", 90);
deleteStudent("S001");
readStudents();
}
}

You might also like