I have this program that is for a login form. I am trying to take user variable that is local in the actionListener method in order to use it in mysql database when the user answer a question. So basically I have this class that the user does his login and then it goes to another form to click to begin the test and then it goes to the form that I want to use user variable. I have done getter and setter methods in order to have username and then use it in the JFrame which user answers a question. When I print a message in this class, it returns user variable. But when I want to use it in the other JFrame it returns null! Any ideas, how can I do it?
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JPasswordField;
public class Build1 extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;
private String user;
JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Build1 frame = new Build1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Build1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(96, 58, 86, 20);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblUsername = new JLabel("Username");
lblUsername.setBounds(10, 61, 59, 14);
contentPane.add(lblUsername);
JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(10, 113, 59, 14);
contentPane.add(lblPassword);
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try{
String user = textField.getText().trim();
String pass = String.valueOf(passwordField.getPassword());
setUsername(user);
getUsername();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/users","root","1234");
PreparedStatement st = con.prepareStatement("select * from user where username='"+user+"'");
ResultSet rs = st.executeQuery();
if(rs.next())
{
String dbpass = rs.getString(4);
if(dbpass.equals(pass)){
if(user.equals("Admin")){
Build5 Admin = new Build5();
Admin.setVisible(true);
Build1.this.dispose();
Build1.this.setVisible(false);
}
else{
JOptionPane.showMessageDialog(frame," "+user);
Build2 User = new Build2();
User.setVisible(true);
Build1.this.dispose();
Build1.this.setVisible(false);
}
}
else
{
JOptionPane.showMessageDialog(null,"Login Unsuccessful!","Error",1);
}
}
else
{
JOptionPane.showMessageDialog(null,"Username not found","Error",1);
}
} catch (SQLException ex) {
System.out.println(ex);
}
}
});
btnLogin.setBounds(186, 202, 89, 23);
contentPane.add(btnLogin);
JButton btnRegister = new JButton("Register");
btnRegister.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try {
Build3 frame = new Build3();
frame.setVisible(true);
Build1.this.dispose();
Build1.this.setVisible(false);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
btnRegister.setBounds(186, 236, 89, 23);
contentPane.add(btnRegister);
JButton btnRecoverPassword = new JButton("Recover Password");
btnRecoverPassword.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try {
Build4 frame = new Build4();
frame.setVisible(true);
Build1.this.dispose();
Build1.this.setVisible(false);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
btnRecoverPassword.setBounds(153, 270, 147, 23);
contentPane.add(btnRecoverPassword);
passwordField = new JPasswordField();
passwordField.setBounds(96, 110, 86, 20);
contentPane.add(passwordField);
}
public void setUsername(String user){
this.user = this.textField.getText();
}
public String getUsername() {
JOptionPane.showMessageDialog(frame,"1 "+user);
return user;
}
}
The other class:
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
public class Build6 extends JFrame {
private JPanel contentPane;
JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Build6 frame1 = new Build6();
frame1.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Build6() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
final JRadioButton rdbtnTrue = new JRadioButton("True");
rdbtnTrue.setBounds(162, 132, 109, 23);
contentPane.add(rdbtnTrue);
final JRadioButton rdbtnFalse = new JRadioButton("False");
rdbtnFalse.setBounds(162, 158, 109, 23);
contentPane.add(rdbtnFalse);
final JRadioButton rdbtnDontKnow = new JRadioButton("Don't know");
rdbtnDontKnow.setBounds(162, 184, 109, 23);
contentPane.add(rdbtnDontKnow);
final JRadioButton rdbtnWhatever = new JRadioButton("Whatever");
rdbtnWhatever.setBounds(162, 210, 109, 23);
contentPane.add(rdbtnWhatever);
JLabel lblDoesAMan = new JLabel("Does a man fly?");
lblDoesAMan.setBounds(162, 97, 109, 14);
contentPane.add(lblDoesAMan);
JButton btnAnswer = new JButton("Answer");
btnAnswer.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try {
Build1 a = new Build1();
String user = a.getUsername();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/answers","root","1234");
Connection con1 = DriverManager.getConnection("jdbc:mysql://localhost:3306/users","root","1234");
PreparedStatement st = con1.prepareStatement("select * from user where username='"+user+"'");
ResultSet rs = st.executeQuery();
while(rs.next()){
String username = rs.getString("username");
if(user.equals(username)){
JOptionPane.showMessageDialog(frame,"DAN DAN!"+user+" "+username);
}
}
if(rdbtnDontKnow.isSelected()){
java.util.Date dt = new java.util.Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.text.SimpleDateFormat sdf1 = new java.text.SimpleDateFormat("yyyy-MM-dd");
String currentday = sdf1.format(dt);
String currentdayTime = sdf.format(dt);
String answer = "Wrong";
String test ="test1";
PreparedStatement st11 = (PreparedStatement) con.prepareStatement("INSERT INTO answer (username,daytime,answer,test,day) VALUES (?,?,?,?,?)");
st11.setString(1, user);
st11.setString(2, currentdayTime);
st11.setString(3, answer);
st11.setString(4, test);
st11.setString(5, currentday);
st11.executeUpdate();
st11.close();
}
else if(rdbtnWhatever.isSelected()){
java.util.Date dt = new java.util.Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.text.SimpleDateFormat sdf1 = new java.text.SimpleDateFormat("yyyy-MM-dd");
String currentdayTime = sdf.format(dt);
String currentday = sdf1.format(dt);
String answer = "Wrong";
String test ="test1";
PreparedStatement st11 = (PreparedStatement) con.prepareStatement("INSERT INTO answer (username,daytime,answer,test,day) VALUES (?,?,?,?,?)");
st11.setString(1, user);
st11.setString(2, currentdayTime);
st11.setString(3, answer);
st11.setString(4, test);
st11.setString(5, currentday);
st11.executeUpdate();
st11.close();
}
else if(rdbtnTrue.isSelected()){
java.util.Date dt = new java.util.Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.text.SimpleDateFormat sdf1 = new java.text.SimpleDateFormat("yyyy-MM-dd");
String currentday = sdf1.format(dt);
String currentdayTime = sdf.format(dt);
String answer = "Wrong";
String test ="test1";
PreparedStatement st11 = (PreparedStatement) con.prepareStatement("INSERT INTO answer (username,daytime,answer,test,day) VALUES (?,?,?,?,?)");
st11.setString(1, user);
st11.setString(2, currentdayTime);
st11.setString(3, answer);
st11.setString(4, test);
st11.setString(5, currentday);
st11.executeUpdate();
st11.close();
}
else if(rdbtnFalse.isSelected()){
java.util.Date dt = new java.util.Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.text.SimpleDateFormat sdf1 = new java.text.SimpleDateFormat("yyyy-MM-dd");
String currentday = sdf1.format(dt);
String currentdayTime = sdf.format(dt);
String answer = "Wrong";
String test ="test1";
PreparedStatement st11 = (PreparedStatement) con.prepareStatement("INSERT INTO answer (username,daytime,answer,test,day) VALUES (?,?,?,?,?)");
st11.setString(1, user);
st11.setString(2, currentdayTime);
st11.setString(3, answer);
st11.setString(4, test);
st11.setString(5, currentday);
st11.executeUpdate();
st11.close();
}else{
JOptionPane.showMessageDialog(frame,"Select an answer!");
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
btnAnswer.setBounds(192, 261, 89, 23);
contentPane.add(btnAnswer);
ButtonGroup group = new ButtonGroup(); //Σε περίπτωση που θέλουμε να διαλέγει μόνο ένα!
group.add(rdbtnTrue);
group.add(rdbtnFalse);
group.add(rdbtnDontKnow);
group.add(rdbtnWhatever);
}
}
Edit(20/1): The new Login Form (sql part where user variable is located)
JButton lgnBtn = new JButton("Login");
lgnBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try{
String user = t.getText().trim();
String pass = String.valueOf(t1.getPassword());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/users","root","1234");
PreparedStatement st = con.prepareStatement("select * from user where username='"+user+"'");
ResultSet rs = st.executeQuery();
if(rs.next())
{
String dbpass = rs.getString(4);
if(dbpass.equals(pass)){
if(user.equals("Admin")){
Build5 Admin = new Build5();
Admin.setVisible(true);
}
else{
JOptionPane.showMessageDialog(frame," "+user);
Build2 User = new Build2();
User.setVisible(true);
}
}
else
{
JOptionPane.showMessageDialog(null,"Login Unsuccessful!","Error",1);
}
}
else
{
JOptionPane.showMessageDialog(null,"Username not found","Error",1);
}
} catch (SQLException ex) {
System.out.println(ex);
}
}
});