📄 userinfo.java~1~
字号:
package hospitalinfomationsystem;
//引入包
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("userID");
jTextField1.setSelectionStart(11);
jTextField1.setText("");
jLabel2.setText("password");
jPasswordField1.setText("");
jLabel3.setText("user type");
jLabel4.setText("user name");
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:patient", "", "", false, "sun.jdbc.odbc.JdbcOdbcDriver"));
jPanel1.add(jTextField1, new XYConstraints(93, 34, 81, 24));
jPanel1.add(jLabel2, new XYConstraints(195, 35, 67, 22));
jPanel1.add(jComboBox1, new XYConstraints(276, 87, 81, 24));
jPanel1.add(jLabel3, new XYConstraints(195, 90, 67, 22));
jPanel1.add(jLabel4, new XYConstraints(17, 96, 69, -1));
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));
this.getContentPane().add(jPanel1, new XYConstraints(0, 0, 450, -1));
this.setLocation(150, 150);
this.setSize(400, 300);
this.setForeground(Color.pink);
this.setBackground(Color.pink);
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();
if (id != null) {
try {
number = Integer.parseInt(id);
}
catch (NumberFormatException nfe) {
number = 0;
}
}
else {
JOptionPane.showMessageDialog(this, "you must enter patient ID!!");
number = 0;
}
if (number == 0 || type == null || 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.setString(4, type);
addInfo.execute();
addInfo.close();
}
catch (SQLException err) {
err.printStackTrace();
}
JOptionPane.showMessageDialog(null, "you have add user infomation");
}
}
//删除按钮的程序
void jButton2_actionPerformed(ActionEvent e) {
String patient = jTextField1.getText();
int no = 0;
if (patient != null) {
try {
no = Integer.parseInt(patient);
}
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 = "";
String type1 = "";
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;
String user = "";
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")
type1 = type;
else
type1 = rs.getString("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.setString(3, user);
alter.execute();
alter.close();
}
catch (SQLException sql) {
sql.printStackTrace();
}
JOptionPane.showMessageDialog(null, "you have alter user infomation");
}
}
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 + -