⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 login.java

📁 需要平台:Myeclips+Mysql或Myeclips+SQL Server 2
💻 JAVA
字号:
package com.view;

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

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

import com.model.DBConn;

public class Login extends JFrame implements ActionListener{
	private Connection conn;
	private Statement sql;
	private ResultSet rs;
	
	private JLabel uname_lb,upw_lb;
	private static JTextField uname_tf;
	private static JPasswordField upw_tf;
	private JButton entry,exit;
	private JPanel uname_jp,upw_jp,namepw_jp,entry_exit_jp;
	public Login(){
		super();
		setSize(200, 150);
		setDefaultCloseOperation(3);
		conn=DBConn.newIntance();
		uname_lb=new JLabel("用户名");
		upw_lb=new JLabel("    密码");
		uname_tf=new JTextField(10);
		upw_tf=new JPasswordField(10);
		entry=new JButton("登录");
		exit=new JButton("退出");
		namepw_jp=new JPanel(new BorderLayout());
		uname_jp=new JPanel();
		upw_jp=new JPanel();
		entry_exit_jp=new JPanel();
		
		entry.addActionListener(this);
		exit.addActionListener(this);
		
		uname_jp.add(uname_lb);
		uname_jp.add(uname_tf);
		upw_jp.add(upw_lb);
		upw_jp.add(upw_tf);
		namepw_jp.add(uname_jp,"North");
		namepw_jp.add(upw_jp,"South");
		entry_exit_jp.add(entry);
		entry_exit_jp.add(exit);
		this.getContentPane().setLayout(new BorderLayout());
		
		this.getContentPane().add(namepw_jp,"North");
		this.getContentPane().add(entry_exit_jp,"South");
		
		this.setVisible(true);
		
	}
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==entry){
			boolean bo=false;
			//new GUIinit();
			//this.dispose();
			try {
				sql=conn.createStatement();
				rs=sql.executeQuery("select * from users");
				System.out.println(uname_tf.getText());
				System.out.println(upw_tf.getPassword());
				while(rs.next()){
					String uname=uname_tf.getText().trim();
					//String password=upw_tf.getPassword().toString().trim();
					//getpassword问题?
					String password=upw_tf.getText().trim();
					System.out.println(rs.getString(2));
					System.out.println(rs.getString(3));
					System.out.println(uname_tf.getText().equals(rs.getString(2)));
					System.out.println(upw_tf.getPassword().equals(rs.getString(3)));
					/*if(uname_tf.getText().equals(rs.getString(2))&&upw_tf.getPassword().equals(rs.getString(3))){
						System.out.println(rs.getString(2));
						System.out.println(rs.getString(3));
						new GUIinit();
						this.dispose();
						break;
					}
					else{
						continue;
					}*/
					if(uname.equals(rs.getString(2))&&password.equals(rs.getString(3))){
						//System.out.println(rs.getString(2));
						//System.out.println(rs.getString(3));
						bo=true;
						new GUIinit();
						this.dispose();
						break;
					}
					else{
						
						continue;
					}
				}
				if(!bo){
					JOptionPane.showMessageDialog(null, "用户名或密码错误,请重新输入。" );
					uname_tf.setText("");
					upw_tf.setText("");
					uname_tf.requestFocus();
				}
				
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		}
		if(e.getSource()==exit){
			System.exit(0);
		}
		
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -