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

📄 main.java

📁 JAVA做的学生管理信息系统,共同交流
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.table.*; 
import java.util.*; 
import java.sql.*;
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.Toolkit; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
 

public class Main { 

public Main() { 
// 声明一个登录窗口 
new LoginSystem(); 
} 

public static void main(String[] args) { 
new Main(); 
} 

} 
 
//import java.awt.*; 
//import javax.swing.*; 
//import java.awt.event.*; 
 class LoginSystem extends JFrame implements ActionListener { 
private JLabel label_u; 

private JLabel label_p; 

private JButton button_ok; 

private JButton button_cancel; 

private JPasswordField passwordField; 

private JTextField textField; 

private Container con; 

// 产生登录界面 
public LoginSystem() { 
super("登录界面"); 
setSize(214, 143); 
// setAlwaysOnTop(true); 
con = getContentPane(); 
con.setBackground(new Color(129, 249, 252)); 
con.setLayout(null); 
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

label_u = new JLabel(); 
label_u.setFont(new Font("", Font.PLAIN, 14)); 
label_u.setText("用户名"); 
label_u.setBounds(10, 10, 60, 20); 
con.add(label_u); 

label_p = new JLabel(); 
label_p.setFont(new Font("", Font.PLAIN, 14)); 
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_ok = new JButton(); 
button_ok.setText("确定"); 
button_ok.setBounds(29, 77, 60, 23); 
con.add(button_ok); 

button_cancel = new JButton(); 
button_cancel.setText("取消"); 
button_cancel.setBounds(119, 77, 60, 23); 
con.add(button_cancel); 

passwordField.addActionListener(this); 
button_ok.addActionListener(this); 

button_cancel.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_ok || e.getSource() == passwordField) { 
// 取得用户输入的用户名 
String tempUser = textField.getText(); 
// 取得用户输入的密码 
char temp[] = passwordField.getPassword(); 
String tempPass = new String(temp); 
CheckUser checkUser = new CheckUser(); 
if (checkUser.isValidUser(tempUser, tempPass) == false) { 
passwordField.setText(""); 
// 弹出警告对话框 
JOptionPane.showMessageDialog(null, "用户名或密码不正确", "错误", 
JOptionPane.ERROR_MESSAGE); 
} else { 
 
 
 // 初始化主界面 
setVisible(false); 
int flag = checkUser.getUserType(tempUser); 
new MainFrame(tempUser, flag); 
} 
} else if (e.getSource() == button_cancel) { 
System.exit(0); 
} 
} 
} 


//import javax.swing.*; 

//import java.awt.*; 
//import java.awt.event.*; 

 class MainFrame extends JFrame implements ActionListener { 
JLabel label[]; 

JLabel label_admin, label_user; 

JTextField textfield[]; 

Container con; 

JButton b_all, b_one, b_pw, b_save, b_exit, b_about, b_del, b_user; 

String username; 

// 产生主界面 
public MainFrame(String username, int flag) { 
super("欢迎使用学生信息查询系统--" + username); 
this.username = username; 
con = getContentPane(); 
con.setLayout(null); 
setSize(520, 350); 
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

label = new JLabel[10]; 
textfield = new JTextField[10]; 
for (int i = 0; i < 10; i++) { 
label[i] = new JLabel(); 
textfield[i] = new JTextField(); 
} 
label[0].setText("学号"); 
label[1].setText("姓名"); 
label[2].setText("性别"); 
label[3].setText("出生日期"); 
label[4].setText("所属班级"); 
label[5].setText("家庭住址"); 
label[6].setText("手机"); 
label[7].setText("入学时间"); 
label[8].setText("毕业时间"); 
label[9].setText("学位"); 

for (int i = 0; i < 10; i++) { 
con.add(label[i]); 
con.add(textfield[i]); 
} 

for (int i = 0; i < 10; i++) { 
label[i].setBounds(10, i * 30 + 10, 70, 20); 
label[i].setFont(new Font("", Font.PLAIN, 14)); 
textfield[i].setBounds(80, i * 30 + 10, 150, 20); 
textfield[i].setFont(new Font("", Font.PLAIN, 14)); 
} 

b_all = new JButton(); 
b_all.setText("查询所有"); 
b_one = new JButton(); 
b_one.setText("单个查询"); 
b_exit = new JButton(); 
b_exit.setText("退出"); 
b_pw = new JButton(); 
b_pw.setText("修改密码"); 
b_about = new JButton(); 
b_about.setText("关于"); 

con.add(b_about); 
con.add(b_all); 
con.add(b_one); 
con.add(b_exit); 
con.add(b_pw); 
b_all.setBounds(260, 70, 100, 20); 
b_one.setBounds(390, 70, 100, 20); 
b_exit.setBounds(400, 280, 80, 20); 
b_pw.setBounds(260, 100, 100, 20); 
b_about.setBounds(300, 280, 80, 20); 

b_all.addActionListener(this); 
b_one.addActionListener(this); 
b_exit.addActionListener(this); 
b_pw.addActionListener(this); 
b_about.addActionListener(this); 

// 根据用户类型确定文本是否可以编辑 
if (flag == 1) { 
for (int i = 1; i < 10; i++) { 
textfield[i].setEditable(false); 
} 
} 

label_user = new JLabel("单个查询时,填写学号,点击单个查询即可"); 
con.add(label_user); 
label_user.setFont(new Font("", Font.PLAIN, 14)); 
label_user.setBounds(240, 10, 310, 20); 

if (flag == 0) { 
label_admin = new JLabel("修改记录时,修改左侧内容,点击保存即可"); 
con.add(label_admin); 
label_admin.setFont(new Font("", Font.PLAIN, 14)); 
label_admin.setBounds(240, 40, 310, 20); 
b_save = new JButton(); 
b_save.setText("保存修改"); 
b_del = new JButton();   
 b_del.setText("删除信息"); 
b_user = new JButton(); 
b_user.setText("用户管理"); 
con.add(b_user); 
con.add(b_del); 
con.add(b_save); 
b_del.setBounds(260, 130, 100, 20); 
b_save.setBounds(390, 100, 100, 20); 
b_user.setBounds(390, 130, 100, 20); 
b_save.addActionListener(this); 
b_del.addActionListener(this); 
b_user.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() == b_all) { 
new ShowAllInfo(); 
} else if (e.getSource() == b_one) { 
String ID = textfield[0].getText(); 
for (int i = 0; i < 10; i++) { 
textfield[i].setText(""); 
} 
// System.out.println(ID); 
String noID = (new JTextField()).getText(); 
if (ID.equals(noID)) { 
textfield[0].setText("请输入学号"); 
} else { 
StudentInfo SInfo = new StudentInfo(); 
SInfo.SearchByID(ID); 
textfield[0].setText(SInfo.ID); 
textfield[1].setText(SInfo.Name); 
textfield[2].setText(SInfo.Sex); 
textfield[3].setText(SInfo.Birthdate.toString()); 
textfield[4].setText(SInfo.Classname); 
textfield[5].setText(SInfo.Homeaddr); 
textfield[6].setText(SInfo.Mobile); 
textfield[7].setText(SInfo.Intime.toString()); 
textfield[8].setText(SInfo.Outtime.toString()); 
textfield[9].setText(SInfo.Diploma); 
} 
} else if (e.getSource() == b_save) { 
String ID, Name, Sex, Classname, Homeaddr, Mobile, Diploma; 
String Birthdate, Intime, Outtime; 
ID = textfield[0].getText(); 
Name = textfield[1].getText(); 
Sex = textfield[2].getText(); 
Birthdate = textfield[3].getText(); 
Classname = textfield[4].getText(); 
Homeaddr = textfield[5].getText(); 
Mobile = textfield[6].getText(); 
Intime = textfield[7].getText(); 
Outtime = textfield[8].getText(); 
Diploma = textfield[9].getText(); 
StudentInfo SInfo = new StudentInfo(); 
if (SInfo.isExist(ID)) { 
// 更新信息 
// JOptionPane.showMessageDialog(null, "此学号已存在", "通知", 
// JOptionPane.INFORMATION_MESSAGE); 
if (SInfo.UpdateInfo(ID, Name, Sex, Birthdate, Classname, 
Homeaddr, Mobile, Intime, Outtime, Diploma)) { 
JOptionPane.showMessageDialog(null, "保存修改成功", "信息", 
JOptionPane.INFORMATION_MESSAGE); 
} else { 
JOptionPane.showMessageDialog(null, "保存修改失败", "警告", 
JOptionPane.WARNING_MESSAGE); 
} 
} else { 
// 创建新信息 
// JOptionPane.showMessageDialog(null, "此学号不存在", "通知", 
// JOptionPane.INFORMATION_MESSAGE); 
if (SInfo.CreateInfo(ID, Name, Sex, Birthdate, Classname, 
 Homeaddr, Mobile, Intime, Outtime, Diploma)) { 
JOptionPane.showMessageDialog(null, "创建信息成功", "信息", 
JOptionPane.INFORMATION_MESSAGE); 
} else { 
JOptionPane.showMessageDialog(null, "创建信息失败", "警告", 
JOptionPane.WARNING_MESSAGE); 
} 
} 
} else if (e.getSource() == b_pw) { 
new ChangePassword(username); 
} else if (e.getSource() == b_about) { 
new About(); 
} else if (e.getSource() == b_exit) { 
System.exit(0); 
} else if (e.getSource() == b_del) { 
String ID; 
ID = textfield[0].getText(); 
StudentInfo SInfo = new StudentInfo(); 
if (SInfo.isExist(ID)) { 
if (SInfo.DeleteInfo(ID)) { 
JOptionPane.showMessageDialog(null, "删除成功", "通知", 
JOptionPane.INFORMATION_MESSAGE); 
} else { 
JOptionPane.showMessageDialog(null, "删除失败", "警告", 
JOptionPane.WARNING_MESSAGE); 
} 
} else { 
JOptionPane.showMessageDialog(null, "此学号不存在", "通知", 
JOptionPane.INFORMATION_MESSAGE); 
} 
for (int i = 0; i < 10; i++) { 
textfield[i].setText(""); 
} 
} else if (e.getSource() == b_user) { 
new UserManager(); 
} 
} 
} 

//import java.sql.*; 

 class CheckUser { 

// 连接数据库 
public CheckUser() { 
try { 
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
} catch (ClassNotFoundException e) { 
System.out.println("连接数据库错误" + e); 
} 
} 

// 判断用户名和密码是否合法 
public boolean isValidUser(String username, String password) { 
boolean isValid = false; 
String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=student"; 
Connection con = null; 
Statement stmt = null; 
ResultSet rs = null; 
String isValidStr = new String("select * from users where Username='" 
+ username + "' and " + "Password='" + password + "'"); 
try { 
con = DriverManager.getConnection(url); 
stmt = con.createStatement(); 
// 查询所输入的用户名和密码是否存在 
rs = stmt.executeQuery(isValidStr); 
if (rs.next()) { 
isValid = true; 
} 
} catch (SQLException e) { 
System.out.println("查询数据库错误" + 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 isValid; 
} 

// 获取用户类型 
public int getUserType(String username) { 
int flag = 1; 
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student"; 
String user = "sa"; 
String pw = ""; 
Connection con = null; 
Statement stmt = null; 
ResultSet rs = null; 
String userTypeStr = new String("select * from users where Username='" 
+ username + "'"); 
try { 
con = DriverManager.getConnection(url, user, pw); 
stmt = con.createStatement(); 
// 查询所输入的用户名和密码是否存在 
 
 
 rs = stmt.executeQuery(userTypeStr); 
if (rs.next()) { 
flag = rs.getInt("flag"); 
} 
} catch (SQLException e) { 
System.out.println("查询数据库错误" + 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 flag; 
} 

// 修改密码 
public boolean changepw(String username, String password) { 
boolean changed = false; 
String url = "jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=student"; 
Connection con = null; 
Statement stmt = null; 
String changepwStr = new String("update users set Password='" 
+ password + "' where Username='" + username + "'"); 
try { 
con = DriverManager.getConnection(url); 
stmt = con.createStatement(); 
// 修改密码 
stmt.executeUpdate(changepwStr); 
changed = true; 

} catch (SQLException e) { 
System.out.println("修改密码" + e); 
} finally { 
if (stmt != null) { 
try { 
stmt.close(); 
} catch (SQLException ex) { 
} 

⌨️ 快捷键说明

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