//需求分析:写一个成绩管理系统。其中的一个功能是实现成绩录入。要求能根据课程名称在系统里找到学生和课程信息;在结果区域中添加成绩。 //规则:要求对输入成绩的操作要有有效性检验。成绩只接受大于0且小于100的数字;同一记录不能反复提交;当点击“提交”,告知用户操作是否成功,并给出相应信息;课程名下拉列表中的课程列表根据数据库中的课程表,自动生成。 " y8 a+ t/ I: {& i( d6 Y$ m( V5 h' ~( l
//代码如下:: r* q* k4 \' u1 {4 d6 m$ Q# }' b
import java.awt.*;. y7 Q; q7 C3 z& T
import java.awt.event.*;
0 t# X; i7 z7 |( ^8 ~/ mimport java.sql.*; import javax.swing.*;) O, z2 A! y2 ^0 x8 b' P/ C
import javax.swing.table.DefaultTableModel; import school.schoolPicture.JdbcUtil; public class AddScore extends JPanel implements ActionListener {9 Q8 I* f/ f. @5 D9 ]( m
String[] 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();2 b* L6 d; c7 B m m
Connection con = JDBCUtil.getConnection();
/ e$ ^+ E1 K2 v$ y// Connection con2 = JdbcUtil.getConnection();5 p! x4 z3 h4 b1 O) I! P+ M
PreparedStatement ps = null;( A! l: L+ R- X4 _* _- S4 z' Y% W
ResultSet rs = null;
$ {0 E/ T) }7 l; G/ T8 i6 Y' @ResultSetMetaData meta = null;
- b$ j/ W; { L* C, uString sql = null;5 l# O; t( a+ T, y' u, N' x; \. p
int cols = 0;
. ]+ Y/ _1 U: q8 X1 R2 |6 vint rowCount = 0; public AddScore() {
- t0 Z- q5 Q: U4 r! k& [0 OCreateAddScoreGUI();9 f. T5 R! U' \1 P6 u+ L
} public void actionPerformed(ActionEvent e) {3 k) b% q- \; G: s ?. ]
String str = e.getActionCommand();& B3 Q9 f- [6 j {+ m4 p
if ("查询".equals(str)) { //当点击查询时,用searchScore()方法对其进行处理
V) }; g1 ?' _. O2 j$ OsearchScore(); ^! H7 w; T9 K) W& A3 f' {+ Y) d
} else if ("提交".equals(str)) { //当点击提交时,用tryCommit()方法对其进行处理" r# e+ ~2 q2 d" D# | R, E4 [
tryCommit(); } else if ("退出".equals(str)) {7 o9 M/ X; _9 V) M) F7 T
this.setVisible(false);
: \) o; @$ m$ c, B, V}
: }- Z) B0 \: P5 p! ?}
c$ {! l' q: b! Kpublic void tryCommit() {2 ~0 k" y; u. V: t
if (cols < 1)/ p8 w. l6 g: c$ g1 E' j& `: t3 O }, ~
return;! Z7 T8 }9 y6 X% S$ F
sql = "insert into scorexx values(?,?,?,?)";
7 S% s" K8 {( K: Etry {
! \& r4 K& }- w K8 Rps = con.prepareStatement(sql);
}! P- U6 Z2 b1 r6 OString[] value = new String[4]; A4 ]. B) t$ G, U) t
for (int k = 0; k < rowCount; k++) {- Q3 C" E- W) C' F8 ~7 H
for (int i = 1; i <= cols + 1; i++) {
, C3 P& b7 y* N1 L/ O! f* ?2 C, }// 如果成绩为空,取消提交操作,并给出提示。
% y9 `; Z8 T- z! G+ H4 u: aif (jtAddScore.getValueAt(k, i - 1) != null) {
. D" L+ h" z9 I5 N* jvalue[i - 1] = jtAddScore.getValueAt(k, i - 1)
5 T: X! n1 l l: [ }- K5 Y.toString();
' B# }3 `, [% X! s6 ~} else {
- c6 |4 W4 `& c# A) vif (i == 4)
3 m/ @5 s* Q0 v, |; f/ MJOptionPane
, r( Q7 _: W( x.showMessageDialog(null, "无效成绩输入!成绩不能为空");
" y, n' r: R, H+ D+ u4 B+ Zreturn;
& W J( U8 X8 u0 S}, \. }& U6 c- ~$ e( ~
// 如果成绩字段里面不是数字,则取消本次操作??
! W/ K/ |) q+ J// String str = "^[0-9]{1}[0-9]?[0-9]?$";) Z+ a9 u- v; B, x8 g. Z
// if (i == 4) {
3 M( E3 f) t$ Y. i4 u& {// if(jtAddScore.getValueAt(k, i - 1) instanceof Integer){
; S: v& [5 ^7 |7 H3 K0 Z// if($ {# }! L' l0 X
// Integer.parseInt(value[3])<0||Integer.parseInt(value[3])>100)
$ R7 l4 s" g/ Q8 Z// {JOptionPane.showMessageDialog(null, "成绩只能大于0且小于100");9 ]" C: s% a& n; z
// return;}2 L% U0 a1 I& A1 W1 p
// }; J$ c6 n* r5 f3 U
// }
7 e1 K9 Y% v0 @' Q" }System.out.print(value[i - 1]);6 P9 o3 o0 ~ }) z
}
- p, _( x; n8 c; dps.setString(1, value[0]);) s2 W6 v: b; c% x0 U& b6 t# o: _
ps.setString(2, value[1]);
0 @' D, r1 `7 q1 `- S7 R0 t ips.setString(3, value[2]);9 @7 c# c: l F# N$ T. s: C) K
ps.setInt(4, Integer.parseInt(value[3]));
4 @0 K7 Y7 E) H0 G& ~) D+ Qps.executeUpdate();
* N6 _2 L7 f' c9 tcon.commit();0 A% r7 m9 v9 _! ]
System.out.println();% R( ^( F8 t3 B, \! P% R- O6 K
}: n+ p9 _& q) J0 T6 e
JOptionPane& b% m% q1 D* [0 o/ E; \; j! E9 g4 z
.showMessageDialog(null, "提交成功:更新 " + rowCount + " 条记录!");8 v$ @5 Y" v8 w9 u- f- p
} catch (Exception ew) {4 }- p: y4 Q/ m6 [( Q
ew.printStackTrace(); } } * [7 w# w" i* f4 [9 D) _
// 自动添加课程下拉列表中的课程
+ O6 f+ m6 w& {public void addCourseToJCheckBoxCourse() {
: {) `- X/ a- l$ U& QString courseName = null;
5 p9 f& i. Q6 H# Ssql = "select cname from coursexx";4 I1 v4 Z O$ h8 @
try {
1 I0 `6 {4 u2 B% d4 xps = con.prepareStatement(sql);
) Z" e( M; w- h1 v$ Q* o0 @/ ]rs = ps.executeQuery();
' R- y( f& D1 |+ TResultSetMetaData meta = rs.getMetaData();. m6 P6 P7 I3 `# ?( A- M
int cols = meta.getColumnCount();
3 [/ k( A( }) f, G1 F* @while (rs.next()) {% y$ j, V4 `1 v* V+ M! Z' F6 I
for (int i = 1; i <= cols; i++) {, n6 z- j" R: c$ M/ g& H+ e
courseName = rs.getString(i);) G5 l! b j- L* A
System.out.println(courseName);0 Q+ C* S1 Z# U
jcbCourse.addItem(courseName); C3 b9 G7 i1 _( x2 |5 ~
rowCount++;
. D0 J' b8 B- X1 c. C}3 b( R& P ~0 M
}
& T1 K: m7 Y+ O. ?8 M! g; Ucon.commit();
0 _) y3 G! d: Q6 L0 t* q) H3 u} catch (Exception ers) {- {# s& R! d w2 j5 a
ers.printStackTrace();
6 e2 F" U% `' G9 C: c! i% o$ i( ltry {2 V( H+ S& M! d/ L, q* P3 q
con.rollback();
+ g% N1 j: {+ P" y& I; S} catch (Exception er) {5 h* j3 b* ~5 c' r. \
er.printStackTrace();+ ~& S7 t% z) i, p
}; r) ]4 k. ], h& `4 G# r: Z
} } ' A- K1 j9 o* [. r) p
public void searchScore() {! U4 P6 x; s! R& l c* ]
// 每次查询之前清空之前的结果3 i& `9 Q n8 p2 a
for (int k = 0; k < 50; k++) {% W, I/ c h2 g! o
for (int i = 0; i < cols; i++) {
1 e) F2 S. l+ xjtAddScore.setValueAt(null, k, i);7 s% j1 m3 R& y- b/ N1 O% V: L" @. G) n
} }- Z, a# Z* M! x _
// Method1:JDBC Method2:hibernate
# C5 Q- u" g% U n. t5 A+ A" AString cName = jcbCourse.getSelectedItem().toString().trim();
3 s' V; ?% \8 r6 J+ H+ C w0 Rsql = "select x.sname,k.term,k.cno from "
: I k- z0 w8 }% v! K, F- A0 R& _+ "choicesxx x join Coursexx k "
! H4 z; Q) e- H3 r- ^, l+ |% l9 n+ "on x.cno=k.cno and k.cname=?";0 m" H, o4 ?2 H8 W9 d4 [
try {
Z: s5 Z, k0 B% T( M& Aps = con.prepareStatement(sql);
: ?" @" u6 [& E4 K- Yps.setString(1, cName);
& V0 H! H' o) l' A3 C1 krs = ps.executeQuery();
; l m2 W, k/ k. {3 I- imeta = rs.getMetaData();7 p$ x/ @% e$ t, Z2 _7 }3 O: o/ X8 m
cols = meta.getColumnCount();5 y' [1 P6 U( Q' ^& P+ o& P
boolean flag = true;6 {3 M8 P r. q7 u5 `! k
String result = null;
. B. ], C$ A+ O) Z9 sint k = 1;- n4 x! \# k9 v5 m, y
while (rs.next()) { L1 t8 m: t# k' N. E
for (int i = 1; i <= cols; i++) {! D( m$ @. s1 B" u d
result = rs.getString(i);
- p! m: |+ j4 |( ^% q7 e9 z4 DjtAddScore.setValueAt(result, k - 1, i - 1);
* K* |" H( S J C6 {1 Z7 x' ~}: c# J1 s# U+ {3 z
rowCount = k++;7 t; Q5 O, o9 h1 X- H+ @
} } catch (Exception exx) {% l! N9 w. `' }9 D' U! I
exx.printStackTrace();# O) s! @( M) O8 R1 U& F/ c5 C
}
9 o/ l5 z) z. U. {} $ S8 h" r B% o/ K
//产生用户界面 public void CreateAddScoreGUI() {$ }( m; I$ a9 M. W
this.setLayout(new FlowLayout()); JLabel jlCourse = new JLabel("课程:");
" a# U9 I* l1 w9 g; xjcbCourse.addActionListener(this);
& \8 g" V3 T/ U8 |( g; @/ [jcbCourse.addItem(new String("请选择课程"));( B, d8 u2 m# m+ G" r
addCourseToJCheckBoxCourse();
5 D" a1 k7 r% h" q' F3 uthis.add(jlCourse);2 P; m0 P& V$ m6 q' M) y- S; G
this.add(jcbCourse); // JLabel jlClassName = new JLabel("班级:");
6 w& _0 y0 [, s$ K# @// jcbClassName.addActionListener(this);) m9 e3 ]" ~7 r. Q4 @
// jcbClassName.addItem(new String("请选择班级"));
9 ^5 h( J8 y0 l7 g' ~// addClassNameToJCheckBoxClassName();
% Z2 y6 x R e0 }* N# v9 r t// this.add(jlClassName);
|) B: H( ^- R3 V9 C// this.add(jcbClassName); // JLabel jlTerm = new JLabel("学期:");
# B, r% l$ U. [// jcbTerm.addActionListener(this);
* @+ X* k: o6 e$ D0 ?" B7 j: ]// jcbTerm.addItem(new String("请选择学期"));
6 H/ T3 o# h+ \// addTermToJCheckBoxTerm();( y& Z' t( {" m2 ?: j1 J- \
// this.add(jlTerm);) I, h' l4 p% d9 b+ t
// this.add(jcbTerm); JButton jbSearch = new JButton("查询");1 H- X' R( G' T9 E5 n' y
jbSearch.addActionListener(this);$ Q- f! Z$ c D& o" ~7 Y! A
JButton jbOK = new JButton("提交");
1 q& B" J7 I3 K5 R4 Q* s; i0 ajbOK.addActionListener(this);' m+ y, w* M; R2 m! n
JButton jbExit = new JButton("退出");2 E& S. w2 J: N3 Y5 ]
jbExit.addActionListener(this);
5 Z4 ]/ n4 x x, N! N5 }/ pthis.add(jbSearch);
v- D7 h. C. a3 V; E$ \1 B! C8 fthis.add(jbOK);; S8 } T$ x/ \! A2 }: j) Z
this.add(jbExit); this.add(jspScore); this.setVisible(true);
, l% r% }) G. W; x! f, I* A- r$ | othis.setSize(500, 600); } public static void main(String[] args) {
3 b! k; ]. Y) Inew AddScore();' t" X4 T$ {. [) t Q) X3 T% ^7 B
}
7 J* U/ j, ]/ l0 `( r4 }" a6 j. A}
. W. h% I9 n3 U4 Z4 `8 L4 G |