📄 main.java
字号:
con.close();
}
} catch (Exception ex) {
}
}
}
}
//import java.sql.*;
class StudentInfo {
String ID, Name, Sex, Classname, Homeaddr, Mobile, Diploma;
String Birthdate, Intime, Outtime;
public StudentInfo() {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException e) {
System.out.println("连接数据库错误" + e);
}
}
// 根据学号查询信息
public void SearchByID(String ID) {
String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=student";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String IDsearchStr = new String(
"select * from StudentInfo where Personalid='" + ID + "'");
try {
con = DriverManager.getConnection(url);
stmt = con.createStatement();
// 查询所输入学号对应的信息
rs = stmt.executeQuery(IDsearchStr);
if (rs.next()) {
this.ID = ID;
Name = rs.getString(2);
Sex = rs.getString(3);
Birthdate = rs.getString(4);
Classname = rs.getString(5);
Homeaddr = rs.getString(6);
Mobile = rs.getString(7);
Intime = rs.getString(8);
Outtime = rs.getString(9);
Diploma = rs.getString(10);
} else {
this.ID = "无此学号信息";
}
} catch (SQLException e) {
this.ID = "查询出错";
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
}
}
if (con != null) {
try {
con.close();
} catch (SQLException ex) {
}
}
}
}
// 查询特定ID是否存在
public boolean isExist(String ID) {
boolean isExist = false;
String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=student";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String isExistStr = new String(
"select * from StudentInfo where Personalid='" + ID + "'");
try {
con = DriverManager.getConnection(url);
stmt = con.createStatement();
// 查询所输入ID是否存在
rs = stmt.executeQuery(isExistStr);
if (rs.next()) {
isExist = true;
}
} catch (SQLException e) {
System.out.println("查询ID错误" + e);
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
}
}
if (con != null) {
try {
con.close();
} catch (SQLException ex) {
}
}
}
return isExist;
}
// 更新信息
public boolean UpdateInfo(String ID, String Name, String Sex,
String Birthdate, String Classname, String Homeaddr, String Mobile,
String Intime, String Outtime, String Diploma) {
boolean update = false;
String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=student";
Connection con = null;
Statement stmt = null;
String UpdateStr = new String("update StudentInfo set Name='" + Name
+ "',Sex='" + Sex + "',Birthdate='" + Birthdate
+ "',Classname='" + Classname + "',Homeaddr='" + Homeaddr
+ "',Mobile='" + Mobile + "',Intime='" + Intime + "',Outtime='"
+ Outtime + "',Diploma='" + Diploma + "' where Personalid='"
+ ID + "'");
try {
con = DriverManager.getConnection(url);
stmt = con.createStatement();
// 更新信息
stmt.executeUpdate(UpdateStr);
update = true;
} catch (SQLException e) {
System.out.println("更新信息" + e);
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
}
}
if (con != null) {
try {
con.close();
} catch (SQLException ex) {
}
}
}
return update;
}
// 创建信息
public boolean CreateInfo(String ID, String Name, String Sex,
String Birthdate, String Classname, String Homeaddr, String Mobile,
String Intime, String Outtime, String Diploma) {
boolean create = false;
String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=student";
Connection con = null;
Statement stmt = null;
String CreateStr = new String("insert into StudentInfo values('" + ID
+ "','" + Name + "','" + Sex + "','" + Birthdate + "','"
+ Classname + "','" + Homeaddr + "','" + Mobile + "','"
+ Intime + "','" + Outtime + "','" + Diploma + "')");
try {
con = DriverManager.getConnection(url);
stmt = con.createStatement();
// 创建信息
stmt.executeUpdate(CreateStr);
create = true;
} catch (SQLException e) {
System.out.println("创建信息" + e);
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
}
}
if (con != null) {
try {
con.close();
} catch (SQLException ex) {
}
}
}
return create;
}
// 删除信息
public boolean DeleteInfo(String ID) {
boolean delete = false;
String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=student";
Connection con = null;
Statement stmt = null;
String DeleteStr = new String(
"delete from StudentInfo where Personalid='" + ID + "'");
try {
con = DriverManager.getConnection(url);
stmt = con.createStatement();
// 删除信息
stmt.executeUpdate(DeleteStr);
delete = true;
} catch (SQLException e) {
System.out.println("删除信息" + e);
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
}
}
if (con != null) {
try {
con.close();
} catch (SQLException ex) {
}
}
}
return delete;
}
}
//import javax.swing.*;
//import java.awt.*;
//import java.awt.event.*;
class UserManager extends JFrame implements ActionListener {
private JLabel label_u;
private JLabel label_p;
private JButton button_add;
private JButton button_del;
private JPasswordField passwordField;
private JTextField textField;
private Container con;
public UserManager() {
super("用户管理");
setSize(214, 143);
con = getContentPane();
con.setLayout(null);
label_u = new JLabel();
label_u.setText("用户名");
label_u.setBounds(10, 10, 60, 20);
con.add(label_u);
label_p = new JLabel();
label_p.setText("密码");
label_p.setBounds(10, 46, 60, 20);
con.add(label_p);
textField = new JTextField();
textField.setBounds(76, 10, 120, 20);
con.add(textField);
passwordField = new JPasswordField();
passwordField.setBounds(76, 46, 120, 20);
passwordField.setEchoChar('*');
con.add(passwordField);
button_add = new JButton();
button_add.setText("添加");
button_add.setBounds(29, 77, 60, 23);
con.add(button_add);
button_del = new JButton();
button_del.setText("删除");
button_del.setBounds(119, 77, 60, 23);
con.add(button_del);
passwordField.addActionListener(this);
button_add.addActionListener(this);
button_del.addActionListener(this);
// 将窗口置于屏幕中央
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button_add) {
CheckUser add_user = new CheckUser();
// 取得用户输入的用户名
String tempUser = textField.getText();
// 取得用户输入的密码
char temp[] = passwordField.getPassword();
String tempPass = new String(temp);
if (add_user.isExist(tempUser)) {
JOptionPane.showMessageDialog(null, "用户已存在", "警告",
JOptionPane.WARNING_MESSAGE);
} else {
if (add_user.adduser(tempUser, tempPass)) {
JOptionPane.showMessageDialog(null, "创建用户成功", "信息",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "创建用户失败", "警告",
JOptionPane.WARNING_MESSAGE);
}
setVisible(false);
}
} else if (e.getSource() == button_del) {
CheckUser del_user = new CheckUser();
// 取得用户输入的用户名
String tempUser = textField.getText();
if (del_user.isExist(tempUser)) {
if (del_user.deluser(tempUser)) {
JOptionPane.showMessageDialog(null, "删除用户成功", "信息",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "删除用户失败", "警告",
JOptionPane.WARNING_MESSAGE);
}
textField.setText("");
passwordField.setText("");
setVisible(false);
} else {
JOptionPane.showMessageDialog(null, "用户不存在", "警告",
JOptionPane.WARNING_MESSAGE);
}
}
}
}
//import java.awt.Container;
//import java.awt.Dimension;
//import java.awt.Toolkit;
//import java.awt.event.ActionEvent;
//import java.awt.event.ActionListener;
//import javax.swing.*;
class ChangePassword extends JFrame implements ActionListener {
JPasswordField newpassword;
JLabel label;
JButton button;
Container container;
String username;
public ChangePassword(String username) {
super("修改密码");
this.username = username;
container = getContentPane();
container.setLayout(null);
setSize(250, 115);
newpassword = new JPasswordField();
newpassword.setEchoChar('*');
button = new JButton();
button.setText("确定");
label = new JLabel();
label.setText("输入新密码:");
label.setBounds(10, 10, 100, 25);
newpassword.setBounds(120, 10, 120, 25);
button.setBounds(95, 55, 60, 25);
container.add(newpassword);
container.add(label);
container.add(button);
button.addActionListener(this);
// 将窗口置于屏幕中央
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
// 取得用户输入的密码
char temp[] = newpassword.getPassword();
String tempPass = new String(temp);
CheckUser pw = new CheckUser();
if (pw.changepw(username, tempPass)) {
JOptionPane.showMessageDialog(null, "密码修改成功", "信息",
JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "密码修改失败", "警告",
JOptionPane.WARNING_MESSAGE);
}
setVisible(false);
}
}
}
//import java.awt.*;
//import java.awt.event.*;
//import javax.swing.*;
class About extends JFrame {
JLabel label;
JButton button;
// 生成一个窗口
public About() {
super("关于");
setSize(250, 115);
setAlwaysOnTop(true);
label = new JLabel();
label.setText("制作者:赵坤");
button = new JButton();
button.setText("确定");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
Container con = getContentPane();
con.setLayout(null);
label.setBounds(85, 10, 82, 25);
button.setBounds(95, 55, 60, 25);
con.add(label);
con.add(button);
// 将窗口置于屏幕中央
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
setResizable(false);
setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -