0

I am trying to create a register system for my MySQL database. I have a Java form that I created, in which the user inserts a username, password, and their email. I take that infromation and store it in a variable accordingly. The MySQL connection code works, and it does insert a new row, but the information is blank on the MySQL page. Am I doing someting wrong?

public class Main extends JFrame {

private static final long serialVersionUID = 1L;

private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;

private static Connection con;
private static PreparedStatement st;
private static int rs;

String username;
String password;
String email;
private JTextField textField_1;

String insertTableSQL = "INSERT INTO `users`" + "(username, password, email) VALUES" + "(?,?,?)";

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Main frame = new Main();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public Main() {
    setResizable(false);
    setTitle("Barrage : Login / Register");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 350, 200);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblNewLabel = new JLabel("Username: ");
    lblNewLabel.setBounds(60, 25, 68, 20);
    contentPane.add(lblNewLabel);

    JLabel lblPassword = new JLabel("Password: ");
    lblPassword.setBounds(60, 59, 68, 20);
    contentPane.add(lblPassword);

    textField = new JTextField();
    username = textField.getText();
    textField.setBounds(138, 22, 120, 26);
    contentPane.add(textField);
    textField.setColumns(10);

    passwordField = new JPasswordField();
    password = passwordField.getText();
    passwordField.setBounds(138, 56, 120, 26);
    contentPane.add(passwordField);

    JButton btnRegister = new JButton("Register");
    btnRegister.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            try{
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/barrage", "root", "");
                st = con.prepareStatement(insertTableSQL);
                st.setString(1, username);
                st.setString(2, password);
                st.setString(3, email);
                st.executeUpdate();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }

    });
    btnRegister.setBounds(119, 138, 89, 23);
    contentPane.add(btnRegister);

    JLabel lblEmail = new JLabel("E-mail: ");
    lblEmail.setBounds(60, 96, 68, 20);
    contentPane.add(lblEmail);

    textField_1 = new JTextField();
    email = textField_1.getText();
    textField_1.setBounds(138, 93, 120, 26);
    contentPane.add(textField_1);
    textField_1.setColumns(10);
}

}

1
  • You need to assign values to the three parameter variables inside you actionPerformed method. So you will need 3 separate JTextField components. Commented Nov 19, 2013 at 0:00

2 Answers 2

2

You need to call the various fields' getText() method in your ActionListener so that you get the values after the user has entered them.

As it stands now you get the initial values prior to any user interaction.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to set the values of username u.a. in the moment where you you call the actionPerformed method.

Right now you set the value when creating the GUI. At that moment they are null. They are not "linked", they just take the value that the controls return at that time. If the controls getText() result changes, your variables do not get updated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.