📄 user_manager.java
字号:
package user;
import java.sql.*;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.sql.DriverManager;
public class user_manager extends JFrame implements ActionListener
{
private static final long serialVersionUID = -7700451926035976789L;
JPanel cp;
int i1=0;//复选框选择记录
String msg=new String();
JScrollPane jsp = new JScrollPane();
ImageIcon icon=new ImageIcon("picture/12.jpg");
JLabel tp=new JLabel(icon,JLabel.CENTER);
Object[][] rowData=new Object[50][3];
String[] columnNames = {
"账号","密码","用户类型"};
JTable jT1 = new JTable(rowData, columnNames) ;
JLabel j1=new JLabel("用户名:");
JLabel j2=new JLabel("密码: ");
JLabel j3=new JLabel("用户类型:");
JComboBox cbx1=new JComboBox();
JButton b1=new JButton("查 询");
JButton b2=new JButton("删 除");
JButton b3=new JButton("解 锁");
JButton b4=new JButton("刷 新");
JButton b5=new JButton("返 回");
JButton b6=new JButton("指派管理员");
JButton b7=new JButton("取消管理员");
JTextField jt1=new JTextField(16);
JTextField jt2=new JTextField(16);
public user_manager()
{try{Init();if(new quanxian().get_quanxian()==1) {b6.setEnabled(false);b7.setEnabled(false);}
}catch (Exception err){err.printStackTrace();}}
public void Init() throws Exception{
cp=(JPanel)this.getContentPane();
cp.setLayout(null);
this.setSize(600,400);
this.setTitle("系统用户管理");
b1.setBounds(460,20,70,25);
b1.setFont(new java.awt.Font("Dialog",0,15));
b1.addActionListener(this);
cp.add(b1);
b2.setBounds(460,60,70,25);
b2.setFont(new java.awt.Font("Dialog",0,15));
b2.addActionListener(this);
cp.add(b2);
b3.setBounds(460,100,70,25);
b3.setFont(new java.awt.Font("Dialog",0,15));
b3.addActionListener(this);
cp.add(b3);
b4.setBounds(460,140,70,25);
b4.setFont(new java.awt.Font("Dialog",0,15));
b4.addActionListener(this);
cp.add(b4);
b5.setBounds(460,180,70,25);
b5.setFont(new java.awt.Font("Dialog",0,15));
b5.addActionListener(this);
cp.add(b5);
b6.setBounds(435,220,120,25);
b6.setFont(new java.awt.Font("Dialog",0,15));
b6.addActionListener(this);
cp.add(b6);
b7.setBounds(435,260,120,25);
b7.setFont(new java.awt.Font("Dialog",0,15));
b7.addActionListener(this);
cp.add(b7);
j1.setBounds(30,260,60,25);
j1.setForeground(new Color(255,0,0));
j1.setFont(new java.awt.Font("Dialog",0,15));
cp.add(j1,null);
jt1.setBounds(90,260,100,25);
jt1.setFont(new java.awt.Font("Dialog",0,15));
cp.add(jt1,null);
j3.setBounds(220,260,80,25);
j3.setForeground(new Color(255,0,0));
j3.setFont(new java.awt.Font("Dialog",0,15));
cp.add(j3,null);
cbx1.setBounds(300,260,100,25);
cbx1.setFont(new java.awt.Font("Dialog",0,14));
if(new quanxian().get_quanxian()==0)
{cbx1.addItem("管理员");
cbx1.addItem("普通用户");}
else{cbx1.addItem("普通用户");cbx1.setEnabled(false);}
cp.add(cbx1,null);
jsp.setBounds(6,8,400,225);
jsp.getViewport().add(jT1,null);
cp.add(jsp);
tp.setSize(600,400);
tp.setText("");
cp.add(tp,null);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
java.sql.Connection con=DriverManager.getConnection("jdbc:odbc:yundonghui","sa","123");
Statement ps=con.createStatement();
ResultSet rs = null;
if(new quanxian().get_quanxian()==1)
{ rs=ps.executeQuery("select * from 用户管理 where 用户类型='普通用户' order by 用户类型");}
else{rs=ps.executeQuery("select * from 用户管理 where 用户类型!='系统管理员' order by 用户类型 ");}
jT1.setModel(new javax.swing.table.DefaultTableModel());
Object[][] rowData=new Object[50][4];
String[] columnNames = {
"账号","密码","用户类型","锁定"};
JTable jT1 = new JTable(rowData, columnNames);
jsp.getViewport().add(jT1,null); //加载JTable对象
int i=0;
while(rs.next())
{
rowData[i][0] = rs.getString(1);
rowData[i][1] = rs.getString(2);
rowData[i][2]=rs.getString(3);
rowData[i][3]=rs.getString(4);
i++;
}
}catch (Exception err){err.printStackTrace();JOptionPane.showMessageDialog(null,"数据库连接出错!");}
}
public static void main(String args[])
{new user_manager();}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b5){this.dispose();}
else
try
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:yundonghui","sa","123");
Statement ps=con.createStatement();
ResultSet rs=null;
if(e.getSource()==b1)
{if(jt1.getText().equals(""))
{rs=ps.executeQuery("select * from 用户管理 where 用户类型='"+cbx1.getSelectedItem().toString()+"' ");
if(!rs.next())
{
JOptionPane.showMessageDialog(null, "没有记录!");
if(new quanxian().get_quanxian()==1)
{ rs=ps.executeQuery("select * from 用户管理 where 用户类型='普通用户' ");}
else{rs=ps.executeQuery("select * from 用户管理 where 用户类型!='系统管理员' ");}
}
else{rs=ps.executeQuery("select * from 用户管理 where 用户类型='"+cbx1.getSelectedItem().toString()+"' ");i1=1;msg=cbx1.getSelectedItem().toString();}
}
else{ if(new quanxian().get_quanxian()==0) {rs=ps.executeQuery("select * from 用户管理 where 用户名='"+jt1.getText().trim()+"' and 用户类型!='系统管理员' ");}
else{rs=ps.executeQuery("select * from 用户管理 where 用户名='"+jt1.getText().trim()+"' and 用户类型='普通用户' ");}
if(!rs.next())
{
JOptionPane.showMessageDialog(null, "没有记录!");
if(new quanxian().get_quanxian()==1)
{ rs=ps.executeQuery("select * from 用户管理 where 用户类型='普通用户' ");}
else{rs=ps.executeQuery("select * from 用户管理 where 用户类型!='系统管理员' ");}
}
else{ if(new quanxian().get_quanxian()==0) {rs=ps.executeQuery("select * from 用户管理 where 用户名='"+jt1.getText().trim()+"' and 用户类型!='系统管理员' ");}
else{rs=ps.executeQuery("select * from 用户管理 where 用户名='"+jt1.getText().trim()+"' and 用户类型='普通用户' ");}i1=2;msg=jt1.getText().trim(); }
}
}
if(e.getSource()==b2)
{if(i1==0){JOptionPane.showMessageDialog(null, "请查询出要删除的用户!");}
else if(i1==1)
{int v=JOptionPane.showConfirmDialog(this,"确认删除所有("+msg+")吗?","删除确认",JOptionPane.OK_CANCEL_OPTION);
if(v==JOptionPane.OK_OPTION){ps.executeUpdate("delete from 用户管理 where 用户类型='"+msg+"'");msg="";cbx1.setSelectedItem("管理员");}
}
else
{int v=JOptionPane.showConfirmDialog(this,"确认删除("+msg+")吗?","删除确认",JOptionPane.OK_CANCEL_OPTION);
if(v==JOptionPane.OK_OPTION){ps.executeUpdate("delete from 用户管理 where 用户名='"+msg+"'");msg="";jt1.setText("");}
}
}
else if(e.getSource()==b3)
{
if(i1==0)
{ int v=JOptionPane.showConfirmDialog(this,"确认解除所有锁定的账号吗?","解锁确认",JOptionPane.OK_CANCEL_OPTION);
if(v==JOptionPane.OK_OPTION){if(new quanxian().get_quanxian()==1)ps.executeUpdate("update 用户管理 set 锁定='未锁定'where 用户类型!='管理员'");
else ps.executeUpdate("update 用户管理 set 锁定='未锁定'");}
}
else if(i1==1) //i1==1:通过用户类型查询
{int v=JOptionPane.showConfirmDialog(this,"确认解除所有锁定的("+msg+")账号吗?","解锁确认",JOptionPane.OK_CANCEL_OPTION);
if(v==JOptionPane.OK_OPTION){ps.executeUpdate("update 用户管理 set 锁定='未锁定' where 用户类型='"+msg+"'");}
}
else
{ rs=ps.executeQuery("select * from 用户管理 where 用户名='"+msg+"'");
rs.next();
if(rs.getString(4).trim().equals("未锁定")){JOptionPane.showMessageDialog(null, "此账号未被锁定!");}
else{ int v=JOptionPane.showConfirmDialog(this,"确认解除("+msg+")的锁定吗?","解锁确认",JOptionPane.OK_CANCEL_OPTION);
if(v==JOptionPane.OK_OPTION){ps.executeUpdate("update 用户管理 set 锁定='未锁定' where 用户名='"+msg+"'");}}
}
}
else if(e.getSource()==b6){
if(i1==0||i1==1){JOptionPane.showMessageDialog(null, "请先查询要指派的管理员用户");}
else{
rs=ps.executeQuery("select * from 用户管理 where 用户名='"+msg+"'");
rs.next();
if(rs.getString(3).trim().equals("管理员")){JOptionPane.showMessageDialog(null, "此用户已是管理员!");}
else{
ps.executeUpdate("update 用户管理 set 用户类型='管理员' where 用户名='"+msg+"'");}
}
}
else if(e.getSource()==b7)
{
if(i1==0||i1==1){JOptionPane.showMessageDialog(null, "请先查询要取消的管理员用户");}
else{
rs=ps.executeQuery("select * from 用户管理 where 用户名='"+msg+"'");
rs.next();
if(!rs.getString(3).trim().equals("管理员")){JOptionPane.showMessageDialog(null, "此用户不是管理员!");}
else{
ps.executeUpdate("update 用户管理 set 用户类型='普通用户' where 用户名='"+msg+"'");} }
}
if(e.getSource()!=b1)
{ if(new quanxian().get_quanxian()==1)
{ rs=ps.executeQuery("select * from 用户管理 where 用户类型='普通用户'");}
else{rs=ps.executeQuery("select * from 用户管理 where 用户类型!='系统管理员' ");};
msg="";i1=0;jt1.setText("");
}
jT1.setModel(new javax.swing.table.DefaultTableModel());
Object[][] rowData=new Object[50][4];
String[] columnNames = {
"账号","密码","用户类型","锁定"};
JTable jT1=new JTable(rowData, columnNames); //创建新的JTable对象
jsp.getViewport().add(jT1,null); //加载JTable对象
int i=0;
while(rs.next())
{
rowData[i][0] = rs.getString(1);
rowData[i][1] = rs.getString(2);
rowData[i][2]=rs.getString(3);
rowData[i][3]=rs.getString(4);
i++;
}
}catch(Exception err){err.printStackTrace();JOptionPane.showMessageDialog(null,"数据库连接错误!");}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -