//需求分析:写一个成绩管理系统。其中的一个功能是实现成绩录入。要求能根据课程名称在系统里找到学生和课程信息;在结果区域中添加成绩。 //规则:要求对输入成绩的操作要有有效性检验。成绩只接受大于0且小于100的数字;同一记录不能反复提交;当点击“提交”,告知用户操作是否成功,并给出相应信息;课程名下拉列表中的课程列表根据数据库中的课程表,自动生成。 8 Q5 m9 q! W0 |" b
//代码如下:
$ J( {' S- L- H- R+ W$ cimport java.awt.*;
" H7 j& n$ H% J+ \; R+ E9 Qimport java.awt.event.*;. Q( b4 d$ R, `; e5 s, b+ K
import java.sql.*; import javax.swing.*;( L* X; I' Z& y I& S4 o
import javax.swing.table.DefaultTableModel; import school.schoolPicture.JdbcUtil; public class AddScore extends JPanel implements ActionListener {
5 _+ I, p( o7 n7 OString[] columnNames = new String[] { "姓名", "学期", "课程ID", "成绩" }; DefaultTableModel dtmScore = new DefaultTableModel(columnNames, 57); JTable jtAddScore = new JTable(dtmScore); JScrollPane jspScore = new JScrollPane(jtAddScore); JComboBox jcbCourse = new JComboBox(); JComboBox jcbClassName = new JComboBox(); JComboBox jcbTerm = new JComboBox();4 F6 ^6 i! l7 P; C4 ]/ l( J3 V, c+ w
Connection con = JDBCUtil.getConnection();# P7 y+ V# j. V: O9 B+ D
// Connection con2 = JdbcUtil.getConnection();
& @4 F6 t/ N, L! [PreparedStatement ps = null;
, p5 ^$ r* \, v0 O! }; `ResultSet rs = null;/ l* Z$ s+ D, j/ B8 t
ResultSetMetaData meta = null;
2 ~, `8 X# y, S6 CString sql = null;4 N- M( Y+ _0 a
int cols = 0; |5 e; z9 X# m. B/ u8 G1 ~
int rowCount = 0; public AddScore() {# L+ }: ~# k9 i1 _
CreateAddScoreGUI();
& }& G8 r( Z$ V/ J" }3 K} public void actionPerformed(ActionEvent e) {, D% Q$ p2 P4 |# |
String str = e.getActionCommand();
6 T0 V9 V9 J$ \if ("查询".equals(str)) { //当点击查询时,用searchScore()方法对其进行处理! Q$ [/ k+ t8 M0 U& h; s2 D
searchScore();) D9 d1 r' u# ?
} else if ("提交".equals(str)) { //当点击提交时,用tryCommit()方法对其进行处理( `* V0 I/ o; F. E2 ?
tryCommit(); } else if ("退出".equals(str)) {
* L6 a6 X+ Q0 C5 G/ ^" bthis.setVisible(false);5 @0 ~( b! a1 p; m2 g
}6 D% n8 R0 D: x" ~ R$ }; z/ f E
}' r4 x8 S M6 t3 B
public void tryCommit() {- z2 S/ ?9 E# A( _7 |% n; e3 O E( b
if (cols < 1)& E5 |" W8 m: E
return;
/ M3 D) N! L0 L% {+ q$ osql = "insert into scorexx values(?,?,?,?)";
4 c. n1 ~1 v N* H4 ]try {
+ u* v0 e* {) W! ^7 @; kps = con.prepareStatement(sql);
7 U% K3 v. w. B. M' iString[] value = new String[4];0 h/ P% n6 d) M- n
for (int k = 0; k < rowCount; k++) {
: _# e3 _2 @; K" T ]; afor (int i = 1; i <= cols + 1; i++) {
& N/ N- J7 ?, [9 W: z+ n' X! U// 如果成绩为空,取消提交操作,并给出提示。6 l8 |8 y% \# q6 O
if (jtAddScore.getValueAt(k, i - 1) != null) {
: j" k5 r8 B5 Y+ evalue[i - 1] = jtAddScore.getValueAt(k, i - 1)
6 l5 r0 x0 H. }* O0 W.toString();' V, k& R/ U7 a) l6 e( o6 e
} else {
; R- T/ j3 U8 p5 S% eif (i == 4)
. ?, C$ d1 d/ I: T: H1 r: X" J0 bJOptionPane- I! K0 U( R# I
.showMessageDialog(null, "无效成绩输入!成绩不能为空");+ [+ x" S1 _4 o. I- q: Z
return;
; l, q* p9 ~/ z) H" r y* |5 z0 V}
* m }( }1 E$ G4 z8 Q3 I5 S+ |$ J// 如果成绩字段里面不是数字,则取消本次操作??. t1 P+ j5 F# I- {
// String str = "^[0-9]{1}[0-9]?[0-9]?$";1 k/ N0 I1 L o( u6 ]" R
// if (i == 4) {
u' i( k# C- S1 c% G// if(jtAddScore.getValueAt(k, i - 1) instanceof Integer){
1 i! C$ j/ A. {+ O3 _! R0 S& J// if(1 F( ?7 p0 |7 w; w# Q( ]7 v
// Integer.parseInt(value[3])<0||Integer.parseInt(value[3])>100)3 h& c9 n9 u) V& y: x; M! ~
// {JOptionPane.showMessageDialog(null, "成绩只能大于0且小于100");
1 u, l' e! x9 V" |2 m// return;}: ?6 }- J3 Y* z: e
// }& O5 J G) P+ J* E2 v( \
// }
Z! C# @7 O! z, c0 DSystem.out.print(value[i - 1]);
! M: m4 D. w1 ~5 p}
5 d4 o. B \2 J4 d1 x+ O% _ps.setString(1, value[0]);! O, B6 W H- G, E. a7 V
ps.setString(2, value[1]);) F) J" \* B& w; c+ C6 g* ~5 {
ps.setString(3, value[2]);( L. i3 o6 V) v2 }2 k* d1 D& b
ps.setInt(4, Integer.parseInt(value[3]));' h& Q* U" \% b9 o
ps.executeUpdate();4 k! X8 l# `4 x7 V% v
con.commit();
2 `0 i2 I* I( DSystem.out.println();/ d4 O( n& i; n7 B
}' ~8 K6 F$ [( t+ N
JOptionPane
* p# J c9 _# j" u4 p( Q1 G.showMessageDialog(null, "提交成功:更新 " + rowCount + " 条记录!");
8 G5 b1 Q1 d& ?8 Y# H( h} catch (Exception ew) {
; L* N% o1 R/ k3 dew.printStackTrace(); } } $ D/ S3 k L% p e% S
// 自动添加课程下拉列表中的课程% ^; v+ ?# F6 z
public void addCourseToJCheckBoxCourse() {) C3 b, Y' s% y2 v
String courseName = null;" L* `+ j: K: E: i
sql = "select cname from coursexx";
/ _5 x) J6 O' ~, x1 Wtry {
* W5 k2 G. i* v7 ^ps = con.prepareStatement(sql);
9 o- _ m% w" L# Hrs = ps.executeQuery();
, Z; q# {, b) f1 C( o3 {/ |ResultSetMetaData meta = rs.getMetaData();& E* T+ {" e: H% i
int cols = meta.getColumnCount();
% d& p; w1 I& `# j; v' |$ uwhile (rs.next()) {# _ F B" ~# t8 P( u* y D. a$ W
for (int i = 1; i <= cols; i++) {. s% j2 [3 b6 M3 B5 i
courseName = rs.getString(i);
9 i( D; v0 X9 @ T* G4 D% s: P9 xSystem.out.println(courseName);
|# j% y, d0 m/ vjcbCourse.addItem(courseName);. T, s7 }5 ~* T$ q' V
rowCount++;/ ~4 ^1 D# k& {% y* W( I0 L9 j
}- h7 U5 |2 r# t8 B! _
}5 ?8 f0 y9 H* q& L+ u( B) l
con.commit();$ R- N+ ]7 P, K
} catch (Exception ers) { D9 T! \4 c( [: }) W( ]5 r
ers.printStackTrace();9 M9 ^$ F$ Z. T9 P
try {
5 f4 y: p* m/ p( X w, X1 ?: X. Rcon.rollback();
# g7 G2 V- d* Y( X7 ^8 K9 v} catch (Exception er) { o! u9 ]8 f& E1 Z+ s7 Q6 b Z8 m1 F8 j
er.printStackTrace();% A0 P( W' U; F u
}% @& A% |7 x6 I& v8 x# T- a; t: l+ q
} } + L. c4 x3 B' J
public void searchScore() {
3 g$ W. m4 h+ N1 Y! @// 每次查询之前清空之前的结果8 B- U. @" Q* P5 S
for (int k = 0; k < 50; k++) {1 x/ z a+ n0 n! U8 N: J) N% p# N
for (int i = 0; i < cols; i++) {6 e% Y2 t' ` `, C
jtAddScore.setValueAt(null, k, i);3 H* {/ J1 I" y% c" a2 T/ w
} }" D( P/ _4 _& v4 ?2 }# h% f, @
// Method1:JDBC Method2:hibernate
1 ^2 J* S: C9 m: DString cName = jcbCourse.getSelectedItem().toString().trim();
1 a: p; O* f6 K& [3 b9 n8 b. ]sql = "select x.sname,k.term,k.cno from "
$ I, n: g) W- Z: {6 J1 |+ "choicesxx x join Coursexx k "
% ?! z5 f, z) I2 o( H+ "on x.cno=k.cno and k.cname=?";
# Y6 E3 q8 x, V( Ktry {# K# W; X0 z) E5 U
ps = con.prepareStatement(sql);
! _0 o2 k y9 |$ Xps.setString(1, cName);
* U) l2 R V: v2 ?$ M* C8 |rs = ps.executeQuery();- T" Y# P5 [% E
meta = rs.getMetaData();; U2 ~# ?- i. `9 ~" r
cols = meta.getColumnCount();
$ \3 h* ^1 A8 u1 q/ yboolean flag = true;* R- V5 k# t- z
String result = null;
1 N* `# x1 z1 D- f, Mint k = 1;
6 P9 l* o' B8 [while (rs.next()) {
/ z0 v8 F6 W. n- J$ c# X. Kfor (int i = 1; i <= cols; i++) {
5 N; [. B" P' ?# _* U( Fresult = rs.getString(i);6 G6 U. _- j t; K
jtAddScore.setValueAt(result, k - 1, i - 1);7 V5 j+ n& H; C& ]& t) A
}8 {3 s2 h; b7 h- c7 \6 o
rowCount = k++;" e! H6 m* x* D# f; s
} } catch (Exception exx) {6 C+ A7 I x" Y4 x, k' z
exx.printStackTrace();6 K. o# B3 F1 U$ i S% s/ O
}
; U' w9 N1 o9 ] p& P}
% y& I# V5 G0 }9 n- }//产生用户界面 public void CreateAddScoreGUI() {
9 P2 w3 }4 O8 h+ j8 s# athis.setLayout(new FlowLayout()); JLabel jlCourse = new JLabel("课程:");* @/ V8 e% M& M' C5 J1 H
jcbCourse.addActionListener(this);
4 H, H+ ^ t) T$ ?. A) WjcbCourse.addItem(new String("请选择课程"));
2 A- P4 h6 t, eaddCourseToJCheckBoxCourse();
3 U* _. J% U& K( D4 rthis.add(jlCourse);5 K1 m$ w) R( P6 |
this.add(jcbCourse); // JLabel jlClassName = new JLabel("班级:");: R0 }$ \8 `3 Z' N+ K) c: J
// jcbClassName.addActionListener(this);; k3 ^' m2 Y" ?4 v; |- `
// jcbClassName.addItem(new String("请选择班级"));
2 x. u( K' x7 K0 q- j4 }# r" }; p// addClassNameToJCheckBoxClassName();. N+ W8 L1 d+ J* r- R! n
// this.add(jlClassName);
$ G1 N% r; f, M) E: m// this.add(jcbClassName); // JLabel jlTerm = new JLabel("学期:");
* W1 f1 G3 E1 v& |+ l' |// jcbTerm.addActionListener(this);6 J9 y: u! ]1 L4 J+ Z) }% {
// jcbTerm.addItem(new String("请选择学期"));* \6 j }4 h$ x9 J, i$ ~& m$ k* [
// addTermToJCheckBoxTerm();
/ E( q" H. b( b! ]2 @& G& q// this.add(jlTerm);
5 Z0 ?; V7 K, U! h: O* s2 l: r& P// this.add(jcbTerm); JButton jbSearch = new JButton("查询");- b6 @" ?9 C- b) o- c
jbSearch.addActionListener(this);: Q _, u! W1 [) l( i C
JButton jbOK = new JButton("提交");# ^0 `" Y$ ^+ m% w8 w! k- [
jbOK.addActionListener(this);
8 D5 n9 V$ J3 F9 }" C# KJButton jbExit = new JButton("退出");
! e l% L) N) `- P- l h' CjbExit.addActionListener(this);& t: v8 N" g- m/ V
this.add(jbSearch);/ ^( g' [7 n' p6 V* K# e
this.add(jbOK);
+ b, Q# I1 ^. ?# `' Y* b( A* rthis.add(jbExit); this.add(jspScore); this.setVisible(true);
$ F2 r3 ]* z# w( n0 p+ Xthis.setSize(500, 600); } public static void main(String[] args) {
( i& Y3 z5 z# ynew AddScore();
) A2 y2 c! v+ `( H8 n# b, R}$ s) S- u. E9 A: O! c4 V
} 7 `4 m9 h/ `; O5 m5 v/ \& ~8 a
|