Class Lab Week 3
Class Lab Week 3
To create a simple login GUI in NetBeans using Java Swing, follow these steps:
Steps:
Project structure
//LoginApp.java class
package loginapplication;
/**
*
* @author jimmy
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public LoginApp() {
setTitle("Login Page");
setSize(350, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(3, 2, 5, 5));
// 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);
}
}
/**
*
* @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
3. Download MySQL JDBC Driver and add it to your NetBeans project (mysql-
connector-java-x.x.x.jar).
https://dev.mysql.com/downloads/connector/j/
// 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();
}
}