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

📄 userinfo.java

📁 Java数据库系统项目开发实践源程序第4章.rar
💻 JAVA
字号:
package enterpriseemployeeadministrate;

import javax.swing.*;
import java.awt.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
import com.borland.dx.sql.dataset.*;
import java.io.*;
import java.sql.*;


public class UserInfo extends JFrame{
  String[] userType={"","系统管理员","高级用户","普通用户"};
  JPanel jPanel1 = new JPanel();
  XYLayout xYLayout1 = new XYLayout();
  JLabel jLabel1 = new JLabel();
  JTextField jTextField1 = new JTextField();
  JLabel jLabel2 = new JLabel();
  JPasswordField jPasswordField1 = new JPasswordField();
  JLabel jLabel3 = new JLabel();
  JComboBox jComboBox1 = new JComboBox(userType);
  JLabel jLabel4 = new JLabel();
  JTextField jTextField2 = new JTextField();
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JButton jButton3 = new JButton();
  JButton jButton4 = new JButton();
  Database database1 = new Database();
  XYLayout xYLayout2 = new XYLayout();

  public UserInfo() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  private void jbInit() throws Exception {

    jPanel1.setLayout(xYLayout1);
    this.getContentPane().setBackground(UIManager.getColor("InternalFrame.activeTitleGradient"));
    this.setForeground(UIManager.getColor("InternalFrame.activeTitleGradient"));
    this.getContentPane().setLayout(xYLayout2);
    jLabel1.setText("会员号");
    jTextField1.setSelectionStart(11);
    jTextField1.setText("");
    jLabel2.setText("密        码");
    jPasswordField1.setText("");
    jLabel3.setText("会员类别");
    jLabel4.setText("姓     名");
    jTextField2.setText("");
    jButton1.setText("添加");
    jButton1.addActionListener(new UserInfo_jButton1_actionAdapter(this));
    jPanel1.setBackground(UIManager.getColor("InternalFrame.activeTitleGradient"));
    jPanel1.setForeground(UIManager.getColor("InternalFrame.activeTitleGradient"));
    jComboBox1.setBackground(UIManager.getColor("EditorPane.background"));
    jButton2.setText("删除");
    jButton2.addActionListener(new UserInfo_jButton2_actionAdapter(this));
    jButton3.setText("更改");
    jButton3.addActionListener(new UserInfo_jButton3_actionAdapter(this));
    jButton4.setText("取消");
    jButton4.addActionListener(new UserInfo_jButton4_actionAdapter(this));
    database1.setConnection(new com.borland.dx.sql.dataset.ConnectionDescriptor("jdbc:odbc:person", "", "", false, "sun.jdbc.odbc.JdbcOdbcDriver"));
    jPanel1.add(jTextField1,     new XYConstraints(93, 34, 81, 24));
    jPanel1.add(jLabel2,     new XYConstraints(195, 35, 55, 22));
    jPanel1.add(jComboBox1,       new XYConstraints(276, 87, 81, 24));
    jPanel1.add(jLabel3,        new XYConstraints(195, 90, 67, 22));
    jPanel1.add(jTextField2,     new XYConstraints(93, 92, 81, 24));
    jPanel1.add(jLabel1,  new XYConstraints(21, 36, 65, -1));
    jPanel1.add(jButton1,  new XYConstraints(36, 165, 60, 22));
    jPanel1.add(jButton2,    new XYConstraints(113, 165, 60, 22));
    jPanel1.add(jButton3,    new XYConstraints(189, 165, 60, 22));
    jPanel1.add(jButton4,      new XYConstraints(267, 163, 60, 22));
    jPanel1.add(jPasswordField1, new XYConstraints(276, 34, 81, 24));    jPanel1.add(jLabel4, new XYConstraints(21, 96, 69, -1));
    this.getContentPane().add(jPanel1,   new XYConstraints(0, 0, 450, 227));
    this.setLocation(150,150);
    this.setSize(400,300);
    this.setForeground(UIManager.getColor("InternalFrame.activeTitleGradient"));
    this.setBackground(UIManager.getColor("InternalFrame.activeTitleGradient"));
    this.setTitle("用户信息管理界面");
  }

public String getUserId(){return jTextField1.getText();}
public String getUserName(){return jTextField2.getText();}
public String getUserPassword(){return jPasswordField1.getText();}
public String getUserType(){
    return (jComboBox1.getItemAt(jComboBox1.getSelectedIndex())).toString();
   }

  void jButton4_actionPerformed(ActionEvent e) {
this.setVisible(false);
  }

