Java GUI 连接数据库完成登录注册页面

运用GUI连接数据库进行登录注册

登录页面
在这里插入图片描述

详细代码

package com.itheima.denglu;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.LayoutManager;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JDialog;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.itheima.mian.showlist;
import com.itheima.sql.connectionlain;

public class login {
	private JFrame frame;
	private JTextField textField;
	private JPasswordField passwordField;
	private JButton quitBt;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					login window = new login();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public login() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		
		frame = new JFrame();
		frame.setBounds(400, 200, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		frame.setResizable(false);
		frame.getContentPane().setLayout(null);
		
		 JLabel j1=new JLabel("***欢迎登陆学生信息管理系统***");
		 j1.setBounds(120, 26, 180, 15);
		 frame.getContentPane().add(j1);
         

		JLabel lblNewLabel = new JLabel("用户名:");
		lblNewLabel.setBounds(100, 56, 54, 15);
		frame.getContentPane().add(lblNewLabel);

		JLabel lblNewLabel_1 = new JLabel("密码:");
		lblNewLabel_1.setBounds(100, 102, 54, 15);
		frame.getContentPane().add(lblNewLabel_1);

		textField = new JTextField();
		textField.setBounds(196, 53, 100, 21);
		frame.getContentPane().add(textField);
		textField.setColumns(10);

		passwordField = new JPasswordField();
		passwordField.setBounds(196, 96,100, 21);
		frame.getContentPane().add(passwordField);

		JButton btnNewButton = new JButton("登录");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				connectionlain c = new connectionlain();

				Connection conn = c.getConnect();

				PreparedStatement ps = null;
				ResultSet rs = null;

				try {
					ps = conn
							.prepareStatement("select *from user where name=? and password=?");

					ps.setString(1, textField.getText());
					ps.setString(2, passwordField.getText());

					rs = ps.executeQuery();
					if (rs.next()) {
						try {
							new login();
							frame.removeNotify();
						} catch (ClassNotFoundException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						

					} else {
						JOptionPane pane = new JOptionPane("用户或密码错误");
	                     JDialog dialog  = pane.createDialog("警告");
	                     dialog.show();
					}

				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}

			

			
		});
		btnNewButton.setBounds(41, 169, 93, 23);
		frame.getContentPane().add(btnNewButton);

		JButton btnNewButton_1 = new JButton("注册");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				new add();
				frame.removeNotify();

			}
		});
		btnNewButton_1.setBounds(183, 169, 93, 23);
		frame.getContentPane().add(btnNewButton_1);
		
		JButton j2 = new JButton("退出");
		j2.setBounds(323, 169, 93, 23);
        frame.getContentPane().add(j2);
        j2.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO 自动生成的方法存根
                if(e.getActionCommand().equals("退出")){
                    System.exit(0);
                }
            }
        });
        
		
		
	}

	private void add(JButton quitBt2) {
		// TODO Auto-generated method stub
		
	}

	private Container getContentPane() {
		// TODO Auto-generated method stub
		return null;
	}


}

注册页面
在这里插入图片描述

详细代码

package com.itheima.denglu;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;


import com.itheima.sql.connectionlain;

public class add {
	private JFrame frame;
	private JTextField textField;
	private JTextField textField_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					add window = new add();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public add() {
		initialize();
	}
	

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(400, 200, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		frame.setResizable(false);
		frame.getContentPane().setLayout(null);

		JLabel lblNewLabel = new JLabel("用户名:");
		lblNewLabel.setBounds(100, 56, 54, 15);
		frame.getContentPane().add(lblNewLabel);

		JLabel lblNewLabel_1 = new JLabel("密码:");
		lblNewLabel_1.setBounds(100, 102, 54, 15);
		frame.getContentPane().add(lblNewLabel_1);

		textField = new JTextField();
		textField.setBounds(196, 53, 100, 21);
		frame.getContentPane().add(textField);
		textField.setColumns(10);

		textField_1 = new JTextField();
		textField_1.setBounds(196, 96,100, 21);
		frame.getContentPane().add(textField_1);
		textField_1.setColumns(10);

		JButton btnNewButton = new JButton("注册");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				connectionlain c = new connectionlain();

				Connection conn = c.getConnect();

				PreparedStatement ps = null;
				try {
					ps = conn
							.prepareStatement("insert into user values(default, ?,?)");

					ps.setString(1, textField.getText());
					ps.setString(2, textField_1.getText());
					ps.execute();

					new prompt();
					frame.removeNotify();
				//  System.out.println("成功");

				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
		});
		btnNewButton.setBounds(81, 169, 93, 23);
		frame.getContentPane().add(btnNewButton);
		
		JButton returnButton = new JButton("返回");
		returnButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				new login();
				frame.removeNotify();

			}
		});
		returnButton.setBounds(223, 169, 93, 23);
		frame.getContentPane().add(returnButton);

	}

}

页面提示
在这里插入图片描述

详细代码

package com.itheima.denglu;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class prompt {
	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					prompt window = new prompt();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public prompt() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(400, 200, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);

		frame.getContentPane().setLayout(null);
		
		JLabel lblNewLabel = new JLabel("注册成功 !");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 41));
		lblNewLabel.setBounds(77, 51, 215, 46);
		frame.getContentPane().add(lblNewLabel);
		
		JButton btnNewButton = new JButton("返回登录");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new login();
				frame.removeNotify();
				
				
			}
		});
		btnNewButton.setBounds(157, 153, 103, 33);
		frame.getContentPane().add(btnNewButton);
	}

}

数据库连接

package com.itheima.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class connectionlain {
	
	public Connection getConnect() {
		Connection conn=null;
		String url="jdbc:mysql://127.0.0.1:3306/student";
		String user="root";
		String password="root";
		try{
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection(url, user, password);
			return conn;
		}catch(ClassNotFoundException e){
		    System.out.println("错误");
		    e.printStackTrace();
		    return null;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}
	

}

注意!

记得导入数据库架包

评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值