//需求分析:写一个成绩管理系统。其中的一个功能是实现成绩录入。要求能根据课程名称在系统里找到学生和课程信息;在结果区域中添加成绩。 //规则:要求对输入成绩的操作要有有效性检验。成绩只接受大于0且小于100的数字;同一记录不能反复提交;当点击“提交”,告知用户操作是否成功,并给出相应信息;课程名下拉列表中的课程列表根据数据库中的课程表,自动生成。 7 U9 u# X v6 c- e9 N5 S' ?$ X
//代码如下:& K* M2 [ j7 `" f' Y
import java.awt.*;
) R1 f0 b! o# \- Uimport java.awt.event.*;
) |( ?2 Y N t" U) }+ Rimport java.sql.*; import javax.swing.*;2 K* O6 i, h3 U1 q" E
import javax.swing.table.DefaultTableModel; import school.schoolPicture.JdbcUtil; public class AddScore extends JPanel implements ActionListener {
$ R: d2 S( s9 ~& Q, q2 mString[] 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 L" ]9 a0 d$ \1 @0 \, [! {" c
Connection con = JDBCUtil.getConnection();$ X; N J! d5 {+ N
// Connection con2 = JdbcUtil.getConnection();+ ^9 ?1 r6 G5 b+ B; l
PreparedStatement ps = null;
9 Q6 S$ L3 ?/ x+ P2 ]( NResultSet rs = null;
& }" N3 Q: v b1 j1 C" O- q& [ResultSetMetaData meta = null;; _$ d3 y' R) U2 I- q. Z
String sql = null;
0 t+ o9 K9 [2 w8 yint cols = 0;
2 s. \4 A, c# {9 gint rowCount = 0; public AddScore() {
( ^9 D9 |8 h8 @9 @: x$ B0 u+ qCreateAddScoreGUI();
; H3 m: z6 |9 \1 d: i( v" G5 c} public void actionPerformed(ActionEvent e) {
- `; m$ k3 Y; h) u" GString str = e.getActionCommand();
; x0 Q1 j2 N k$ o9 g8 ^- Gif ("查询".equals(str)) { //当点击查询时,用searchScore()方法对其进行处理
1 E# Q' o4 L, ~4 Z7 _7 asearchScore();
( ?+ ~7 p" n7 R0 ?2 ]} else if ("提交".equals(str)) { //当点击提交时,用tryCommit()方法对其进行处理8 ]& |. p& X( R6 Z9 O& t2 o* H
tryCommit(); } else if ("退出".equals(str)) {
- V4 V. d* {7 ~2 g1 ithis.setVisible(false);
; M( F6 U4 P- [5 d/ u8 n}. V, t7 ?+ d4 U# N
}; E: ] M/ q; P
public void tryCommit() {
8 |) T% }! h+ C4 jif (cols < 1)* p+ j( }3 Q4 X
return;
4 e2 m$ J3 X# ?9 g) p J4 q2 Dsql = "insert into scorexx values(?,?,?,?)";
" p9 G' @1 y2 H! R4 `) u) jtry {
; Z) }& W4 N4 k! ^ps = con.prepareStatement(sql);- x! P5 y+ j6 G- x- J0 z& |
String[] value = new String[4];
8 o& I7 p# Y: }( L' y) u) d8 Vfor (int k = 0; k < rowCount; k++) {
; x& n' h3 }! c1 U- P4 }for (int i = 1; i <= cols + 1; i++) {8 y' Q& {0 S2 N$ ~" b3 X, K
// 如果成绩为空,取消提交操作,并给出提示。
4 A$ k/ @/ K$ F; hif (jtAddScore.getValueAt(k, i - 1) != null) {
- t! a! R: u; S" Lvalue[i - 1] = jtAddScore.getValueAt(k, i - 1): m v8 J( }% D4 U! I7 w! Q/ Y$ q5 |
.toString();
* g5 Z* N8 s' i- g2 V9 N( p/ x} else {; G+ k. }+ \) l' ^4 G4 F# A0 ?
if (i == 4)
( ]* l- g" S' XJOptionPane# `; r0 G6 l" ?6 a. M
.showMessageDialog(null, "无效成绩输入!成绩不能为空");' i; a- M! p, d9 j' U. f
return;1 m1 \2 g9 E0 [0 F" D
}
' I9 s3 z. y9 a. T% @1 j. Q+ v# g. n% E// 如果成绩字段里面不是数字,则取消本次操作??) R! `9 c8 A K5 A, m+ ^ X3 R, v
// String str = "^[0-9]{1}[0-9]?[0-9]?$";
' m" G) n- A2 P9 P( C& @// if (i == 4) {
6 T3 C5 ~# D' D4 ?$ } ?// if(jtAddScore.getValueAt(k, i - 1) instanceof Integer){) b" o7 M7 }0 r; m6 B; M& U. [
// if(! n8 A2 D3 E: v' Z( S' N$ l
// Integer.parseInt(value[3])<0||Integer.parseInt(value[3])>100)# a" T2 F$ u: [4 R9 T* J+ L: x3 a! q( F
// {JOptionPane.showMessageDialog(null, "成绩只能大于0且小于100");
8 o. a9 K$ I- I% z8 S// return;}
3 h- v6 F$ k: u# Q# s// }# r# a3 ^7 l' q) L2 E- A& X
// }
3 Y1 p' e/ K! \, @ LSystem.out.print(value[i - 1]);0 ~! Q; W0 {; m+ p
}
5 Y8 L1 }- w/ Rps.setString(1, value[0]);
5 t! `! L `8 c! g# o3 c7 tps.setString(2, value[1]);
& L- m3 N X9 v, k( S( L% e/ cps.setString(3, value[2]);# R3 h+ R6 p$ e& [
ps.setInt(4, Integer.parseInt(value[3]));4 ~9 J3 I+ k0 R* g4 D* c
ps.executeUpdate();* C; X1 S' ]+ f: T5 u% g U8 m
con.commit();
7 f/ e' A/ i$ F+ z; j3 kSystem.out.println();
! C3 r6 u9 U+ ]+ j}
( Y4 _6 d0 {$ FJOptionPane' o7 `1 O0 n3 [- v
.showMessageDialog(null, "提交成功:更新 " + rowCount + " 条记录!");3 Y! \9 K; z* \$ X# {
} catch (Exception ew) {
5 T/ ^/ J! D3 ]) |% t( ^ a: Kew.printStackTrace(); } } , L% B4 C7 i* M$ c7 t1 {; B+ I) Z0 L* a
// 自动添加课程下拉列表中的课程# r$ @4 P) U5 J# g f* P
public void addCourseToJCheckBoxCourse() {1 e+ |' w. v% M9 A* Z
String courseName = null;
s- N. @7 l' G* z. Qsql = "select cname from coursexx";7 Q' Y9 m5 g$ n. A( v
try {
# \! N0 H& A7 i5 g3 Yps = con.prepareStatement(sql);. @, o# d' {$ F8 e" A
rs = ps.executeQuery();
: }/ B7 y; K* S4 T! YResultSetMetaData meta = rs.getMetaData();- k: q. F% b4 z7 x1 r
int cols = meta.getColumnCount();8 n+ \2 z1 H: ^% j0 v8 ~2 K
while (rs.next()) {3 u3 }1 l* G4 F2 ~. a. j- s: h
for (int i = 1; i <= cols; i++) {8 w1 A, F% Q r" V6 l
courseName = rs.getString(i);+ ?) R5 g3 f& t: b
System.out.println(courseName);
; @6 }" F$ B j9 q# e8 I2 ^) r9 OjcbCourse.addItem(courseName);
- L. w: j9 G% u7 O# ArowCount++;4 s. }% L- `* P4 j1 e" S5 k* ]
}
9 G [. k7 _' O* _( N8 c}' \/ M, o+ `0 N- @6 [% u3 p1 H
con.commit();( t6 ]2 Y- o0 V3 a$ X; q+ l
} catch (Exception ers) {3 ], u; Y, @1 K
ers.printStackTrace();2 w8 Y( l1 T5 Y4 W$ \% M
try {% m* c. t: U" G( b4 c: ~
con.rollback();+ H' h |8 C3 ?* Z' ^- g! ~
} catch (Exception er) {; x3 m( A5 M8 M
er.printStackTrace();
6 K* p8 e# t# X- z. n3 b}
+ N; v5 v+ h# U% ?" h! ^} }
' r/ f, N1 ^% W: Y0 |) y0 j0 f6 gpublic void searchScore() {
2 P# g: z. z& [9 u4 @// 每次查询之前清空之前的结果* B3 B8 {% V7 i
for (int k = 0; k < 50; k++) {5 ^; F$ X- h" c$ x2 _
for (int i = 0; i < cols; i++) {
8 P! G1 X* V K XjtAddScore.setValueAt(null, k, i);
4 S$ p6 [% D4 d+ u" g C9 T} }
; S" s4 w+ N3 b# M. \// Method1:JDBC Method2:hibernate8 ~/ v& i; ]# D& x
String cName = jcbCourse.getSelectedItem().toString().trim();
9 u: A7 G" r/ M# G+ Q2 wsql = "select x.sname,k.term,k.cno from "
: H* Z* @- j9 a3 `2 A! P+ "choicesxx x join Coursexx k "& Q X& \% R! [ T6 ^
+ "on x.cno=k.cno and k.cname=?";
% s' [- S# Q v* V" R J5 Xtry {2 P1 F- {4 J, O, u) O- g
ps = con.prepareStatement(sql);! o' m+ B) @6 o1 Q$ K& r
ps.setString(1, cName);$ q7 ]: C8 `% K" a d, ^
rs = ps.executeQuery(); D* ?7 c! A. Y& A$ G( r
meta = rs.getMetaData();
3 c2 M. p+ R2 k# v. P7 C: z+ Gcols = meta.getColumnCount();
- u# D' x* t4 W- H* B: P5 tboolean flag = true;- f; ]2 g2 u2 ~6 Y# i& i7 O
String result = null;# D7 t# V3 O( D" d% [+ H
int k = 1;( i" d$ K& ~6 ~$ M3 N/ K
while (rs.next()) {7 Y+ h( B; M$ q; a& C: Y
for (int i = 1; i <= cols; i++) {2 a* _2 T$ o2 }7 c0 r/ b
result = rs.getString(i);
# d) G9 W1 [* Z/ cjtAddScore.setValueAt(result, k - 1, i - 1);7 F* o3 K# a" R1 q' {
}5 k# I9 r1 R1 g2 \
rowCount = k++;
* U* m |- `+ I; [3 ~) _} } catch (Exception exx) {
' q$ l7 x0 l9 t7 y. R# n/ yexx.printStackTrace();- i- l3 j! Z% k- e% R! N
}/ e7 `" V: E. J' P1 }6 F7 C7 L5 j
} 5 d+ i* U4 W& V2 A- A) e
//产生用户界面 public void CreateAddScoreGUI() {
0 I4 k* S$ E, Y" X2 F C( E; D& M" lthis.setLayout(new FlowLayout()); JLabel jlCourse = new JLabel("课程:");
; B/ [5 j, u' W% djcbCourse.addActionListener(this);
, z3 o3 n) D0 [jcbCourse.addItem(new String("请选择课程"));: W% L7 W! J% n8 v( n
addCourseToJCheckBoxCourse();7 y; M; _- ?( ~4 |
this.add(jlCourse);
1 w3 k+ U" s5 p) J5 jthis.add(jcbCourse); // JLabel jlClassName = new JLabel("班级:");5 G$ ]/ R, l4 ^2 m5 {. D
// jcbClassName.addActionListener(this);
6 f. Z7 G2 Q" e c// jcbClassName.addItem(new String("请选择班级"));
V6 C5 A. }2 ]0 n5 v6 R% p// addClassNameToJCheckBoxClassName();
* v2 d9 I' ?+ Z$ ~; U. Z) |2 B// this.add(jlClassName);
2 n5 A9 {+ ^& t0 V% ]/ h% X// this.add(jcbClassName); // JLabel jlTerm = new JLabel("学期:");
X$ q! S* z+ r* n; N4 q// jcbTerm.addActionListener(this);5 E& `9 i- _+ w) U- x
// jcbTerm.addItem(new String("请选择学期"));4 J$ j2 N9 s7 ~9 T3 r/ G
// addTermToJCheckBoxTerm();3 g: [! u- b4 F! N) ^) h/ J
// this.add(jlTerm);) H5 G7 T- e7 A! R9 [) I
// this.add(jcbTerm); JButton jbSearch = new JButton("查询");' b& @8 N- s$ t9 I
jbSearch.addActionListener(this);
6 \2 u0 W- k3 |JButton jbOK = new JButton("提交");
. f! ^, u/ C. E4 @jbOK.addActionListener(this);* z$ L, k' c0 I! }, Y
JButton jbExit = new JButton("退出");1 R0 d# ^7 o( F3 X2 {( E
jbExit.addActionListener(this);
( U; x. Z1 Y) t5 gthis.add(jbSearch);
# O: @ Q: l" g3 athis.add(jbOK);" o. {; [# f; z( X' d" F8 }% r1 D
this.add(jbExit); this.add(jspScore); this.setVisible(true);
( S* }6 w8 |: J; k7 T2 n8 n7 X/ uthis.setSize(500, 600); } public static void main(String[] args) {/ r4 H4 Q. K ?! m: `
new AddScore();- Q$ M: }, X3 u
}
' N. C0 i/ r' @$ q, A" D5 q}
% ?$ X% r* B8 s, U% K8 n |