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

📄 book_login.java

📁 图书馆管理系统 用swing界面做的一个图书馆管理系统 里面有增删改等功能 适合初学者看看
💻 JAVA
字号:

//登陆窗口
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.awt.event.*;
import java.net.*;

public class book_login extends JFrame implements ActionListener{
	
	JTextField tland=new JTextField(15);
	JPasswordField tpassword=new JPasswordField(15);
	
	JButton bland=new JButton("登 陆");
	JButton bcancle=new JButton("取 消");
	
	JLabel lland=new JLabel("登陆账户",JLabel.CENTER);
	JLabel lpassword=new JLabel("登陆密码",JLabel.CENTER);
	
	JLabel la_info=new JLabel("请输入账户和密码");
	
	Connection conn = null;
	
	public book_login(){
		super("登陆图书馆管理系统");
		this.setLayout(null);
		
		JPanel p_up =new JPanel();
		JPanel p_down=new JPanel();//文字提示面板
		
		p_up.setBounds(25,20,250,100); //登陆界面
		p_up.setLayout(new GridLayout(3,2,5,5));
		
		
		//默认账户和密码
	    tland.setText("admin");
	    tpassword.setText("1111");
	    	    
		p_up.add(lland);
        p_up.add(tland);
		p_up.add(lpassword);
		p_up.add(tpassword);
		p_up.add(bland);
		p_up.add(bcancle);
		this.add(p_up);
		
		p_down.setBounds(25,120,250,50);//文字提示面板
		p_down.add(la_info);
		this.add(p_down);
		
		//主窗口
		this.setBounds(300,300,300,200);
		this.setVisible(true);
		this.validate();
		this.setResizable(false);
		
		//注册监听器
		bland.addActionListener(this);
		bcancle.addActionListener(this);
		
		
		//连接数据库
		conn = book_sql.getConn();
		//System.out.println(conn);	
	}
	public void actionPerformed(ActionEvent e){
		
		if(e.getSource()==bcancle){//退出按钮
			System.exit(0);
		}
		
		if(e.getSource()==bland){ //登陆按钮
			    
			String sland=tland.getText().trim();
			char[] temp = tpassword.getPassword();//密码****显示框
	    	String spassword = new String(temp);
			
			if(sland.equals("")&&spassword.equals("")){
				la_info.setText("Sorry! 账户和密码不能为空");
				return;
			}
			if(sland.equals("")){
				la_info.setText( "Sorry! 账户不能为空");
				return;
			}
			if(spassword.equals("")){
				la_info.setText(" Sorry! 密码不能为空");
				return;
			}else{
				
				ResultSet rs = null;
				Statement st = null;
							
				//方法1
				String sql = "select* from user where name = '" + sland + "'"
			    			+ " and password = '" + spassword + "'";
			    //System.out.println(sql);		    
			    try{
			    	st=conn.createStatement();
			    	rs=st.executeQuery(sql);
			    	if(rs.next()){
			    		
			    		la_info.setText("恭喜,你可以进入了!");		    		
			    		new book_main();//进入主页面
			    		this.dispose();
			    		
			    	}else  la_info.setText("账号和密码有误或没权限进入");
			    }catch(SQLException sqle){
			    	sqle.printStackTrace();
			    }
			}
		//			    			
		}
	}
    public static void main(String[] args){
    	new book_login();
    }
}

      //连接数据库
    /*
		try{
			Class.forName("com.mysql.jdbc.Driver");
		}catch(ClassNotFoundException e){
			System.out.println("找不到指定的驱动包类");
			}		
		try{
			String url="jdbc:mysql://localhost/bookstore?useUnicode = true&characterEncoding = UTF-8";
			conn = DriverManager.getConnection(url,"root","123456");
			st =conn.createStatement();
		}catch(SQLException e){
			e.printStackTrace();
			}
			
			
		//进入主页面方法2
		    
		 String sql = "select * from user";
	     //System.out.println(sql);
		try{
		    //System.out.print(st);
		    rs=st.executeQuery(sql);
		    while(rs.next()){
			if(rs.getString(1).equals(sland)&&rs.getString(2).equals(spassword))
			System.out.print("恭喜你,你可以进入了");
			break;
			}			
		}catch(SQLException sqle){
			sqle.printStackTrace();
		}
	*/

⌨️ 快捷键说明

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