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

📄 user.java

📁 管理学生信息
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
class User extends JFrame implements ActionListener //插入用户
{
	JButton ok,cancel;
	JRadioButton teacher,del,sert,manager,student;
	JTextField text0;
	Choice text1;
	JPasswordField text2;
	User(){
		super("插入/删除用户");
		JLabel label0,label1,label2;
		setLayout(null);
		setSize(500,400);
		setVisible(true);
		setBackground(Color.green);
		label0=new JLabel("*请选择用户名:");
		label1=new JLabel("*请输入用户名:");
		label2=new JLabel("*请输入密码:");
		text0=new JTextField(20);
		text1=new Choice();
		text2=new JPasswordField(20);
		add(label0);add(text0);
		add(label1);add(text1);add(label2);
		add(text2);

		student=new JRadioButton("学生");
		teacher=new JRadioButton("老师");
		manager=new JRadioButton("管理员",true);
		del=new JRadioButton("删除");
		sert=new JRadioButton("添加",true);
		add(del);add(sert);
		ButtonGroup quanxian=new ButtonGroup();//将单选按钮分组,使其成为单选
		ButtonGroup operation=new ButtonGroup();
		ok=new JButton("确定");
		cancel=new JButton("取消");
		add(teacher);
		add(manager);
		add(student);
		quanxian.add(student);
		quanxian.add(teacher);
		quanxian.add(manager);
		operation.add(del);
		operation.add(sert);
		add(ok);
		add(cancel);
		ok.addActionListener(this);
		cancel.addActionListener(this);
		teacher.addActionListener(this);
		manager.addActionListener(this);
		student.addActionListener(this);
		label0.setBounds(50,50,100,30);
		label1.setBounds(50,100,100,30);
		label2.setBounds(50,150,100,30);
		text0.setBounds(150,50,200,30);
		text1.setBounds(150,100,200,30);
		text2.setBounds(150,150,200,30);
		student.setBounds(50,200,100,30);
		teacher.setBounds(150,200,100,30);
		manager.setBounds(250,200,100,30);
		del.setBounds(50,250,100,30);
		sert.setBounds(150,250,100,30);
		ok.setBounds(50,300,100,30);
		cancel.setBounds(250,300,100,30);
	}
	public void actionPerformed(ActionEvent e){
		if(e.getSource()==ok){
			int k;
			String sk;
			if(student.isSelected())
			{
				k=2;
				sk=text1.getSelectedItem();
			}
			else
				if(teacher.isSelected())
				{	
					k=1;
					sk=text1.getSelectedItem();
				}
				else
				{
					k=0;
					sk=text0.getText();
				}
		Connection con=null;
		Statement sm=null;
		String command=null;
		try{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			con=DriverManager.getConnection("jdbc:odbc:student");
		}catch(Exception ex){
			System.out.println(ex.getMessage());
		return;
		}
		try{
			sm=con.createStatement();
			command="select * from yonghu where username='"+sk+"'";
			ResultSet re=sm.executeQuery(command);
			if(re.next())
			{
				if(sert.isSelected())
					JOptionPane.showMessageDialog(this,"该人以在表中!","warring",JOptionPane.WARNING_MESSAGE);
				else
				{
					if(String.valueOf(re.getString("pws").trim()).compareTo(String.valueOf(text2.getPassword()).trim())==0){
						k=JOptionPane.showConfirmDialog(this,"你确认要删除吗?");
						if(k==0){
						command="delete from yonghu where username='"+sk+"'";
						sm.executeUpdate(command);
						JOptionPane.showMessageDialog(this,"删除成功","删除成功",JOptionPane.INFORMATION_MESSAGE);
						}
					}
					else
						JOptionPane.showMessageDialog(this,"warring","密码错误!",JOptionPane.WARNING_MESSAGE);
				}
			}
			else{
				if(sert.isSelected())
				{
					command="insert into yonghu(username,pws,arth) values('"+sk+"','"+String.valueOf(text2.getPassword())+"','"+k+"')";
					sm.executeUpdate(command);
					JOptionPane.showMessageDialog(this,"插入成功","插入成功",JOptionPane.INFORMATION_MESSAGE);
				}
				else
					JOptionPane.showMessageDialog(this,"warring","查无此人!",JOptionPane.WARNING_MESSAGE);
			}
		}catch(Exception ey){
			JOptionPane.showMessageDialog(this,"有带*的空未填入数据","warring",JOptionPane.WARNING_MESSAGE);
		}
		try{
			sm.close();
			con.close();
			dispose();
		}catch(Exception ez){}
		setVisible(false);
		}
		if(e.getSource()==cancel)
		{
			dispose();
			setVisible(false);
		}
		if(e.getSource()==manager)
		{
			text1.removeAll();
			text0.setEditable(true);
		}
		if(e.getSource()==student)
		{
			text1.removeAll();
			text0.setEditable(false);
			Connection con=null;
			Statement sm=null;
			String command=null;
			try{
				Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
				con=DriverManager.getConnection("jdbc:odbc:student");
			}catch(Exception ex){
				System.out.println(ex.getMessage());
			return;
			}
			try{
				sm=con.createStatement();
				command="select sno from st";
				ResultSet rs=sm.executeQuery(command);
				while(rs.next()){
					text1.add(rs.getString(1).trim());
				}
				sm.close();
				con.close();
			}catch(SQLException ee){
				System.out.println(ee.getMessage());
			}
		}
		if(e.getSource()==teacher)
		{
			text1.removeAll();
			text0.setEditable(false);
			Connection con=null;
			Statement sm=null;
			String command=null;
			try{
				//DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
				Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
				con=DriverManager.getConnection("jdbc:odbc:student");
			}catch(Exception ex){
				System.out.println(ex.getMessage());
			return;
			}
			try{
				sm=con.createStatement();
				command="select teacher from ch";
				ResultSet rs=sm.executeQuery(command);
				while(rs.next()){
					text1.add(rs.getString(1).trim());
				}
				sm.close();
				con.close();
			}catch(SQLException ee){
				System.out.println(ee.getMessage());
			}
		}
	}
}

⌨️ 快捷键说明

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