📄 addorchangeuser.java
字号:
//添加和修改用户窗口
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.sql.*;
public class AddOrChangeUser extends JFrame implements ActionListener{
static final long serialVersionUID=80;
JLabel userlabel,passwordlabel,confirmlabel,typelabel;
JTextField userfield;
JPasswordField passwordfield,confirmfield;
JComboBox typebox;
JButton createbutton,cancelbutton;
Container container;
Dimension screenSize;
Toolkit tool;
String[] combobox={"Common","Book","Borrow","System"};
Connection con;
Statement sql;
boolean add;
JTable table;
Image myimage;
Object[][] userresult;
AddOrChangeUser(String title,Connection con,boolean add,JTable table,Object[][] userresult){
super(title);
this.con=con;
this.add=add;
this.userresult=userresult;
this.table=table;
tool=getToolkit();
URL url = getClass().getResource("/images/USER.GIF");
if(url!=null){
myimage=tool.getImage(url);
setIconImage(myimage);
}
screenSize=tool.getScreenSize();
container=getContentPane();
container.setBackground(Color.ORANGE);
container.setLayout(null);
setResizable(false);
setSize(350,150);
userlabel=new JLabel("用户名");
passwordlabel=new JLabel("密码");
confirmlabel=new JLabel("确认密码");
typelabel=new JLabel("类型");
userfield=new JTextField(20);
passwordfield=new JPasswordField(20);
confirmfield=new JPasswordField(20);
typebox=new JComboBox(combobox);
if(add){
createbutton=new JButton("创建");
}
else{
userfield.setText("空即不修改");
createbutton=new JButton("修改");
}
cancelbutton=new JButton("取消");
createbutton.addActionListener(this);
cancelbutton.addActionListener(this);
container.add(userlabel);
userlabel.setBounds(20,3,40,30);
container.add(userfield);
userfield.setBounds(60,8,150,20);
container.add(typelabel);
typelabel.setBounds(220,3,30,30);
container.add(typebox);
typebox.setBounds(255,6,80,24);
container.add(passwordlabel);
passwordlabel.setBounds(30,40,30,30);
container.add(passwordfield);
passwordfield.setBounds(60,45,150,20);
container.add(confirmlabel);
confirmlabel.setBounds(10,80,50,30);
container.add(confirmfield);
confirmfield.setBounds(60,85,150,20);
container.add(createbutton);
createbutton.setBounds(220,42,110,27);
container.add(cancelbutton);
cancelbutton.setBounds(220,82,110,27);
setLocation((screenSize.width-getSize().width)/2,(screenSize.height-getSize().height)/2);
setVisible(true);
validate();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
if(sql!=null){
try{
sql.close();
}
catch(SQLException e3){
JOptionPane.showMessageDialog(null,"计算机已与服务器断开");
}
}
dispose();
}
});
try{
sql=con.createStatement();
}
catch(SQLException e3){
JOptionPane.showMessageDialog(this,"计算机已与服务器断开");
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==createbutton){
String user=userfield.getText().trim();
String password=new String(passwordfield.getPassword());
String confirm=new String(confirmfield.getPassword());
String type=(String)typebox.getSelectedItem();
if(add){
if(!password.equals(confirm)){
JOptionPane.showMessageDialog(this,"请确定一定以及肯定两次输入的密码一样!","警告对话框",JOptionPane.WARNING_MESSAGE);
return;
}
if(user.length()>=4&user.length()<=20&password.length()>=8&password.length()<=20&confirm.length()>=8&confirm.length()<=20){
try{
user="'"+user+"'";
password="'"+password+"'";
String temp="INSERT INTO User_Info VALUES ("+user+","+password+","+"'"+type+"')";
int n=sql.executeUpdate(temp);
if(n==1){
JOptionPane.showMessageDialog(this,"创建用户成功","消息",JOptionPane.INFORMATION_MESSAGE);
}
}
catch(SQLException e2){
JOptionPane.showMessageDialog(this,"用户名已存在");
}
}
else{
JOptionPane.showMessageDialog(this,"请输入正确长度的用户名与密码","警告对话框",JOptionPane.WARNING_MESSAGE);
}
}
else{
if(!password.equals(confirm)){
JOptionPane.showMessageDialog(this,"请确定一定以及肯定两次输入的密码一样!","警告对话框",JOptionPane.WARNING_MESSAGE);
return;
}
if(user.length()!=0){
JOptionPane.showMessageDialog(this,"用户名不能修改");
return;
}
String change="UPDATE User_Info SET ";
Object[][] temp=userresult;
if(password.length()!=0){
if(password.length()>=8&&password.length()<=20){
change+="UserPassword = "+"'"+password+"',";
userresult[table.getSelectedRow()][1]=password;
}
else{
JOptionPane.showMessageDialog(this,"密码长度要在8和20之间");
return;
}
}
userresult[table.getSelectedRow()][2]=type;
change+="UserPurview = "+"'"+type+"'"+" WHERE UserName = "+"'"+userresult[table.getSelectedRow()][0]+"'";
try{
int n=sql.executeUpdate(change);
if(n==1){
JOptionPane.showMessageDialog(this,"修改成功");
table.repaint();
}
else{
JOptionPane.showMessageDialog(this,"修改失败");
userresult=temp;
}
}
catch(SQLException e3){
JOptionPane.showMessageDialog(this,"计算机已与服务器断开");
userresult=temp;
}
}
}
else{
if(sql!=null){
try{
sql.close();
}
catch(SQLException e3){
JOptionPane.showMessageDialog(this,"计算机已与服务器断开");
}
}
dispose();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -