?? e0e445ae1f53001c10cffe9dfad691e6
字號:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.sql.*;
public class GuanLi extends JFrame implements ActionListener {
String xm;
int i, j, xh, yy, gs, wl, zt;
JLabel prompt1 = new JLabel("學生成績管理系統");
JLabel prompt2 = new JLabel(" 用戶:");
JLabel prompt3 = new JLabel(" 密碼:");
JLabel prompt4 = new JLabel(" 姓名:");
JLabel prompt5 = new JLabel(" 學號:");
JLabel prompt6 = new JLabel(" 高等數學:");
JLabel prompt7 = new JLabel(" 大學英語:");
JLabel prompt8 = new JLabel(" 大學物理:");
JLabel prompt9 = new JLabel(" 機械制圖:");
JTextField input1 = new JTextField(8);
JTextField input2 = new JTextField(8);
JTextField input3 = new JTextField(8);
JTextField input4 = new JTextField(8);
JTextField input5 = new JTextField(8);
JTextField input6 = new JTextField(8);
JTextField input7 = new JTextField(8);
JTextField input8 = new JTextField(8);
JButton btn1 = new JButton("登錄");
JButton btn2 = new JButton("增加記錄");
JButton btn3 = new JButton("修改記錄");
JButton btn4 = new JButton("刪除記錄");
JButton btn5 = new JButton("學生查詢");
public GuanLi() {
getContentPane().setLayout(new GridLayout(10, 2));
getContentPane().add(new JLabel());
getContentPane().add(prompt1);
getContentPane().add(new JLabel());
getContentPane().add(prompt2);
getContentPane().add(input1);
getContentPane().add(new JLabel());
getContentPane().add(prompt3);
getContentPane().add(input2);
getContentPane().add(btn1);
getContentPane().add(prompt4);
getContentPane().add(input3);
getContentPane().add(new JLabel());
getContentPane().add(prompt5);
getContentPane().add(input4);
getContentPane().add(new JLabel());
getContentPane().add(prompt6);
getContentPane().add(input5);
getContentPane().add(new JLabel());
getContentPane().add(prompt7);
getContentPane().add(input6);
getContentPane().add(new JLabel());
getContentPane().add(prompt8);
getContentPane().add(input7);
getContentPane().add(new JLabel());
getContentPane().add(prompt9);
getContentPane().add(input8);
getContentPane().add(btn5);
getContentPane().add(btn2);
getContentPane().add(btn3);
getContentPane().add(btn4);
prompt6.setVisible(false);
prompt7.setVisible(false);
prompt8.setVisible(false);
prompt9.setVisible(false);
input5.setVisible(false);
input6.setVisible(false);
input7.setVisible(false);
input8.setVisible(false);
btn2.setVisible(false);
btn3.setVisible(false);
btn4.setVisible(false);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "登錄") {
String a, b;
a = input1.getText();
b = input2.getText();
input1.setText("");
if ((a.equals("zhaoweihua") == true)
&& (b.equals("4649521") == true)) {
try {
Connection con = DBConnection.getConncetion();
con.close();
} catch (SQLException ex) {
System.out.println(ex.toString());
System.out.println("Error!!");
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
prompt4.setVisible(true);
prompt5.setVisible(true);
prompt6.setVisible(true);
prompt7.setVisible(true);
btn5.setLabel("用戶查詢");
prompt8.setVisible(true);
prompt9.setVisible(true);
input3.setVisible(true);
input4.setVisible(true);
input5.setVisible(true);
input6.setVisible(true);
input7.setVisible(true);
input8.setVisible(true);
btn2.setVisible(true);
btn3.setVisible(true);
btn4.setVisible(true);
btn1.setVisible(false);
input1.setText("登錄成功");
input2.setText("");
input1.selectAll();
} else
input2.setText("用戶名或密碼錯");
}
if (e.getActionCommand() == "增加記錄") {
boolean scucss = true;
try {
xm = input3.getText();
xh = Integer.parseInt(input4.getText());
yy = Integer.parseInt(input6.getText());
gs = Integer.parseInt(input5.getText());
wl = Integer.parseInt(input7.getText());
zt = Integer.parseInt(input8.getText());
Connection con = DBConnection.getConncetion();
Statement stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO chengjiguanli VALUES('" + xm+ "'," + xh + "," + gs + "," + yy + "," + wl + "," + zt+ ")");
con.close();
stmt.close();
} catch (SQLException ex) {
System.out.println(ex.toString());
System.out.println("Error!!");
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
try {
XingMing();
} catch (EmptyException as) {
input3.setText("姓名不能為空");
scucss = false;
}
try {
xh = Integer.parseInt(input4.getText());
} catch (NumberFormatException m) {
input4.setText("學號為空或格式錯");
scucss = false;
}
try {
YY();
} catch (EmptyException as) {
yy = -1;
} catch (OverException dd) {
input6.setText("應在0-100間");
scucss = false;
} catch (NumberFormatException cm) {
input6.setText("成績應為數據");
scucss = false;
}
try {
GS();
} catch (EmptyException as) {
gs = -1;
} catch (OverException dd) {
input5.setText("應在0-100間");
scucss = false;
} catch (NumberFormatException cm) {
input5.setText("成績應為數據");
scucss = false;
}
try {
WL();
} catch (EmptyException as) {
wl = -1;
} catch (OverException dd) {
input7.setText("應在0-100間");
scucss = false;
} catch (NumberFormatException cm) {
input7.setText("成績應為數據");
scucss = false;
}
try {
ZT();
} catch (EmptyException as) {
zt = -1;
} catch (OverException dd) {
input8.setText("應在0-100間");
scucss = false;
} catch (NumberFormatException cm) {
input8.setText("成績應為數據");
scucss = false;
}
if (scucss == true) {
}
}
if (e.getActionCommand() == "修改記錄")
{
try {
xm = input3.getText();
xh = Integer.parseInt(input4.getText());
yy = Integer.parseInt(input6.getText());
gs = Integer.parseInt(input5.getText());
wl = Integer.parseInt(input7.getText());
zt = Integer.parseInt(input8.getText());
Connection con = DBConnection.getConncetion();
Statement stmt = con.createStatement();
stmt.executeUpdate("UPDATE chengjiguanli SET Math=" + gs+ ",English=" + yy + ",Physical=" + wl + ",Draw=" + zt+ " WHERE Sname='" + xm + "' AND Sno=" + xh + "");
con.close();
stmt.close();
}
catch (SQLException ex) {
System.out.println(ex.toString());
System.out.println("Error!!");
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
}
if (e.getActionCommand() == "刪除記錄") {
try {
xm = input3.getText();
xh = Integer.parseInt(input4.getText());
Connection con = DBConnection.getConncetion();
Statement stmt = con.createStatement();
stmt.executeUpdate("DELETE FROM chengjiguanli WHERE Sname='"+ liming+ "' AND Sno=" + 1 + "");
con.close();
stmt.close();
input1.setText("刪除成功");
input2.setText("");
input3.setText("");
input4.setText("");
input5.setText("");
input6.setText("");
input7.setText("");
input8.setText("");
} catch (SQLException ex) {
System.out.println(ex.toString());
System.out.println("Error!!");
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
// StuInf.removeElementAt(mid);
}
if (e.getActionCommand() == "用戶查詢") {
boolean right = true;
try {
xh = Integer.parseInt(input4.getText());
} catch (NumberFormatException m) {
input4.setText("學號為空或格式錯");
right = false;
}
if (right == true) {
try {
xh = Integer.parseInt(input4.getText());
Connection con = DBConnection.getConncetion();
Statement stmt = con.createStatement();
ResultSet rs = stmt
.executeQuery("SELECT * FROM chengjiguanli WHERE Sno="+ xh + "");
rs.next();
String i = rs.getString(1);
String j = rs.getString(3);
String k = rs.getString(4);
String m = rs.getString(5);
String n = rs.getString(6);
input3.setText(i);
input5.setText(j);
input6.setText(k);
input7.setText(m);
input8.setText(n);
con.close();
stmt.close();
rs.close();
} catch (SQLException ex) {
System.out.println(ex.toString());
System.out.println("Error!!");
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
btn3.setEnabled(true);
btn4.setEnabled(true);
}
}
if (e.getActionCommand() == "學生查詢") {
boolean right = true;
prompt2.setVisible(false);
prompt3.setVisible(false);
prompt6.setVisible(true);
prompt7.setVisible(true);
prompt8.setVisible(true);
prompt9.setVisible(true);
btn1.setVisible(false);
btn2.setVisible(false);
btn3.setVisible(false);
btn4.setVisible(false);
input1.setVisible(false);
input2.setVisible(false);
input5.setVisible(true);
input6.setVisible(true);
input7.setVisible(true);
input8.setVisible(true);
try {
xh = Integer.parseInt(input4.getText());
} catch (NumberFormatException m) {
input4.setText("學號為空或格式錯");
right = false;
}
if (right == true) {
try {
xh = Integer.parseInt(input4.getText());
Connection con = DBConnection.getConncetion();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM chengjiguanli WHERE Sno="+ xh + "");
rs.next();
String i = rs.getString(1);
String j = rs.getString(3);
String k = rs.getString(4);
String m = rs.getString(5);
String n = rs.getString(6);
input3.setText(i);
input5.setText(j);
input6.setText(k);
input7.setText(m);
input8.setText(n);
con.close();
stmt.close();
rs.close();
} catch (SQLException ex) {
System.out.println(ex.toString());
System.out.println("Error!!");
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
btn3.setEnabled(true);
btn4.setEnabled(true);
}
}
}
// 異常類
class OverException extends Exception {
String over;
}
class EmptyException extends Exception {
String empty;
}
void XingMing() throws EmptyException {
if ((input3.getText()).equals(""))
throw (new EmptyException());
else
xm = input3.getText();
}
void YY() throws OverException, EmptyException {
if ((input6.getText()).equals(""))
throw (new EmptyException());
else
yy = Integer.parseInt(input6.getText());
if (yy < 0 || yy > 100)
throw (new OverException());
}
void WL() throws OverException, EmptyException {
if ((input7.getText()).equals(""))
throw (new EmptyException());
else
wl = Integer.parseInt(input7.getText());
if (wl < 0 || wl > 100)
throw (new OverException());
}
void ZT() throws OverException, EmptyException {
if ((input8.getText()).equals(""))
throw (new EmptyException());
else
zt = Integer.parseInt(input8.getText());
if (zt < 0 || zt > 100)
throw (new OverException());
}
void GS() throws OverException, EmptyException {
if ((input5.getText()).equals(""))
throw (new EmptyException());
else
gs = Integer.parseInt(input5.getText());
if (gs < 0 || gs > 100)
throw (new OverException());
}
public static void main(String[] args) {
GuanLi frame = new GuanLi();
frame.setTitle("歡迎登錄學生成績管理系統");
frame.setSize(800, 600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -