//需求分析:写一个成绩管理系统。其中的一个功能是实现成绩录入。要求能根据课程名称在系统里找到学生和课程信息;在结果区域中添加成绩。 //规则:要求对输入成绩的操作要有有效性检验。成绩只接受大于0且小于100的数字;同一记录不能反复提交;当点击“提交”,告知用户操作是否成功,并给出相应信息;课程名下拉列表中的课程列表根据数据库中的课程表,自动生成。 6 V; j" \1 v- V
//代码如下:: q+ [3 {1 A7 c0 ~- c$ [5 ?
import java.awt.*;
7 V* _8 Y: o1 g( Himport java.awt.event.*;: w3 T6 H: E0 @
import java.sql.*; import javax.swing.*;* b9 X( \( }' v8 f2 ~
import javax.swing.table.DefaultTableModel; import school.schoolPicture.JdbcUtil; public class AddScore extends JPanel implements ActionListener {: G' ~+ }$ w' g
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();8 ]; j5 f3 R. E7 T, c% s2 {
Connection con = JDBCUtil.getConnection();
0 `. d; `2 a4 X% t) J* i// Connection con2 = JdbcUtil.getConnection();
& H2 i! _: }3 k2 zPreparedStatement ps = null;. S1 c8 K- A! N$ Q8 d
ResultSet rs = null;( |9 V8 {* F6 [8 J% `, O
ResultSetMetaData meta = null;- f% F' l& {- D& E6 R' A: W
String sql = null;3 }: W0 |# E( Q/ G
int cols = 0;. k$ {* ?3 u' S- a, Z# G; R
int rowCount = 0; public AddScore() {# N7 K- v c( p' W8 } j) j
CreateAddScoreGUI();" j- A/ Y( l2 v. [$ b5 n0 x; m
} public void actionPerformed(ActionEvent e) {
4 P4 d' e( \7 T# nString str = e.getActionCommand();
# Y4 s( P& S7 B% l# eif ("查询".equals(str)) { //当点击查询时,用searchScore()方法对其进行处理
+ Y$ t0 ]# V! A" ~. X& y: F! Z8 A! xsearchScore();
) v7 ^" P7 L8 @, s9 P' d} else if ("提交".equals(str)) { //当点击提交时,用tryCommit()方法对其进行处理$ B5 Q8 K. _( `; [! m' h% |) n
tryCommit(); } else if ("退出".equals(str)) {1 G L/ |* O9 ]* D
this.setVisible(false);
8 N* ?8 u1 b2 l' r7 p }}: u* d! d* V8 W: }, S1 `6 w3 p- a
}
/ u4 C. U# m1 G. |5 epublic void tryCommit() {% ?- }0 P4 \3 Y. C, ?# ^! w! J
if (cols < 1)6 j y2 C/ q9 j7 t
return;
; w( H' G/ B- o# ysql = "insert into scorexx values(?,?,?,?)";7 C7 O+ V3 i+ y" z4 x! {7 G5 M
try {
* Z }7 M; i }$ Q) J- D# Hps = con.prepareStatement(sql);
. d) s, V) `* H0 J! Z9 t. DString[] value = new String[4];
. Y7 M9 \' y- Afor (int k = 0; k < rowCount; k++) {. Q( T" t- J; r
for (int i = 1; i <= cols + 1; i++) {
& @) a* B' i" _" c }) h// 如果成绩为空,取消提交操作,并给出提示。* H+ h" j& E) |6 ?- T/ |
if (jtAddScore.getValueAt(k, i - 1) != null) {
! U6 z0 \% x5 G9 vvalue[i - 1] = jtAddScore.getValueAt(k, i - 1)
3 ^6 L2 I+ z9 L6 z9 B# ^.toString();7 Q4 B5 h8 K4 z' Q5 R
} else {
% M3 e1 h1 o2 Z3 n8 r( T( z7 Jif (i == 4)
6 s( n1 |/ H. Z/ d7 u; Z( yJOptionPane
+ p* W) F5 @* @/ J) A.showMessageDialog(null, "无效成绩输入!成绩不能为空");
* _) S5 ^) Z: [/ n6 hreturn;
& \# M3 X. [; c" Z0 p}
. _0 ]8 w# y& n( l+ k// 如果成绩字段里面不是数字,则取消本次操作??& h8 N9 c% }+ u- ?8 a* f" B% L
// String str = "^[0-9]{1}[0-9]?[0-9]?$";) ^$ m* ^3 H9 @( J0 y' h7 K. l* I5 j. W
// if (i == 4) {5 _% g! L6 p: m: ^
// if(jtAddScore.getValueAt(k, i - 1) instanceof Integer){: [ e( G5 Y5 \) I$ t0 \
// if(
( I, o' c, ~9 L. ?# ^// Integer.parseInt(value[3])<0||Integer.parseInt(value[3])>100)4 q7 p- G6 u E; g2 ~4 H
// {JOptionPane.showMessageDialog(null, "成绩只能大于0且小于100");& M, w( q* I( g& ?' l
// return;}
' Z L% [. S; H. s& F: B6 n// }9 T) ?0 R' K# d7 R6 |8 ?
// }
% }6 X6 z( Y% S9 q, iSystem.out.print(value[i - 1]);
. [* s- q6 L6 u7 u0 A}; _2 m0 O, ^: R1 n+ p
ps.setString(1, value[0]);7 T. a. p5 b/ t7 q
ps.setString(2, value[1]);
8 l/ V+ V) D+ s \- gps.setString(3, value[2]);
6 K% b/ y% r7 Ips.setInt(4, Integer.parseInt(value[3]));% l2 S& r* T( S7 R; Q9 u& m3 \6 l- l) G/ B
ps.executeUpdate();1 Y6 m5 y% x p& X( ]) T
con.commit();
3 i! y0 m$ Q: p# C: n2 g7 DSystem.out.println();1 X- x. J! D# l& [4 [! U3 i! @, e3 R% A
}
4 Y& @" ~4 c( e0 w0 eJOptionPane
2 Y9 _' E5 d7 r6 r% `.showMessageDialog(null, "提交成功:更新 " + rowCount + " 条记录!");' N9 J z3 ?7 o8 z$ c
} catch (Exception ew) {
6 G& A/ x4 H6 T. v' gew.printStackTrace(); } }
0 m' p, e0 m/ j4 w; K9 _; ]$ G// 自动添加课程下拉列表中的课程
5 p. X. v5 o. S1 h6 Lpublic void addCourseToJCheckBoxCourse() {/ K4 }! Z* l: B% ?
String courseName = null;: I! \+ K9 C. T( O/ p
sql = "select cname from coursexx";
* {8 C' P/ ]# \try {8 ]+ l' J5 J( B0 p
ps = con.prepareStatement(sql);
3 H, ^2 o3 {* l, ~1 y/ [+ v. Brs = ps.executeQuery();
5 F! G# l) r: F: C8 T7 FResultSetMetaData meta = rs.getMetaData();" i/ I A/ i) N/ f* E$ k! S4 J
int cols = meta.getColumnCount();
0 s( Z( D$ k% N# mwhile (rs.next()) {; N: ?. m% P. x7 E2 h9 r
for (int i = 1; i <= cols; i++) {* C \7 i: m; F) Q+ F* Q
courseName = rs.getString(i);
, O, ~( t, u0 k8 Z+ ~System.out.println(courseName);
: S! q6 C) R. K p# |: ?jcbCourse.addItem(courseName);
5 E$ E, z0 y5 c+ k0 l; {rowCount++;
5 T' o5 ?6 l0 X; H+ ~0 e}% _" q) h" p" Z
}) ]7 ~2 P# S6 g% B/ X& l
con.commit();8 L T @+ ^; m( J& E/ X8 @# Q
} catch (Exception ers) {
+ E1 j& m8 q/ Jers.printStackTrace();& a. r% R, ]. k
try {4 Y7 e/ D3 ?" f1 n% p D
con.rollback();
; U3 X% x/ Z N% P3 n3 ?+ G% Z. q} catch (Exception er) {
$ [: X1 O, Y eer.printStackTrace();' J4 v- M H9 J6 o
}
- e* N- }( c9 p9 a} }
! D/ u; R+ e2 n/ `( Epublic void searchScore() {/ \# n- N7 Q% I% _7 e2 H
// 每次查询之前清空之前的结果
5 y) y6 E+ j# f' r$ ffor (int k = 0; k < 50; k++) {6 ]* D- y7 o, p# B i9 x5 P: d* s
for (int i = 0; i < cols; i++) {
' w7 j' e: }. ?$ n3 kjtAddScore.setValueAt(null, k, i);
" v2 X# Q" w( e2 l u} }# U: m/ `& I, ]& ~8 E+ [# E) t! V
// Method1:JDBC Method2:hibernate- X8 A! f! L# i
String cName = jcbCourse.getSelectedItem().toString().trim();7 G5 C! p, y& b9 f+ q t; t' W, ]
sql = "select x.sname,k.term,k.cno from "& V! @% H& s' B
+ "choicesxx x join Coursexx k "& i- g) y4 K3 u( q- h3 N& Q' n
+ "on x.cno=k.cno and k.cname=?";; p* x$ q% ]' t+ [
try {$ e0 X- a; \' c5 F' Y# {
ps = con.prepareStatement(sql);% O, J, A* T1 ?3 Q3 c/ ^
ps.setString(1, cName);
: |' [ H% f% [$ t* G( J& P: W# urs = ps.executeQuery();9 I; T+ p* _* I- y# s
meta = rs.getMetaData();2 C9 ?! I" Q5 x O- D
cols = meta.getColumnCount();# \& C, r+ m1 e2 E7 i/ \
boolean flag = true;
8 ] r- c; U8 l6 f# A6 JString result = null;
8 g5 B! {9 L E0 sint k = 1;
( T) p* i: [. C# c( {: {while (rs.next()) {
4 e& ] t; @3 ?' S$ E' j3 {for (int i = 1; i <= cols; i++) {! P! ]3 S0 b! ^: w r& a
result = rs.getString(i);
6 l: s- u/ q [/ {- T+ mjtAddScore.setValueAt(result, k - 1, i - 1);& j6 }! J7 b* n2 t/ J# v
}* B0 r' y& u r; n1 i
rowCount = k++;4 X. E# u8 J/ J B+ ]* Y
} } catch (Exception exx) {
$ \/ j& Z+ P5 Wexx.printStackTrace();1 {/ [& D! B1 ], O1 k
}& S" M" O) l8 g
}
2 C- K% i/ n7 e& w* V: R' H//产生用户界面 public void CreateAddScoreGUI() {- a5 m8 C, R% V) S
this.setLayout(new FlowLayout()); JLabel jlCourse = new JLabel("课程:");3 L4 s4 e; ^. l3 F/ O
jcbCourse.addActionListener(this);. y; H5 e1 I, g( a; {# K
jcbCourse.addItem(new String("请选择课程"));* h$ y6 ? N1 u) Q8 r2 d
addCourseToJCheckBoxCourse();4 a4 v" J& O- |, t2 v
this.add(jlCourse);1 Q0 l& p! Y1 z; T* v
this.add(jcbCourse); // JLabel jlClassName = new JLabel("班级:");& `9 P7 X# ?' J
// jcbClassName.addActionListener(this);3 ?( f3 d7 h9 z) f, O
// jcbClassName.addItem(new String("请选择班级")); U' Z7 ^4 j' ]. A) z' p; D5 w1 y- v
// addClassNameToJCheckBoxClassName();
2 k7 l1 {- q; [1 Z3 \5 G// this.add(jlClassName);0 ?7 f o: }+ h S8 a
// this.add(jcbClassName); // JLabel jlTerm = new JLabel("学期:");8 I/ h. b: O+ x/ _
// jcbTerm.addActionListener(this);- H7 u" `& ~ j9 Z8 `
// jcbTerm.addItem(new String("请选择学期"));
' h9 k4 V$ N/ e+ ]6 C// addTermToJCheckBoxTerm();
; y, C6 f3 {+ A" [" x/ x1 t2 P// this.add(jlTerm);: q' f8 D# q9 L% u# P$ `
// this.add(jcbTerm); JButton jbSearch = new JButton("查询");$ w3 h8 J' b- O/ g6 V4 C! g
jbSearch.addActionListener(this);
/ A* R, B% M% E( [0 PJButton jbOK = new JButton("提交");1 v/ `3 a4 r1 o' r# Y
jbOK.addActionListener(this);9 G2 q/ X% B9 R; E8 G
JButton jbExit = new JButton("退出");" p2 i7 Y1 w/ I0 L& r9 \
jbExit.addActionListener(this);
3 O: Q3 \7 G. d7 lthis.add(jbSearch);
) G9 X; s* S2 A3 e9 Gthis.add(jbOK);( w3 Q! q0 J3 j J$ n/ a( c
this.add(jbExit); this.add(jspScore); this.setVisible(true);! `' }* d( K# [7 r! G
this.setSize(500, 600); } public static void main(String[] args) {* X) R! ]4 o/ t
new AddScore();8 m8 o# ~; |7 p# [0 u7 o# W* j
}- v9 ^1 I3 g7 k& b# `
}
5 E5 X( [1 a. M0 |9 s |