I am using Java and Eclipse to build a program that logs users in by checking usernames and passwords from a SQLite database. I am completely brand new to GUI's and it was pointed out to me in a previous post that my GUI and program logic is not very well structured. With that said, I have successfully logged four different users in and displayed their previously entered information on a form with textField's and comboBox's. My Query on the SQL database is working great retrieving this information and I have been able to show the user that information in the correct fields. As you can see in my code below, I have a textField called UsernameCust. This is where I want to pull my variable to select from in the WHERE clause of my query. The problem is that this textField is located in the Window.Java class and I am trying to use this variable in the CustomerInfo.Java class. I have done a ton of research on getter/setter methods and other ways to pass variables from class to class, but the sheer size and frankly bad logic of my program is making it nearly impossible. How can I pass this variable to the CustomerInfo.Java class? Please feel free to correct my class logic as well. I have tried moving things around but lost functionality of the program, so I am trying to achieve this with the structure that I have.
Here is the Window.Java class
import java.awt.EventQueue;
import java.awt.Font;
import java.sql.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Window {
private JFrame frame;
public static JTextField textFieldUsernameCust;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Window window = new Window();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Connection connection=null;
public JPasswordField passwordFieldCust;
public JTextField textFieldUsernameComp;
public JPasswordField passwordFieldComp;
/**
* Create the application.
*/
public Window() {
initialize();
connection=sqliteConnection.dbConnector();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setResizable(false);
frame.setBounds(100, 100, 623, 382);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblUsername = new JLabel("Username:");
lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblUsername.setBounds(86, 166, 86, 14);
frame.getContentPane().add(lblUsername);
JLabel lblPassword = new JLabel("Password:");
lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblPassword.setBounds(86, 202, 65, 14);
frame.getContentPane().add(lblPassword);
textFieldUsernameCust = new JTextField();
textFieldUsernameCust.setBounds(182, 165, 97, 20);
frame.getContentPane().add(textFieldUsernameCust);
textFieldUsernameCust.setColumns(10);
JButton btnLoginCust = new JButton("Login (Customer)");
btnLoginCust.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
String query="select * from CustomerInfo where Username=? and Password=?";
PreparedStatement pst=connection.prepareStatement(query);
pst.setString(1, textFieldUsernameCust.getText() );
pst.setString(2, passwordFieldCust.getText() );
ResultSet rs=pst.executeQuery();
int count=0;
while(rs.next()){
count=count+1;
}
if (count==1)
{
JOptionPane.showMessageDialog(null, "Login Successful");
frame.dispose();
CustomerInfo custinfo=new CustomerInfo();
custinfo.setVisible(true);
}
else if(count>1)
{
JOptionPane.showMessageDialog(null, "Duplicate Username or Password");
}
else
{
JOptionPane.showMessageDialog(null, "Username or Password is incorrect. Please try again");
}
rs.close();
pst.close();
} catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
}
});
btnLoginCust.setBounds(116, 259, 117, 23);
frame.getContentPane().add(btnLoginCust);
JLabel lblWelcome = new JLabel("Welcome to InsurU");
lblWelcome.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 20));
lblWelcome.setBounds(221, 46, 232, 20);
frame.getContentPane().add(lblWelcome);
passwordFieldCust = new JPasswordField();
passwordFieldCust.setBounds(182, 201, 97, 20);
frame.getContentPane().add(passwordFieldCust);
JLabel label = new JLabel("Username:");
label.setFont(new Font("Tahoma", Font.PLAIN, 14));
label.setBounds(329, 168, 86, 14);
frame.getContentPane().add(label);
JLabel label_1 = new JLabel("Password:");
label_1.setFont(new Font("Tahoma", Font.PLAIN, 14));
label_1.setBounds(329, 204, 65, 14);
frame.getContentPane().add(label_1);
textFieldUsernameComp = new JTextField();
textFieldUsernameComp.setColumns(10);
textFieldUsernameComp.setBounds(425, 165, 97, 20);
frame.getContentPane().add(textFieldUsernameComp);
JButton btnLogincompanyt = new JButton("Login (Company)");
btnLogincompanyt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
String query="select * from CustomerInfo where UserName=? and Password=?";
PreparedStatement pst=connection.prepareStatement(query);
pst.setString(1, textFieldUsernameComp.getText() );
pst.setString(2, passwordFieldComp.getText() );
ResultSet rs=pst.executeQuery();
int count=0;
while(rs.next()){
count=count+1;
}
if (count==1)
{
JOptionPane.showMessageDialog(null, "Login Successful");
frame.dispose();
CompanyInfo compinfo=new CompanyInfo();
compinfo.setVisible(true);
}
else if(count>1)
{
JOptionPane.showMessageDialog(null, "Duplicate Username or Password");
}
else
{
JOptionPane.showMessageDialog(null, "Username or Password is incorrect. Please try again");
}
rs.close();
pst.close();
} catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
}
});
btnLogincompanyt.setBounds(374, 259, 117, 23);
frame.getContentPane().add(btnLogincompanyt);
JLabel lblCustomerLogin = new JLabel("Customer Login");
lblCustomerLogin.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblCustomerLogin.setBounds(116, 107, 117, 22);
frame.getContentPane().add(lblCustomerLogin);
JLabel lblCompanyLogin = new JLabel("Company Login");
lblCompanyLogin.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblCompanyLogin.setBounds(374, 108, 117, 20);
frame.getContentPane().add(lblCompanyLogin);
passwordFieldComp = new JPasswordField();
passwordFieldComp.setBounds(425, 201, 97, 20);
frame.getContentPane().add(passwordFieldComp);
}
}
Here is the CustomerInfo.Java class
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.ResultSet;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.util.Scanner;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.awt.event.ActionEvent;
public class CustomerInfo extends JFrame {
private JPanel contentPane;
private JTextField textFieldFirstName;
private JTextField textFieldLastName;
private JTextField textFieldAdress;
private JTextField textFieldCity;
private JTextField textFieldZipCode;
private JTextField textFieldEmail;
private JTextField textFieldNumber;
private JTextField textFieldAge;
private JTextField textFieldUsername;
private JTextField textFieldPassword;
private JTextField textFieldAdt1LName;
private JTextField textFieldAdt1FName;
private JTextField textFieldAdt1Age;
private JTextField textFieldAdt2FName;
private JTextField textFieldAdt2LName;
private JTextField textFieldAdt2Age;
private JTextField textFieldAdtV1Make;
private JTextField textFieldAdtV1Model;
private JTextField textFieldAdtV1Mileage;
private JTextField textFieldAdtV2Make;
private JTextField textFieldAdtV2Model;
private JTextField textFieldAdtV2Mileage;
private JTextField textFieldAdtV3Make;
private JTextField textFieldAdtV3Model;
private JTextField textFieldAdtV3Mileage;
private JTextField textFieldTotalDrivers;
private JTextField textFieldAdtTotVehicles;
private JComboBox comboBoxAdt1DBYear;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CustomerInfo frame = new CustomerInfo();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CustomerInfo() {
String username = "Customer3";
Statement stmt = null;
Connection connection=null;
connection=sqliteConnection.dbConnector();
try{
Class.forName("org.sqlite.JDBC");
Connection conn=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Gregory\\workspacefinal\\Customer.sqlite");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM CustomerInfo WHERE Username = + '" + username + "'");
String firstName = rs.getString("FirstName");
String lastName = rs.getString("LastName");
String adress = rs.getString("Adress");
String city = rs.getString("City");
String zipcode = rs.getString("ZipCode");
String email = rs.getString("Email");
String phonenumber = rs.getString("PhoneNumber");
String age = rs.getString("Age");
String state = rs.getString("State");
String dbmonth = rs.getString("DBMonth");
String dbday = rs.getString("DBDay");
String dbyear = rs.getString("DBYear");
String relatstat = rs.getString("RelationStat");
String adt1FName= rs.getString("Adt1FName");
String adt1LName = rs.getString("Adt1LName");
String adt1DBMonth = rs.getString("Adt1DBMonth");
String adt1DBDay = rs.getString("Adt1DBDay");
String adt1DBYear = rs.getString("Adt1DBYear");
String adt1Relat = rs.getString("Adt1Relat");
String adt1Age = rs.getString("Adt1Age");
String adt2FName = rs.getString("Adt2FName");
String adt2LName = rs.getString("Adt2LName");
String adt2DBMonth = rs.getString("Adt2DBMonth");
String adt2DBDay = rs.getString("Adt2DBDay");
String adt2DBYear = rs.getString("Adt2DBYear");
String adt2Relat = rs.getString("Adt2Relat");
String adt2Age = rs.getString("Adt2Age");
String totalAdtDrivers = rs.getString("TotalAdtDrivers");
String adtV1Make = rs.getString("AdtV1Make");
String adtV1Model = rs.getString("AdtV1Model");
String adtV1Usage = rs.getString("AdtV1Usage");
String adtV1Mileage = rs.getString("AdtV1Mileage");
String adtV2Make = rs.getString("AdtV2Make");
String adtV2Model = rs.getString("AdtV2Model");
String adtV2Usage = rs.getString("AdtV2Usage");
String adtV2Mileage = rs.getString("AdtV2Mileage");
String adtV3Make = rs.getString("AdtV3Make");
String adtV3Model = rs.getString("AdtV3Model");
String adtV3Usage = rs.getString("AdtV3Usage");
String adtV3Mileage = rs.getString("AdtV3Mileage");
String totalAdtVehicles = rs.getString("TotalAdtVehicles");
String movingViolations = rs.getString("MovingViol");
String accidents = rs.getString("Accidents");
String currentInsur = rs.getString("CurrentInsur");
String comprehensive = rs.getString("Comprehensive");
String collision = rs.getString("Collision");
String rental = rs.getString("Rental");
String towing = rs.getString("Towing");
String propDmg = rs.getString("PropDamage");
String bodInjury = rs.getString("BodilyInjury");
String unUiBod = rs.getString("UnUiBod");
String unUiProp = rs.getString("UnUiProp");
textFieldFirstName.setText(firstName);
textFieldLastName.setText(lastName);
textFieldAdress.setText(adress);
textFieldCity.setText(city);
textFieldZipCode.setText(zipcode);
textFieldEmail.setText(email);
textFieldNumber.setText(phonenumber);
textFieldAge.setText(age);
textFieldAdt1FName.setText(adt1FName);
textFieldAdt1LName.setText(adt1LName);
textFieldAdt1Age.setText(adt1Age);
textFieldAdt2FName.setText(adt2FName);
textFieldAdt2LName.setText(adt2LName);
textFieldAdt2Age.setText(adt2Age);
textFieldTotalDrivers.setText(totalAdtDrivers);
textFieldAdtV1Make.setText(adtV1Make);
textFieldAdtV1Model.setText(adtV1Model);
textFieldAdtV1Mileage.setText(adtV1Mileage);
textFieldAdtV2Make.setText(adtV2Make);
textFieldAdtV2Model.setText(adtV2Model);
textFieldAdtV2Mileage.setText(adtV2Mileage);
textFieldAdtV3Make.setText(adtV3Make);
textFieldAdtV3Model.setText(adtV3Model);
textFieldAdtV3Mileage.setText(adtV3Mileage);
textFieldAdtTotVehicles.setText(totalAdtVehicles);
comboBoxState.setSelectedItem(state);
comboBoxDBMonth.setSelectedItem(dbmonth);
comboBoxDBDay.setSelectedItem(dbday);
comboBoxDBYear.setSelectedItem(dbyear);
comboRelationshipStatus.setSelectedItem(relatstat);
comboBoxAdt1DBMonth.setSelectedItem(adt1DBMonth);
comboBoxAdt1DBDay.setSelectedItem(adt1DBDay);
comboBoxAdt1DBYear.setSelectedItem(adt1DBYear);
comboBoxAdt2DBMonth.setSelectedItem(adt2DBMonth);
comboBoxAdt2DBDay.setSelectedItem(adt2DBDay);
comboBoxAdt2DBYear.setSelectedItem(adt2DBYear);
comboBoxAdt1Relat.setSelectedItem(adt1Relat);
comboBoxAdt2Relat.setSelectedItem(adt2Relat);
comboBoxAdtV1Use.setSelectedItem(adtV1Usage);
comboBoxAdtV2Use.setSelectedItem(adtV2Usage);
comboBoxAdtV3Use.setSelectedItem(adtV3Usage);
comboBoxLosses1.setSelectedItem(movingViolations);
comboBoxLosses2.setSelectedItem(accidents);
comboBoxPresent.setSelectedItem(currentInsur);
comboBoxComp.setSelectedItem(comprehensive);
comboBoxColl.setSelectedItem(collision);
comboBoxRent.setSelectedItem(rental);
comboBoxTow.setSelectedItem(towing);
comboBoxProp.setSelectedItem(dbyear);
comboBoxBod.setSelectedItem(relatstat);
comboBoxUnUiBod.setSelectedItem(unUiBod);
comboBoxUnUiProp.setSelectedItem(unUiProp);
stmt.close();
conn.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
} I have not included the GUI elements of the code in the second class to avoid character limit, but they are there. You can see above that I have been using String username = "Customer3"; to test my query. This is where I need to pass the username variable from the first class so the WHERE clause is dynamic based on the username entered to login. Later, I will also need to pass all the variables I set after the Query into yet another class to compare with if statements, so I will need to yet again pass variables from class to class. If I am on the right track trying to use getters and setters, where in the above code would they be placed and how should I properly call them? Thank you!
Aucun commentaire:
Enregistrer un commentaire