欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java连接数据库,实现登录页面

程序员文章站 2022-06-28 23:04:57
1.倒入jar包,使用Driver2.package mySQL;import java.awt.Color;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Font;import java.awt.Graphics;import java.awt.GridLayout;import java.awt.Toolkit;import java.awt.event.ActionEvent;impor...

1.倒入jar包,使用Driver
2.

package mySQL;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.ImageIcon;
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;

public class 登录首页 {

	JFrame f;
	JLabel l1,l2,lv,l,lc,lt;
	JTextField t1,tv;
	JPasswordField t2;
	JButton b1,b2,b3;
	JPanel p1,p2,p3,p;
	BufferedImage image;
	String scode="";
	void init() {
		f=new JFrame("登录窗口");
		l1=new JLabel("学    号",JLabel.RIGHT);
        lv=new JLabel("验证码");
        lt=new JLabel("用户登录",JLabel.CENTER);
        lt.setFont(new Font("宋体",Font.BOLD,18));
        image=getImage();
        l=new JLabel(new ImageIcon(image));
		l2=new JLabel("姓    名",JLabel.RIGHT);
		t1=new JTextField(10);
		tv=new JTextField(10);
		t2=new JPasswordField(10);
		b1=new JButton("确定");
		b2=new JButton("取消");
		b3=new JButton("新用户注册");
		MyListener ll=new MyListener();
		b1.addActionListener(ll);
		b2.addActionListener(ll);
		b3.addActionListener(ll);
		p1=new JPanel(new FlowLayout(FlowLayout.LEFT));
		p2=new JPanel(new FlowLayout(FlowLayout.LEFT));
		p3=new JPanel();
		p=new JPanel(new FlowLayout(FlowLayout.LEFT));
		p1.add(l1);
		p1.add(t1);
		p2.add(l2);
		p2.add(t2);
		p3.add(b1);
		p3.add(b2);
		p3.add(b3);
		p.add(lv);
		p.add(tv);
		p.add(l);
		lc=new JLabel("看不清,换一张");
		lc.setFont(new Font("宋体", Font.ITALIC, 10));
		lc.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent arg0) {
				super.mouseClicked(arg0);
				image=getImage();
				l.setIcon(new ImageIcon(image));
			}
			public void mouseEntered(MouseEvent arg0) {
				super.mouseEntered(arg0);
				lc.setForeground(Color.green);
			}
		});
		p.add(lc);
		f.setLayout(new GridLayout(5,1));
		f.add(lt);
		f.add(p1);
		f.add(p2);
		f.add(p);
		f.add(p3);
		f.setSize(400,300);
		Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();
		int x=(int)((dim.getWidth()-f.getWidth())/2);
		int y=(int)((dim.getHeight()-f.getHeight())/2);
		f.setLocation(x, y);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	BufferedImage getImage() {
		image=new BufferedImage(60, 20,BufferedImage.TYPE_INT_RGB);
		Graphics g=image.getGraphics();
		g.setColor(Color.white);
		g.fillRect(0, 0, 60, 20);
		g.setColor(Color.red);
		g.setFont(new Font("宋体", Font.BOLD, 15));
		String st="";
		for(int i=0;i<4;i++) {
			int x=(int)(Math.random()*10);
			st=String.valueOf(x);
			scode+=st;
			g.drawString(st, 15*i+5, 12);
		}
		for(int i=0;i<100;i++) {
			g.setColor(new Color(50+(int)(Math.random()*100), 50+(int)(Math.random()*100), 50+(int)(Math.random()*100)));
			g.drawOval((int)(Math.random()*60), (int)(Math.random()*20), 1, 1);
		}
		return image;
	}
	class MyListener implements ActionListener {
			 public void actionPerformed(ActionEvent e) {
			 	if(e.getSource()==b1) {
					 String uName=t1.getText(); //用户输入的用户名
			    	char s[]=t2.getPassword();
			    	String uPass=String.valueOf(s); //用户输入的密码
			    	String ucode = tv.getText();//用户输入的验证码
			    	System.out.println("输入的用户名"+uName+",密码:"+uPass+",验证码:"+ucode+",下面开始验证。。。");
			    	/*登录控制:
			    	 * 1.判断验证码
			    	 *    	验证码正确
			    	 *    		验证用户名
			    	 *    		验证密码
			    	 *    验证码错误:提示
			    	*/
			    	if(ucode.equals(scode)) {
			    		
			    	}else {
			    		JOptionPane.showMessageDialog(null,"验证失败");
			    	}
			    	Connection con = null;
					Statement st= null;
					ResultSet rs= null;
					try {
						Class.forName("com.mysql.jdbc.Driver");
						String url="jdbc:mysql://127.0.0.1:3306/gradem";
						con = DriverManager.getConnection(url, "root", "001201");
						st = con.createStatement();
						String sql = "select * from student where sno='"+uName+"'and sname='"+uPass+"'";
						rs = st.executeQuery(sql);
						System.out.println(sql);
						if(rs.next()) {
							//System.out.println(rs.getRow()+":"+uName+"\t"+uPass);
							JOptionPane.showMessageDialog(f,"登录成功");
						}else {
							JOptionPane.showMessageDialog(f,"登录失败");
						}
					} catch (ClassNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}finally {
						if(rs!=null)
							try {
								rs.close();
							} catch (SQLException e1) {
								e1.printStackTrace();
							}
						if(st!=null)
							try {
								st.close();
							} catch (SQLException e1) {
								e1.printStackTrace();
							}
						if(con!=null)
							try {
								con.close();
							} catch (SQLException e1) {
								e1.printStackTrace();
							}
					}
				
			 	}
			 	else if(e.getSource()==b3) {
			 		f.dispose();
			 		new 用户注册().init();
			 	}
			 		
			 	else System.exit(0);
	    	
	    }
	}
   
    public static void main(String args[]) {
    	登录首页 l=new 登录首页();
    	l.init();
    }
    

}

本文地址:https://blog.csdn.net/m0_46641784/article/details/112005609

相关标签: java作业