Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Getting an Error main.java:7: error: cannot find symbol

wtolentinoNov 21 2024

I am new to Java and trying to compile this code but run into these errors:

ava -cp .;C:\app\client\wtolentino\product\19.0.0\client_1\jdbc\lib\ojdbc8.jar main.java
main.java:7: error: cannot find symbol
                new LoginScreen();

main.java


import javax.swing.*;

public class Main {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new LoginScreen();
            }
        });
    }
}

LoginScreen.java


import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class LoginScreen extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;
    public static String user;
    public static String password;

    public LoginScreen() {
        setTitle("Login");
        setSize(300, 150);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        add(panel);
        placeComponents(panel);

        setVisible(true);
    }

    private void placeComponents(JPanel panel) {
        panel.setLayout(null);

        JLabel userLabel = new JLabel("User:");
        userLabel.setBounds(10, 20, 80, 25);
        panel.add(userLabel);

        usernameField = new JTextField(20);
        usernameField.setBounds(100, 20, 165, 25);
        panel.add(usernameField);

        JLabel passwordLabel = new JLabel("Password:");
        passwordLabel.setBounds(10, 50, 80, 25);
        panel.add(passwordLabel);

        passwordField = new JPasswordField(20);
        passwordField.setBounds(100, 50, 165, 25);
        panel.add(passwordField);

        JButton loginButton = new JButton("Login");
        loginButton.setBounds(10, 80, 80, 25);
        panel.add(loginButton);

        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String userInput = usernameField.getText();
                String passInput = new String(passwordField.getPassword());

                if (authenticate(userInput, passInput)) {
                    dispose();
                    new DataEntryScreen();
                } else {
                    JOptionPane.showMessageDialog(null, "Invalid login. Please try again.");
                }
            }
        });
    }

    private boolean authenticate(String userInput, String passInput) {
        user = userInput;
        password = passInput;
        String url = "jdbc:oracle:thin:@//host:1521/DEV"

        try (Connection conn = DriverManager.getConnection(url, user, password)) {
            return conn != null;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}

DataEntryScreen.java

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


public class DataEntryScreen extends JFrame {
    private JComboBox<String> tableDropdown;
    private JTextField usernameField;
    private JTextField firstNameField;
    private JTextField middleNameField;
    private JTextField lastNameField;
    private JTextField emailField;
    private JTable dataTable;

    public DataEntryScreen() {
        setTitle("User Account Reference Data Entry");
        setSize(800, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        add(panel);
        placeComponents(panel);

        setVisible(true);
    }

    private void placeComponents(JPanel panel) {
        panel.setLayout(null);

        JLabel tableLabel = new JLabel("Select Table:");
        tableLabel.setBounds(10, 10, 100, 25);
        panel.add(tableLabel);

        tableDropdown = new JComboBox<>();
        tableDropdown.setBounds(120, 10, 200, 25);
        loadTables(); // Load table names dynamically
        panel.add(tableDropdown);

        JButton showDataButton = new JButton("Show Data");
        showDataButton.setBounds(330, 10, 120, 25);
        panel.add(showDataButton);
        showDataButton.addActionListener(e -> showTableData());

        JLabel usernameLabel = new JLabel("Username:");
        usernameLabel.setBounds(10, 50, 100, 25);
        panel.add(usernameLabel);

        usernameField = new JTextField(20);
        usernameField.setBounds(120, 50, 165, 25);
        panel.add(usernameField);

        JLabel firstNameLabel = new JLabel("First Name:");
        firstNameLabel.setBounds(10, 90, 100, 25);
        panel.add(firstNameLabel);

        firstNameField = new JTextField(20);
        firstNameField.setBounds(120, 90, 165, 25);
        panel.add(firstNameField);

        JLabel middleNameLabel = new JLabel("Middle Name:");
        middleNameLabel.setBounds(10, 130, 100, 25);
        panel.add(middleNameLabel);

        middleNameField = new JTextField(20);
        middleNameField.setBounds(120, 130, 165, 25);
        panel.add(middleNameField);

        JLabel lastNameLabel = new JLabel("Last Name:");
        lastNameLabel.setBounds(10, 170, 100, 25);
        panel.add(lastNameLabel);

        lastNameField = new JTextField(20);
        lastNameField.setBounds(120, 170, 165, 25);
        panel.add(lastNameField);

        JLabel emailLabel = new JLabel("Email Address:");
        emailLabel.setBounds(10, 210, 100, 25);
        panel.add(emailLabel);

        emailField = new JTextField(20);
        emailField.setBounds(120, 210, 165, 25);
        panel.add(emailField);

        JButton insertButton = new JButton("Insert");
        insertButton.setBounds(10, 260, 80, 25);
        panel.add(insertButton);
        insertButton.addActionListener(e -> insertData());

        /*JButton deleteButton = new JButton("Delete");
        deleteButton.setBounds(100, 260, 80, 25);
        panel.add(deleteButton);
        deleteButton.addActionListener(e -> deleteData());

        JButton updateButton = new JButton("Update");
        updateButton.setBounds(190, 260, 80, 25);
        panel.add(updateButton);
        updateButton.addActionListener(e -> updateData());*/

        dataTable = new JTable();
        JScrollPane scrollPane = new JScrollPane(dataTable);
        scrollPane.setBounds(10, 300, 760, 150);
        panel.add(scrollPane);
    }

    private void loadTables() {
        String url = "jdbc:oracle:thin:@//host:1521/DEV"

        try (Connection conn = DriverManager.getConnection(url, LoginScreen.user, LoginScreen.password)) {
            DatabaseMetaData metaData = conn.getMetaData();
            ResultSet tables = metaData.getTables(null, null, "%", new String[] {"TABLE"});

            while (tables.next()) {
                String tableName = tables.getString("TABLE_NAME");
                String table_schem = tables.getString("TABLE_SCHEM");
                tableDropdown.addItem(table_schem + "." + tableName); // Add table name to the dropdown
            }
        } catch (SQLException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "Error loading tables: " + e.getMessage());
        }
    }

    private void showTableData() {
    String selectedTable = (String) tableDropdown.getSelectedItem();
    String url = "jdbc:oracle:thin:@//host:1521/DEV"

    try (Connection conn = DriverManager.getConnection(url, LoginScreen.user, LoginScreen.password)) {
        String query = "SELECT * FROM " + selectedTable;
        PreparedStatement stmt = conn.prepareStatement(query);
        ResultSet rs = stmt.executeQuery();

        ResultSetMetaData metaData = rs.getMetaData();
        int columnCount = metaData.getColumnCount();

        // Fetch column names
        String[] columnNames = new String[columnCount];
        for (int i = 1; i <= columnCount; i++) {
            columnNames[i - 1] = metaData.getColumnName(i);
        }

        // Fetch rows dynamically
        java.util.List<Object[]> rows = new java.util.ArrayList<>();
        while (rs.next()) {
            Object[] rowData = new Object[columnCount];
            for (int colIndex = 0; colIndex < columnCount; colIndex++) {
                rowData[colIndex] = rs.getObject(colIndex + 1);
            }
            rows.add(rowData);
        }

        // Convert list to array for JTable
        Object[][] data = rows.toArray(new Object[0][]);

        // Update table model
        dataTable.setModel(new javax.swing.table.DefaultTableModel(data, columnNames));
    } catch (SQLException e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(this, "Error displaying data: " + e.getMessage());
    }
    }


    private void insertData() {
        executeQuery("INSERT INTO " + tableDropdown.getSelectedItem() +
                " (USERNAME, FIRST_NAME, MIDDLE_NAME, LAST_NAME, EMAIL_ADDRESS) VALUES (?, ?, ?, ?, ?)",
                true);
    }

   /*private void deleteData() {
        executeQuery("DELETE FROM " + tableDropdown.getSelectedItem() + " WHERE USERNAME = ?", false);
    }

    private void updateData() {
        executeQuery("UPDATE " + tableDropdown.getSelectedItem() +
                " SET EMAIL_ADDRESS = ? WHERE USERNAME = ?", false);
    }*/

    private void executeQuery(String query, boolean isInsert) {
        String url = "jdbc:oracle:thin:@//host:1521/DEV"

        try (Connection conn = DriverManager.getConnection(url, LoginScreen.user, LoginScreen.password);
             PreparedStatement stmt = conn.prepareStatement(query)) {

            stmt.setString(1, usernameField.getText());
            if (isInsert) {
                stmt.setString(2, firstNameField.getText());
                stmt.setString(3, middleNameField.getText());
                stmt.setString(4, lastNameField.getText());
                stmt.setString(5, emailField.getText());
            } else {
                stmt.setString(2, emailField.getText());
            }
            stmt.executeUpdate();

            JOptionPane.showMessageDialog(this, "Operation successful.");
        } catch (SQLException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "Error: " + e.getMessage());
        }
    }
}

However, when I asked a co-worker to compile it on her machine it is working. I am not sure what is missing. Please advise. Thank you.

Comments
Post Details
Added on Nov 21 2024
4 comments
112 views