Reputation: 43
I press the buttons on my Java program but it doesn't seem to do anything at all. I just press them and nothing happens. I'm using Java 8. In Eclipse.
I'm not sure if it's because there's a database connection problem? If it is I'd probably just go to SQL.
Here's the code for the Log In Page:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class login extends JFrame implements ActionListener{
JPanel pane = new JPanel();
ImageIcon imageicon = new ImageIcon ("res/login.png");
JLabel bg = new JLabel (" ", imageicon, JLabel.CENTER);
JLabel bg1 = new JLabel (new ImageIcon(""));
JTextField use1 = new JTextField();
JPasswordField pas1 = new JPasswordField();
JButton displayButton = new JButton("Login");
JButton cancelButton = new JButton("Cancel");
String search ="";
String searchP="";
// Select txt2= new Select();
Connection cn;
PreparedStatement ps;
Statement st;
public login(){
// pane.setBackground(Color.orange);
setContentPane (pane);
pane.setLayout (null);
//---------------------------------------------
pane.add(use1);
use1.setBounds(50,225, 190, 30);
use1.setToolTipText("Username");
pane.add(pas1);
pas1.setBounds(50,260, 190, 30);
pas1.setToolTipText("Password");
//---------------------------------------------
pane.add(displayButton);
displayButton.setBounds(60,300, 80, 25);
displayButton.addActionListener(this);
pane.add(cancelButton);
cancelButton.setBounds(150,300, 80, 25);
cancelButton.addActionListener(this);
//---------------------------------------------
pane.add(bg1);
bg1.setVisible(true);
bg1.setBounds (5,40,200,129);
pane.add(bg);
bg.setBounds (0,0,300,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try{
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
cn=DriverManager.getConnection("jdbc:ucanaccess://res/YOLO.accdb");
}catch(ClassNotFoundException a){
System.err.println("Failed to Load Driver");
a.printStackTrace();
}catch (SQLException a){
System.err.println("Unable to Connect");
a.printStackTrace();
}
}
public void actionPerformed (ActionEvent e){
if (e.getSource() ==displayButton){
search=use1.getText();
searchP=pas1.getText();
try{
if (!search.equals("")||!searchP.equals("")){
st= cn.createStatement();
ResultSet rs=st.executeQuery("SELECT * FROM Login WHERE UserN = '" + search+"'");
while(rs.next()){
searchP=rs.getString(2);
if(searchP.equalsIgnoreCase(pas1.getText())){
dispose();
JOptionPane.showMessageDialog (null, "Login Successful!! ");
main frame = new main ();
frame.setTitle ("YOLO Airline Ticketing & Hotel Reservation");
frame.setBounds(200,200,800,500);
frame.setVisible (true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
st.close();
}
}
}catch(SQLException m){
System.out.println(m.getMessage());
}finally{
if (search.equals("")||searchP.equals("")){
if(search.equals("")){
JOptionPane.showMessageDialog(null,"Invalid Input!","Warning!",JOptionPane.INFORMATION_MESSAGE);
}
if(searchP.equals("")){
JOptionPane.showMessageDialog(null,"Invalid Password!","Warning!",JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
}
public static void main(String args[]) {
login myframe = new login();
myframe.setBounds(400,280,300,400);
myframe.setTitle("YOLO Login");
myframe.setVisible(true);
myframe.setResizable(false);
}
}
Upvotes: 2
Views: 171
Reputation: 667
Your code is fine, except for one statement:
...
main frame = new main ();
...
I didn't see any definition for main
, am I missing something?
Eclipse should protest against your using undefined class, don't overlook warnings, ever.
Upvotes: 1
Reputation: 5339
try
displayButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
}
});
Upvotes: 1