  void jButton1_actionPerformed(ActionEvent e) {
    int number;
    String id = this.getUserId();
    String name = this.getUserName();
    String password = this.getUserPassword();
    String type = this.getUserType();
    int type1=0;
    if (type=="系统管理员") type1=1;
      else {
        if(type=="高级用户") type1=2;
        else {
          if(type=="普通用户") type1=3;
          else type1=0;}
          }


    if (id != null) {
      try {
        number = Integer.parseInt(id);
      }
      catch (NumberFormatException nfe) {
        number = 0;
      }
    }

    else {
      JOptionPane.showMessageDialog(this, "you must enter person ID!!");
      number = 0;
    }

    if (number == 0||type1==0||name==null||password==null)
      JOptionPane.showMessageDialog(this, "you must enter complete infomation!!");
    else {
      try {
        PreparedStatement addInfo = database1.createPreparedStatement(
            "INSERT INTO user_info"
            + "(user_ID,user_name,user_password,user_type)"
            + "  VALUES(?,?,?,?)");
        addInfo.setInt(1, number);
        addInfo.setString(2, name);
        addInfo.setString(3, password);
        addInfo.setInt(4,type1);
        addInfo.execute();
        addInfo.close();

      }
      catch (SQLException err) {
        err.printStackTrace();
      }
      JOptionPane.showMessageDialog(null, "you have add user infomation");
    }
  }

  void jButton2_actionPerformed(ActionEvent e) {
    String person=jTextField1.getText();
    int no = 0;

    if (person != null) {
      try {
        no = Integer.parseInt(person);
      }
      catch (NumberFormatException nfe) {
        nfe.printStackTrace();
        no = 0;
      }
    }

    else {
      no = 0;
      JOptionPane.showMessageDialog(this, "please enter correct infomation");
    }

    if (no == 0)
      JOptionPane.showMessageDialog(this, "please enter right infomation");
    else {
      try {PreparedStatement deleteInfo=database1.createPreparedStatement("DELETE FROM user_info WHERE user_ID=?");
           deleteInfo.setInt(1,no);
           deleteInfo.execute();
           deleteInfo.close();
      }
      catch (SQLException sql){sql.printStackTrace();}
      JOptionPane.showMessageDialog(this,"you have delete "+no+" user!!");
    }

  }
//更改用户信息
  void jButton3_actionPerformed(ActionEvent e) {
    String id1="";
    String name1="";
    String password1="";
    int user1;
    String id = this.getUserId();
    String name = this.getUserName();
    String password = this.getUserPassword();
    String type = this.getUserType();
    int number=0;
if (id != null) {
  try {
    number = Integer.parseInt(id);
  }
  catch (NumberFormatException nfe) {
    number = 0;
  }
}
else {
  JOptionPane.showMessageDialog(this, "you must enter user ID!!");
  number = 0;
}

if (number == 0)
  JOptionPane.showMessageDialog(this, "you must enter user ID");
else {
    try{
      int number1=0;
      int user=0;
      if(type=="系统管理员")user=1;
        else if (type=="高级用户")user=2;
          else if(type=="普通用户")user=3;
            else user=0;
      PreparedStatement alterInfo = database1.createPreparedStatement(
          "SELECT * FROM user_info WHERE user_ID='"+number+"'");
      ResultSet rs=alterInfo.executeQuery();
      if(name!=null) name1=name;
      else name1=rs.getString("user_name");

      if(password!=null) password1=password;
        else password1=rs.getString("user_password");

      if(user!=0)user1=user;
        else user1=rs.getInt("user_type");

       rs.close();
       alterInfo.close();

       PreparedStatement alter=null;
       alter=database1.createPreparedStatement("UPDATE user_info SET user_name=?,user_password=?,user_type=? WHERE user_ID='"+number+"'");
       alter.setString(1,name1);
       alter.setString(2,password1);
       alter.setInt(3,user);
       alter.execute();
       alter.close();
     }
     catch(SQLException sql){sql.printStackTrace();}
      JOptionPane.showMessageDialog(null,"you have alter user infomation");
    }
  }

public static void main(String args[])
  {UserInfo user=new UserInfo();
  user.setVisible(true);}
  }



class UserInfo_jButton4_actionAdapter implements java.awt.event.ActionListener {
  UserInfo adaptee;

  UserInfo_jButton4_actionAdapter(UserInfo adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton4_actionPerformed(e);
  }
}

class UserInfo_jButton1_actionAdapter implements java.awt.event.ActionListener {
  UserInfo adaptee;

  UserInfo_jButton1_actionAdapter(UserInfo adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton1_actionPerformed(e);
  }
}

class UserInfo_jButton2_actionAdapter implements java.awt.event.ActionListener {
  UserInfo adaptee;

  UserInfo_jButton2_actionAdapter(UserInfo adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton2_actionPerformed(e);
  }
}

class UserInfo_jButton3_actionAdapter implements java.awt.event.ActionListener {
  UserInfo adaptee;

  UserInfo_jButton3_actionAdapter(UserInfo adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton3_actionPerformed(e);
  }
}

⌨️ 快捷键说明

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