The main technologies used in this paper are :GUI,JDBC, Multithreading

  The functions realized are as follows :

1, Login function

2, Registration function

3, Whether to hide the choice of password and realize the function

4, Select gender function

5, Password and confirm password function

6, The login page displays the current time in real time

7, When logging in, the user name and password have no matching data in the database , Will jump to the registration page .

8, same , After registration , Data will be used JDBC Write data to database , Then jump back to the login page .

  Login page :
import javax.swing.*; import java.awt.*; import java.awt.event.*; import
java.sql.*; import java.time.LocalTime; public class JDBC_ Login function { public static
void main(String[] args) { guitext3 gt=new guitext3(); new Thread(new
time1(gt.time)).start();// Start thread } } class guitext3 { JFrame jf; JLabel
l1,l2,time; JTextField f1; JPasswordField f2; JButton jb1,jb2; JCheckBox jc;
public guitext3() { jf = new JFrame("QQ Login window "); jf.setSize(320,250);
jf.setLocation(700,300);// The setting window is displayed in the center of the screen every time it is started jf.setLayout(null); Font font = new
Font(" Imitation Song Dynasty ", Font.BOLD, 20);// Set uniform font , Make the code cleaner and more beautiful l1 = new JLabel(" user name :");
l1.setBounds(10,10,100,40); l1.setFont(font); f1 = new JTextField(null,20);
f1.setBounds(90,15,180,30); f1.setFont(font); l2 = new JLabel(" dense code :");
l2.setBounds(8,50,100,40); l2.setFont(font); f2=new JPasswordField(null,20);
f2.setBounds(90,55,180,30); f2.setEchoChar('*');// Set the password to be explicit * f2.setFont(font);
jc=new JCheckBox(" Show password "); jc.setBounds(230,130,80,40); jc.addItemListener(new
ItemListener() { public void itemStateChanged(ItemEvent e) { if
(e.getStateChange() == ItemEvent.SELECTED) {// checked f2.setEchoChar((char) 0); }
else { f2.setEchoChar('*'); } } }); ActionListener listener=new
AbstractAction() { @Override public void actionPerformed(ActionEvent e) { } };
jc.addActionListener(listener); jb1 = new JButton(" Sign in ");
jb1.setBounds(30,100,80,40); time=new JLabel(); time.setBounds(30,150,140,40);
time.setFont(font); // Anonymous Inner Class jb1.addMouseListener(new MouseAdapter() { public
void mouseClicked(MouseEvent e) { Statement st=null; Connection con=null;
ResultSet rs=null; try { // Register driver Class.forName("com.mysql.cj.jdbc.Driver");
// there 3306/ Followed by the database name // Get database connection String
url="jdbc:mysql://localhost:3306/students?serverTimezone=GMT%2B8";
// adopt DriverManager Complete registration con=
DriverManager.getConnection(url,"root","123");// Your own database username and password // implement SQL sentence String
sql="select * from student";//from Followed by table name st=con.createStatement();
rs=st.executeQuery(sql); boolean flag=false; while(rs.next()){
// If the user name and password entered are the same as those in the database , Pop up “ Login successful !” Window of
if(f1.getText().equals(rs.getString(1))&&f2.getText().equals(rs.getString(2))){
JOptionPane.showMessageDialog(null, " Login successful !"); flag=true;// After successful login, change the mark to true Convenient confirmation
break; } } if(flag==false){// If marked as false, The user name and password are not found in the database , eject “ Login failed ! Please register your account !” Window of
JOptionPane.showMessageDialog(null, " Login failed ! Please register your account !"); f1.setText(null);// Clear user bar
f2.setText(null);// Clear password bar // This account does not exist , Registration required , Jump to the registration window guitext4 gt=new guitext4();
jf.dispose();// Closing Windows , Free all resources } } catch (ClassNotFoundException ex) {
ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } } });
jb1.setFont(font); jb2 = new JButton(" sign out "); jb2.setBounds(150,100,80,40);
// Anonymous Inner Class jb2.addMouseListener(new MouseAdapter() { // Override mouse click events public void
mouseClicked(MouseEvent e) {// If you click exit window , Pop up “ Exit successful !” Window of
JOptionPane.showMessageDialog(null, " Exit successful !"); // System exit System.exit(0); } });
jb2.setFont(font); // Add these buttons, text, etc. to the form jf.add(l1); jf.add(f1); jf.add(l2);
jf.add(f2); jf.add(jb1); jf.add(jb2); jf.add(time); jf.add(jc);
jf.setVisible(true);// Let components display } } // Write a time thread class class time1 implements Runnable{
JLabel time;// Convenient parameter transmission public time1(JLabel time) { this.time = time; } public void
run(){ while (true) { try { Thread.sleep(1000);// dormancy 1 second LocalTime
time=LocalTime.now();// Get current time this.time.setText(time.toString());// set up JLabel text
//SimpleDateFormat t = new SimpleDateFormat ("HH:mm:ss");// Format time , Format time as : branch : second
// this.time.setText(t.format(time));// Add content to tags , That is, time } catch
(InterruptedException e) { e.printStackTrace(); } } } }
  Registration page :
import javax.swing.*; import java.awt.*; import java.awt.event.*; import
java.sql.*; public class JDBC_ Registration function { public static void main(String[] args) {
guitext4 gt=new guitext4(); } } class guitext4{ JFrame jf; // JPanel jp; JLabel
l1,l2,l3; JTextField f1; JPasswordField f2,f3; JButton jb1,jb2; JRadioButton
jr1,jr2; JCheckBox jc; public guitext4() { jf = new JFrame("QQ Registration window ");
jf.setSize(320,280);// Set form size jf.setLocation(700,300);// The setting window is displayed in the center of the screen every time it is started
jf.setLayout(null); // jp = new JPanel(); Font font = new Font(" Imitation Song Dynasty ", Font.BOLD,
20);// Set uniform font , Make the code cleaner and more beautiful l1 = new JLabel(" user name :"); l1.setBounds(10,10,100,40);
l1.setFont(font); f1 = new JTextField(null,20); f1.setBounds(90,15,180,30);
f1.setFont(font); l2 = new JLabel(" dense code :"); l2.setBounds(8, 50, 100, 40);
l2.setFont(font); f2 = new JPasswordField(null, 20); f2.setBounds(90, 55, 180,
30); f2.setEchoChar('*');// Set the password to be explicit * f2.setFont(font); l3 = new
JLabel(" Confirm password :"); l3.setBounds(8, 88, 130, 40); l3.setFont(font); f3 = new
JPasswordField(null, 20); f3.setBounds(120, 95, 160, 30);
f3.setEchoChar('*');// Set the password to be explicit * jc=new JCheckBox(" Show password ");// Create a check button
jc.setBounds(230,130,80,40); jc.addItemListener(new ItemListener() { public
void itemStateChanged(ItemEvent e) { if (e.getStateChange() ==
ItemEvent.SELECTED) {// checked f2.setEchoChar((char) 0);// Display original data
f3.setEchoChar((char)0);// Display original data } else { f2.setEchoChar('*');// Set the password to be explicit *
f3.setEchoChar('*');// Set the password to be explicit * } } }); f3.setFont(font); ButtonGroup group =
new ButtonGroup(); // Create two radio buttons jr1 = new JRadioButton(" male "); jr1.setBounds(70,
130, 80, 40); jr1.setFont(font); jr2 = new JRadioButton(" female ");
jr2.setBounds(150, 130, 80, 40); jr2.setFont(font);
// Add two radio buttons to the same ButtonGroup Group group.add(jr1); group.add(jr2); // Implement monitoring interface
ActionListener listener = new AbstractAction() { @Override
//actionPerformed==> Called when an action occurs public void actionPerformed(ActionEvent e) { } };
jr1.addActionListener(listener); jr2.addActionListener(listener);
jc.addActionListener(listener); jb1 = new JButton(" register "); jb1.setBounds(50, 170,
80, 40); // Anonymous Inner Class jb1.addMouseListener(new MouseAdapter() { public void
mouseClicked(MouseEvent e) { PreparedStatement ps =
null;//PreparedStatement==> Represents precompiled SQL Statement object . //
SQL Statement precompiled and stored in PreparedStatement Object . Connection con =
null;//Connection==> Connection to a specific database try { // Register driver
Class.forName("com.mysql.cj.jdbc.Driver"); // there 3306/ Followed by the database name // Get database connection String
url = "jdbc:mysql://localhost:3306/students?serverTimezone=GMT%2B8";
// adopt DriverManager Complete registration con = DriverManager.getConnection(url, "root",
"123");// User name and password of your own database // implement SQL sentence String sql = "insert into
student(username,password,sex) values(???)"; ps = con.prepareStatement(sql);
if(f1.getText().length()!=0){ // notes : Everything here getText() Can't write it !=null ps.setString(1,
f1.getText()); }else{ JOptionPane.showMessageDialog(null, " Name cannot be empty !");// Pop up window }
if(f2.getText().length()!=0) { ps.setString(2, f2.getText()); }else{
JOptionPane.showMessageDialog(null, " Password cannot be empty !"); }
if(jr1.isSelected()||jr2.isSelected()) {// If you choose male or female , Just store it in the database . if
(jr1.isSelected()) {//isSelected()==> Judge whether the button is selected ps.setString(3,
jr1.getText());// Select male , Will sex= Add to data } if (jr2.isSelected()) { ps.setString(3,
jr2.getText());// Select female , Will sex= Add to data } }else{// Otherwise, pop-up reminder “ Please select gender !”
JOptionPane.showMessageDialog(null, " Please select gender !"); } } catch
(ClassNotFoundException ex) { ex.printStackTrace(); } catch (SQLException ex) {
ex.printStackTrace(); } if (new String(f2.getPassword()).equals(new
String(f3.getPassword()))) {// If the password matches the confirmation password if (new
String(f2.getPassword()).equals(new
String(f3.getPassword()))&&f1.getText().length()!=0&&f2.getText().length()!=0
&&(jr1.isSelected()||jr2.isSelected())) {// If the password matches the confirmation password , User name and password are not empty and gender is selected , Then the registration is successful
JOptionPane.showMessageDialog(null, " login was successful !"); // After successful registration, jump to the login window guitext3 gt = new
guitext3(); jf.dispose();// Closing Windows , Free all resources try { int i =
ps.executeUpdate();// Store the registered account in the database } catch (SQLException ex) {
ex.printStackTrace(); } } } else { JOptionPane.showMessageDialog(null,
" login has failed ! The password is inconsistent with the confirmation password !"); f2.setText(null);// Clear password f3.setText(null);// Clear confirmation password } } });
jb1.setFont(font); jb2 = new JButton(" sign out "); jb2.setBounds(170, 170, 80, 40);
// Anonymous Inner Class jb2.addMouseListener(new MouseAdapter() { public void
mouseClicked(MouseEvent e) { JOptionPane.showMessageDialog(null, " Exit successful !");
// System exit System.exit(0); } }); jb2.setFont(font); jf.add(l1); jf.add(f1);
jf.add(l2); jf.add(f2); jf.add(l3); jf.add(f3); jf.add(jr1); jf.add(jr2);
jf.add(jc); jf.add(jb1); jf.add(jb2); // jf.add(jp); jf.setVisible(true); } }
  Login page running results :

 

  Registration page running results :

 

  Hide password renderings :

 

Technology
Daily Recommendation
views 267
views 232
views 231