小试牛刀-教务信息管理系统(StuAdmin)

package StuAdmin;


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


import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
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.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;


public class StuAdmin implements ActionListener {


	static Connection conn;
	static Statement st;


	String index = "-";
	JFrame f;
	JTextField id;
	JPasswordField pw;


	public StuAdmin() {


		f = new JFrame("教务管理系统");
		f.setLayout(null);
		f.setBounds(450, 200, 450, 300);
		JPanel p = new JPanel();
		p.setBounds(7, 5, 430, 260);
		p.setLayout(null);
		p.setBorder(BorderFactory.createTitledBorder(BorderFactory
				.createLineBorder(new Color(0,0,50), 2), "  教务管理系统  ",
				TitledBorder.CENTER, TitledBorder.TOP));


		JLabel label = new JLabel("USER ID");
		label.setBounds(125,50,80,25);
		p.add(label);
		id = new JTextField();
		id.setBounds(225, 50, 100, 25);
		p.add(id);
		label = new JLabel("PASSWORD");
		label.setBounds(115, 85, 100, 25);
		p.add(label);
		pw = new JPasswordField();
		pw.setBounds(225, 85, 100, 25);
		p.add(pw);


		JRadioButton c1 = new JRadioButton("学生登录");
		JRadioButton c2 = new JRadioButton("教师登录");
		JRadioButton c3 = new JRadioButton("管理员登录");
		c1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				index = "student";
			}
		});
		c2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				index = "teacher";
			}
		});
		c3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				index = "admin";
			}
		});
		ButtonGroup bg = new ButtonGroup();
		bg.add(c1);
		bg.add(c2);
		bg.add(c3);
		c1.setBounds(75, 140, 80, 25);
		c2.setBounds(175, 140, 80, 25);
		c3.setBounds(275, 140, 100, 25);
		p.add(c1);
		p.add(c2);
		p.add(c3);


		JButton b = new JButton("登录");
		b.setBackground(new Color(0, 0, 50));
		b.setFocusPainted(false);
		b.setForeground(Color.WHITE);
		b.addActionListener(this);
		b.setBounds(115, 180, 100, 30);
		p.add(b);
		b = new JButton("忘记密码");
		b.setBackground(new Color(0, 0, 50));
		b.setFocusPainted(false);
		b.setForeground(Color.WHITE);
		b.addActionListener(this);		
		b.setBounds(235, 180, 100, 30);
		p.add(b);


		f.add(p);
		f.setVisible(true);
		f.setResizable(false);
	}


	public static Connection getConnection() {
		Connection con = null; // 创建用于连接数据库的Connection对象
		try {
			Class.forName("com.mysql.jdbc.Driver");// 加载Mysql数据驱动


			con = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/stuadmin", "root", "123456");// 创建数据连接


		} catch (Exception e) {
			System.out.println("数据库连接失败" + e.getMessage());
		}
		return con; // 返回所建立的数据库连接
	}


	public static int login(String table, String id, String pw) {


		conn = getConnection(); // 同样先要获取连接,即连接到数据库
		try {
			String sql = "SELECT * FROM " + table + " WHERE " + table
					+ "id = '" + id + "'"; // 查询数据的sql语句
			st = (Statement) conn.createStatement(); // 创建用于执行静态sql语句的Statement对象,st属局部变量
			ResultSet rs = st.executeQuery(sql); // 执行sql查询语句,返回查询数据的结果集


			while (rs.next()) { // 判断是否还有下一个数据
				String Spw = rs.getString(table + "pw");
				if (pw.equals(Spw)) {
					return 1;
				}
			}
			conn.close(); // 关闭数据库连接


		} catch (SQLException e) {
			System.out.println("查询数据失败");
			return 0;
		}
		return 0;
	}


	public static void main(String[] args) {
		new StuAdmin();
	}


	@SuppressWarnings("deprecation")
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String cmd = e.getActionCommand();
		if (cmd.equals("登录")) {
			if (index == "student") {
				if (login("student", id.getText(), pw.getText()) == 1) {
					f.dispose();
					try {
						new StuBoard(id.getText());
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				} else {
					JOptionPane.showMessageDialog(null, "用户名或密码输入有误!", "error",
							0);
				}
			} else if (index == "teacher") {
				if (login("teacher", id.getText(), pw.getText()) == 1) {
					f.dispose();
					try {
						new TeaBoard(id.getText());
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				} else {
					JOptionPane.showMessageDialog(null, "用户名或密码输入有误!", "error",
							0);
				}
			} else if (index == "admin") {
				if (login("admin", id.getText(), pw.getText()) == 1) {
					f.dispose();
					try {
						new AdmBoard();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				} else {
					JOptionPane.showMessageDialog(null, "用户名或密码输入有误!", "error",
							0);
				}
			} else {
				JOptionPane.showMessageDialog(null, "请选择登录方式!", "error", 0);
			}
		} else if (cmd.equals("忘记密码")) {
			f.dispose();
			new ForgetPw(f);
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值