Java code to create and manage phishing attack database

Start
				
					import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;

public class PhishingDatabaseGUI extends JFrame implements ActionListener{
    // GUI components
    private JButton bCreate, bPopulate, bManage;
    private JPanel p1, p2;
    // Database connection
    private static Connection conn;
    
    // Constructor
    public PhishingDatabaseGUI(){
        // Create the database connection
        conn = createConnection();
        
        // Set frame properties
        setTitle("Phishing Database GUI");
        setSize(400, 150);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // Create buttons
        bCreate = new JButton("Create Database");
        bCreate.addActionListener(this);
        bPopulate = new JButton("Populate Tables");
        bPopulate.addActionListener(this);
        bManage = new JButton("Manage Data");
        bManage.addActionListener(this);
        
        // Create JPanels
        p1 = new JPanel();
        p2 = new JPanel();
        
        // Add components to panels
        p1.add(bCreate);
        p1.add(bPopulate);
        p2.add(bManage);
        
        // Add panels to frame
        add(p1, BorderLayout.NORTH);
        add(p2, BorderLayout.SOUTH);
        
        // Make frame visible
        setVisible(true);
    }
    
    // Create database connection
    private static Connection createConnection(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost/Phishing_Database";
            String username = "root";
            String password = "";
            Connection conn = DriverManager.getConnection(url, username, password);
            System.out.println("Database connection established");
            return conn;
        } 
        catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }
    
    // Button handler
    public void actionPerformed(ActionEvent e){
        if(e.getSource() == bCreate){
            // Create database tables
            createTables();
        }
        else if(e.getSource() == bPopulate){
            // Populate database tables
            populateTables();
        }
        else if(e.getSource() == bManage){
            // Open new GUI to manage database data
            ManageDataGUI mdg = new ManageDataGUI();
        }
    }
    
    // Create database tables
    private static void createTables(){
        try{
            // Create statement
            Statement st = conn.createStatement();
            
            // Create Users table
            String createUsersTable = "CREATE TABLE IF NOT EXISTS Users (userID INT NOT NULL AUTO_INCREMENT, username VARCHAR(50) NOT NULL, password VARCHAR(50) NOT NULL, PRIMARY KEY(userID))";
            st.executeUpdate(createUsersTable);
            System.out.println("Users table created");
            
            // Create Phishing_Data table
            String createPhishDataTable = "CREATE TABLE IF NOT EXISTS Phishing_Data (attackID INT NOT NULL AUTO_INCREMENT, url TEXT NOT NULL, attackType VARCHAR(50) NOT NULL, PRIMARY KEY(attackID))";
            st.executeUpdate(createPhishDataTable);
            System.out.println("Phishing_Data table created");
            
            // Create Attack_History table
            String createAttackHistoryTable = "CREATE TABLE IF NOT EXISTS Attack_History (attackID INT NOT NULL, userID INT NOT NULL, FOREIGN KEY(attackID) REFERENCES Phishing_Data(attackID), FOREIGN KEY(userID) REFERENCES Users(userID))";
            st.executeUpdate(createAttackHistoryTable);
            System.out.println("Attack_History table created");
            
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
    
    // Populate database tables
    private static void populateTables(){
        try{
            // Create statement
            Statement st = conn.createStatement();
            
            // Populate Users table
            String populateUsersTable = "INSERT INTO Users (username, password) VALUES ('John', 'password123'), ('Mary', 'password456'), ('Bob', 'password789')";
            st.executeUpdate(populateUsersTable);
            System.out.println("Users table populated");
            
            // Populate Phishing_Data table
            String populatePhishDataTable = "INSERT INTO Phishing_Data (url, attackType) VALUES ('www.example.com', 'Phishing'), ('www.example2.com', 'Malware'), ('www.example3.com', 'Spam')";
            st.executeUpdate(populatePhishDataTable);
            System.out.println("Phishing_Data table populated");
            
            // Populate Attack_History table
            String populateAttackHistoryTable = "INSERT INTO Attack_History (attackID, userID) VALUES (1, 1), (2, 3), (3, 2)";
            st.executeUpdate(populateAttackHistoryTable);
            System.out.println("Attack_History table populated");
            
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
    
    // Main method
    public static void main(String[] args) {
        new PhishingDatabaseGUI();
    }
}

// Manage Data GUI
class ManageDataGUI extends JFrame implements ActionListener{
    // GUI components
    private JButton bViewUsers, bViewPhishData, bViewAttackHistory;
    private JPanel p1;
    private JTextArea taData;
    // Database connection
    private static Connection conn;
    
    // Constructor
    public ManageDataGUI(){
        // Create the database connection
        conn = createConnection();
        
        // Set frame properties
        setTitle("Manage Data");
        setSize(400, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
        // Create buttons
        bViewUsers = new JButton("View Users");
        bViewUsers.addActionListener(this);
        bViewPhishData = new JButton("View Phishing Data");
        bViewPhishData.addActionListener(this);
        bViewAttackHistory = new JButton("View Attack History");
        bViewAttackHistory.addActionListener(this);
        
        // Create JPanel
        p1 = new JPanel();
        
        // Add components to panel
        p1.add(bViewUsers);
        p1.add(bViewPhishData);
        p1.add(bViewAttackHistory);
        
        // Create JTextArea
        taData = new JTextArea();
        
        // Add components to frame
        add(p1, BorderLayout.NORTH);
        add(taData, BorderLayout.CENTER);
        
        // Make frame visible
        setVisible(true);
    }
    
    // Create database connection
    private static Connection createConnection(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost/Phishing_Database";
            String username = "root";
            String password = "";
            Connection conn = DriverManager.getConnection(url, username, password);
            System.out.println("Database connection established");
            return conn;
        } 
        catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }
    
    // Button handler
    public void actionPerformed(ActionEvent e){
        if(e.getSource() == bViewUsers){
            // View Users table
            viewUsers();
        }
        else if(e.getSource() == bViewPhishData){
            // View Phishing_Data table
            viewPhishData();
        }
        else if(e.getSource() == bViewAttackHistory){
            // View Attack_History table
            viewAttackHistory();
        }
    }
    
    // View Users table
    private static void viewUsers(){
        try{
            // Create statement
            Statement st = conn.createStatement();
            
            // Execute query
            ResultSet rs = st.executeQuery("SELECT * FROM Users");
            
            // Create string to store data
            String data = "";
            
            // Loop through result set
            while(rs.next()){
                data += "UserID: " + rs.getInt(1) + " Username: " + rs.getString(2) + " Password: " + rs.getString(3) + "\n";
            }
            
            // Display data
            taData.setText(data);
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
    
    // View Phishing_Data table
    private static void viewPhishData(){
        try{
            // Create statement
            Statement st = conn.createStatement();
            
            // Execute query
            ResultSet rs = st.executeQuery("SELECT * FROM Phishing_Data");
            
            // Create string to store data
            String data = "";
            
            // Loop through result set
            while(rs.next()){
                data += "AttackID: " + rs.getInt(1) + " URL: " + rs.getString(2) + " Attack Type: " + rs.getString(3) + "\n";
            }
            
            // Display data
            taData.setText(data);
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
    
    // View Attack_History table
    private static void viewAttackHistory(){
        try{
            // Create statement
            Statement st = conn.createStatement();
            
            // Execute query
            ResultSet rs = st.executeQuery("SELECT * FROM Attack_History");
            
            // Create string to store data
            String data = "";
            
            // Loop through result set
            while(rs.next()){
                data += "AttackID: " + rs.getInt(1) + " UserID: " + rs.getInt(2) + "\n";
            }
            
            // Display data
            taData.setText(data);
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
}
				
			
Previous Story

Duben’s adventurous trip to Borneo

Next Story

Rebecca’s romance in Los Angeles