The import javax.mail. Swing. *;
The import javax.mail. Swing. Border. *;
Import the Java. The awt. *;
Import the Java. The awt. Event. An ActionEvent;
Import the Java. The awt. Event. ActionListener;
import java.awt.event.WindowEvent;
Import the Java. The awt. Event. WindowListener;
Import the Java. SQL. Connection;
Import the Java. SQL. DriverManager;
Import of Java, SQL PreparedStatement;
Import the Java. SQL. The ResultSet;
Import the Java. SQL. SQLException;
Public class Login extends JFrame implements ActionListener {
Public static void main (String [] args) {
The Login lo=new Login (" Login ");//main method, has the program will run
}
//graphic interface initialization statement, this window will be layout
JLabel label1=new JLabel (" user name: ");
JLabel label2=new JLabel (" password: ");
The static JTextField text1=new JTextField (10);;
JPasswordField text2=new JPasswordField (10);;
JButton for=new JButton (" login ");
JButton button2=new JButton (" cancel ");
JButton button3=new JButton (" registration ");
Public Login (String s) {
init();//window layout initialization method, it is below the init method
SetLayout (null);
SetTitle (" login ");//set the title
SetBounds (400,50,600,500);//Settings window location and size
SetVisible (true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//window layout initialization method
Void init () {
//set the text color, etc.
Label1. SetForeground (Color black);
Label2. SetForeground (Color black);
Label1. SetFont (new Font (" tahoma ", the Font, BOLD, 20));
Label2. SetFont (new Font (" tahoma ", the Font BOLD, 20));
//will be added to the layout window
Add (label1);
Add (label2);
Add (text1);
Add (text2);
Add (for);
Add (button2);
Add (button3);
//set the label size and location
Label1. SetBounds (110,70,100,40);
Text1. SetBounds (230,70,250,40);
Label2. SetBounds (110130100, 40);
Text2. SetBounds (230130250, 40);
For the setBounds (110320100, 40);
Button2. SetBounds (250320100, 40);
Button3. SetBounds (390320100, 40);
//add the action for the button listener
For the addActionListener ((ActionListener) this);
Button2. AddActionListener (ActionListener) this);
Button3. AddActionListener (ActionListener) this);
}
@ Override
Public void actionPerformed (an ActionEvent e) {
If (um participant etSource ()==for) {
//get the input values
String us=text1. GetText ();
String pa=text2. GetText ();
Try {
Class.forname ("... Org. Apache Derby JDBC EmbeddedDriver ");//links database driven
//links database
The Connection conn=(Connection) DriverManager. GetConnection (" JDBC: Derby: Student; The create=true ");
System. The out. Println (conn);
//query authentication database and log on to
String SQL="Select * from user_u where Uname=? And Upass=? ";
PreparedStatement ps=conn. PrepareStatement (SQL);
Ps. SetString (1, us);
Ps. SetString (2, pa);
ResultSet rs=ps.executeQuery();
{if (rs. Next ())
JOptionPane. ShowMessageDialog (this, "landing successful, welcome to the home page,"
"Sure," JOptionPane. INFORMATION_MESSAGE);
Enclosing the dispose ();
New YWindow ();
} else {
JOptionPane. ShowMessageDialog (this, "user name or password error",
"Sure," JOptionPane. INFORMATION_MESSAGE);
Enclosing the dispose ();
The Login lo=new Login (" Login ");
}
conn.close();
Ps. The close ();
{} to catch (a ClassNotFoundException e1)
e1.printStackTrace();
{} the catch (SQLException e1)
e1.printStackTrace();
}
} else if (um participant etSource ()==button2) {
//determine whether click cancel button
JOptionPane. ShowMessageDialog (this, "exit the application", "exit",
JOptionPane. INFORMATION_MESSAGE);
System.exit(0);//end program
} else {
//click register, jump to the sign-up page
Enclosing the dispose ();
New ZhuCe (" registration ");
}
}
}
CodePudding user response:
New String (text2. GetPassword ());CodePudding user response: