//需求分析:写一个成绩管理系统。其中的一个功能是实现成绩录入。要求能根据课程名称在系统里找到学生和课程信息;在结果区域中添加成绩。 //规则:要求对输入成绩的操作要有有效性检验。成绩只接受大于0且小于100的数字;同一记录不能反复提交;当点击“提交”,告知用户操作是否成功,并给出相应信息;课程名下拉列表中的课程列表根据数据库中的课程表,自动生成。
6 m/ a* b6 A" @' \ y$ W//代码如下:8 g9 T; L+ g8 P9 ]
import java.awt.*;
( I0 `+ c( w3 V; Aimport java.awt.event.*;+ ?* y& K$ I4 m* [7 v( T% i
import java.sql.*;
import javax.swing.*;7 ~# }$ l7 i5 D! ^
import javax.swing.table.DefaultTableModel; import school.schoolPicture.JdbcUtil; public class AddScore extends JPanel implements ActionListener {: R) k) {, C+ n# Y: U# g& l
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();* u: k# G+ W! s& S. s
Connection con = JDBCUtil.getConnection();/ s# n( u& `' ^9 G
// Connection con2 = JdbcUtil.getConnection();# p0 Y! c. u J: `1 E+ w% k% I
PreparedStatement ps = null;% X; j1 B0 W+ B0 J$ t7 D
ResultSet rs = null;% o* X9 A% F' p& E! @5 r: k
ResultSetMetaData meta = null;9 t# j7 R+ c7 \; W. i
String sql = null;- o6 @" J$ i+ H l! Y
int cols = 0;
3 T8 y" Z& c5 k+ ~- k2 m/ }int rowCount = 0; public AddScore() {) u2 q% V* f& h4 R
CreateAddScoreGUI();
1 k7 ^' Y% R8 i: U} public void actionPerformed(ActionEvent e) {
2 _" ?( R/ l) f* N) sString str = e.getActionCommand();) h* _, D* a. t) O' w* ?* C
if ("查询".equals(str)) { //当点击查询时,用searchScore()方法对其进行处理
. b- j9 H& z5 y$ \1 }2 M: n& tsearchScore();5 `/ z: K$ _; e& C- n
} else if ("提交".equals(str)) { //当点击提交时,用tryCommit()方法对其进行处理
3 C2 K" Y6 w- o! Z) V& MtryCommit(); } else if ("退出".equals(str)) {
0 F2 ~2 N3 G6 X" ^' L6 {! ^3 }this.setVisible(false);3 t" G, \2 C9 P+ A2 C
}
`' C+ h/ n7 b6 [}
4 a3 p: u) I( _" W0 Spublic void tryCommit() {$ p5 s# t; l0 l7 j0 a L7 [6 }& X
if (cols < 1)
8 p" K3 r1 S: Z+ U7 Z) R, dreturn;
/ u/ ?8 O( ]. k7 r8 b" Vsql = "insert into scorexx values(?,?,?,?)";) m8 y2 [7 |3 \, i& g U0 v! R
try {
[* a$ G: {& X% B" g* eps = con.prepareStatement(sql);
0 C& p6 j" \- HString[] value = new String[4];
9 l% f2 S( K# h+ ~7 Z( y2 Zfor (int k = 0; k < rowCount; k++) {
5 k, N! g/ e# W4 {; N! Yfor (int i = 1; i <= cols + 1; i++) {
/ T; Q- p: y3 L( _// 如果成绩为空,取消提交操作,并给出提示。/ A4 g+ n2 o0 D! N3 L
if (jtAddScore.getValueAt(k, i - 1) != null) {
+ s3 ], @- H5 k6 J# {7 }value[i - 1] = jtAddScore.getValueAt(k, i - 1)# q+ S) O Y( O/ G4 \, ]( }2 j3 D
.toString();
6 l* v0 p9 ]7 w, a} else {7 }; U! `+ b4 _! F: ^% Z
if (i == 4)! O! r* w- E+ d# u D4 V
JOptionPane7 K+ @! f7 | P7 ?% O
.showMessageDialog(null, "无效成绩输入!成绩不能为空");
|+ O! o' V2 t: yreturn;
; }3 ]+ b3 n2 ^; A/ R* M; t}
4 w9 f8 s' f4 Z/ A9 g// 如果成绩字段里面不是数字,则取消本次操作??: u) v8 g$ {/ K" ?. l% N
// String str = "^[0-9]{1}[0-9]?[0-9]?$";* y. @) T8 R. y( i7 h, S! C
// if (i == 4) {9 x9 l0 k( }% [5 ] Y
// if(jtAddScore.getValueAt(k, i - 1) instanceof Integer){
8 ~8 d r6 [3 c: ], P$ l* {// if(, Q! m8 L0 f( t9 {# W4 a: J% ?
// Integer.parseInt(value[3])<0||Integer.parseInt(value[3])>100)( o4 A2 v2 b/ ?3 R
// {JOptionPane.showMessageDialog(null, "成绩只能大于0且小于100");) q1 [! }# _; P- ]- M+ Q6 W b! b
// return;}
}0 Q6 H! I9 [" _% V" F. \, X" E// }; ^- m$ f) x' e! f' y$ h k$ ~
// }+ w0 d- n3 S! }
System.out.print(value[i - 1]);( O# U2 T& P3 L4 v" W
}
: ]1 X; P/ }) m( k/ Aps.setString(1, value[0]);
% e$ \* L" g9 c, S l Aps.setString(2, value[1]);5 G, H; J6 w9 T d! |9 u
ps.setString(3, value[2]);
$ h$ T; A. M3 ` e0 Hps.setInt(4, Integer.parseInt(value[3]));
/ y2 m7 q3 a4 `' R# c) bps.executeUpdate();
' X2 Y6 M- g, P6 O7 h Q k1 Qcon.commit();/ g5 @6 G/ L+ c- p/ g% C. i
System.out.println();" |% H; [7 p% m+ x: [. ^' M6 m
}
7 s2 H$ E2 O) v, O6 U3 _JOptionPane9 Z+ y" {$ `6 k3 Q5 P$ s
.showMessageDialog(null, "提交成功:更新 " + rowCount + " 条记录!");
# U: G* Z4 A% e! W. `# p} catch (Exception ew) {% I4 m; S' i$ k$ W
ew.printStackTrace(); } } 2 l7 M5 R- D* F% ^
// 自动添加课程下拉列表中的课程' N3 m# [; z/ \) e. |/ h# f8 x
public void addCourseToJCheckBoxCourse() {
& f) l& R' d1 S( a% ~String courseName = null;
# O0 P1 K% Q& V qsql = "select cname from coursexx";
. t% f0 B% O% l6 J" [try {
2 H: U; ]& j7 o4 C3 o1 C7 v0 L; ups = con.prepareStatement(sql);
, [1 [ Q1 c& C4 t6 Z- ^6 trs = ps.executeQuery();2 z2 R+ o& ^) Q+ j; }0 x) y
ResultSetMetaData meta = rs.getMetaData();
1 ]- d% O) j' o. g- Q. w6 L2 tint cols = meta.getColumnCount();
. ]9 b. n; `# c3 Z" Kwhile (rs.next()) {9 G+ S5 x# y/ t3 S
for (int i = 1; i <= cols; i++) {
/ q3 o* R) ]6 s% C- q9 V$ ucourseName = rs.getString(i);
2 o) @$ h* T3 K. P3 n6 c# uSystem.out.println(courseName);
& X1 F( O% o- n9 K* J: ?- IjcbCourse.addItem(courseName);" T3 R+ D5 Z' F1 i4 k% U t/ j
rowCount++;
! H% a+ r" b" K/ r9 z; }}7 \5 _ P. n9 q( P) v! v+ R
}
( Y: W9 A+ X1 t% @* lcon.commit();
2 X2 v q$ f% w, z1 m; Q1 R} catch (Exception ers) {
* W& r! S# [0 S3 a0 l+ p5 G. @5 rers.printStackTrace();5 o# j; I3 k- ? u, m
try {
8 J4 g u, ~' p$ s- m0 B! |8 econ.rollback();' f- |* `+ w2 f1 y
} catch (Exception er) {
+ D& ~" m7 z. z% k* m( |. Z! @5 ker.printStackTrace();
0 W$ O+ l- v. E0 S) P" h! u}9 R6 P3 r! a0 V" {, ~% ]; ^( R
} }
0 u% q5 l" p4 r1 `0 @public void searchScore() {
+ P W: z0 i6 J& A6 w+ E// 每次查询之前清空之前的结果
9 g# ~. e) ~1 ~4 ^7 X1 N1 Gfor (int k = 0; k < 50; k++) {6 U" T1 e7 u& l: ]3 z
for (int i = 0; i < cols; i++) {
% Z) G% f. [" e( S; i- ~jtAddScore.setValueAt(null, k, i);" e) O' Z, O3 t; O0 _8 J) R5 O( Q
} }
- R% ^& c9 C N0 I// Method1:JDBC Method2:hibernate
1 x/ B' {2 v% o6 KString cName = jcbCourse.getSelectedItem().toString().trim();
. D6 G7 D/ F$ b2 O; F1 L- I! ?sql = "select x.sname,k.term,k.cno from "+ G/ v* F) ], b
+ "choicesxx x join Coursexx k "
* F* p$ C/ J2 Y {8 s# H4 `+ "on x.cno=k.cno and k.cname=?"; Y8 }0 \( m; E* I5 c
try {
6 B$ w& ^+ y, E9 e* d+ z) K- G& Zps = con.prepareStatement(sql);
& n4 `& u* U! F, O6 X% }ps.setString(1, cName);# D3 n$ X2 z, l# i$ g% `8 J: {
rs = ps.executeQuery();
6 R2 j+ ^0 p4 X8 w! O7 ometa = rs.getMetaData();* e. O+ n, B+ z. w
cols = meta.getColumnCount();; ]2 d3 z: c$ V0 U# |( p5 h
boolean flag = true;3 T* a3 s9 f$ o9 d2 g/ D
String result = null;2 F. B; b; Y z9 f
int k = 1;
' g, G0 t$ _. b" J9 Dwhile (rs.next()) {
2 z# \% x2 M, o: K/ ]9 C3 j( c1 Hfor (int i = 1; i <= cols; i++) {
* C) y4 h; X3 F) t: x) x+ {result = rs.getString(i);4 C( t1 W+ O/ `$ e2 r* m' u; f
jtAddScore.setValueAt(result, k - 1, i - 1);
, D3 D' I: C: C n* v' q3 V}! v& ?# W9 P* R1 d; o; A. I# ]
rowCount = k++;
5 J! ?% W3 K/ F+ ?} } catch (Exception exx) {8 a. J7 v' c/ n* N
exx.printStackTrace();" G/ t j; X- j+ V7 e. U1 P2 Q
}
& ^9 u3 Y& H0 W, U# j} * r6 C" X2 G* ?- P
//产生用户界面 public void CreateAddScoreGUI() {
0 _# x7 {4 f) w$ _6 Qthis.setLayout(new FlowLayout()); JLabel jlCourse = new JLabel("课程:");
9 u, G: |1 m: M; k" LjcbCourse.addActionListener(this);6 t) |0 l6 y3 ^2 u. T/ L( {
jcbCourse.addItem(new String("请选择课程"));
; W- K3 n+ T# g) q0 XaddCourseToJCheckBoxCourse(); |: U* {: l9 q
this.add(jlCourse);7 h+ @! j0 x4 C( j4 b a1 i
this.add(jcbCourse); // JLabel jlClassName = new JLabel("班级:");
. B+ y, m" Q" X1 X3 Q) L// jcbClassName.addActionListener(this);
- }3 s, m6 w1 ?+ Z9 o+ W// jcbClassName.addItem(new String("请选择班级"));& D$ Y- ]. L" E9 h h' R
// addClassNameToJCheckBoxClassName();1 M" f6 N. j8 [! {) m2 g, \
// this.add(jlClassName);
1 G/ x3 ]. @- c6 X) D// this.add(jcbClassName); // JLabel jlTerm = new JLabel("学期:");1 k5 K# \6 ^; M4 p
// jcbTerm.addActionListener(this);
# E, B. ^% x3 G& P// jcbTerm.addItem(new String("请选择学期"));
) n2 d* O3 _7 w6 D$ J8 S6 i/ D) x// addTermToJCheckBoxTerm();
1 ~1 k0 O, Z. i6 u9 |// this.add(jlTerm);
4 i( F! d. D' r" J// this.add(jcbTerm); JButton jbSearch = new JButton("查询");& b8 j! r. [" |
jbSearch.addActionListener(this);; ?) t* q/ j; d. {- p4 Q, c
JButton jbOK = new JButton("提交");
* V8 f# u' j7 a8 FjbOK.addActionListener(this);4 `; E8 p5 m$ C/ h
JButton jbExit = new JButton("退出");: x1 r4 x! j, T! X
jbExit.addActionListener(this);
: m0 M( e* p" D# `. s% B- Ethis.add(jbSearch);
1 q5 t1 ?: T+ j( D: L5 s) l0 Qthis.add(jbOK); X+ ~1 N! `( Q m0 m
this.add(jbExit); this.add(jspScore); this.setVisible(true);; m3 K" T8 }4 `2 v. B- ]
this.setSize(500, 600); } public static void main(String[] args) {
z$ N1 c& u% Y2 Y. {new AddScore();
1 G) Y* |- y, m9 e& q( `# o}
% u5 S t! z* h6 G8 g/ Q# b5 w( g} * _2 H. B6 H1 Q' `6 Y
|