//需求分析:写一个成绩管理系统。其中的一个功能是实现成绩录入。要求能根据课程名称在系统里找到学生和课程信息;在结果区域中添加成绩。 //规则:要求对输入成绩的操作要有有效性检验。成绩只接受大于0且小于100的数字;同一记录不能反复提交;当点击“提交”,告知用户操作是否成功,并给出相应信息;课程名下拉列表中的课程列表根据数据库中的课程表,自动生成。
( x6 s" |- h M//代码如下:0 k# Q" m3 F+ k8 q
import java.awt.*;, c5 b: U) X: k3 O2 s% v6 T
import java.awt.event.*;( v3 E9 Y! c k0 f8 [- w
import java.sql.*;
import javax.swing.*;6 y8 Y& M) ]7 n+ ?' Y; n
import javax.swing.table.DefaultTableModel; import school.schoolPicture.JdbcUtil; public class AddScore extends JPanel implements ActionListener {- ^0 k% X9 E2 ~9 v R# S
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();/ B6 h% r* Q2 Q+ p2 Z$ B) x
Connection con = JDBCUtil.getConnection();
# K. P, `! G& a// Connection con2 = JdbcUtil.getConnection();. v2 f3 m; [& b: W) l6 w6 ^
PreparedStatement ps = null;
. y/ N: U4 \/ ]5 q- g! E- CResultSet rs = null;! t& _. K8 ~ B: R
ResultSetMetaData meta = null;
* j- Z" T) O; P8 b6 k3 s7 _String sql = null;% A& N2 O; y9 g" }; K% ^3 D4 s
int cols = 0;
& e7 z/ K" O+ _4 @ I( lint rowCount = 0; public AddScore() {) L8 b% E) T8 S1 o! o9 ^
CreateAddScoreGUI();. e& [3 S1 D# ^1 ^
} public void actionPerformed(ActionEvent e) {. l7 M! R8 @5 t9 O( F! U( A* m/ J) @
String str = e.getActionCommand();+ a2 q& q9 |3 M
if ("查询".equals(str)) { //当点击查询时,用searchScore()方法对其进行处理" W9 m& R7 {) L, v8 {: V* k0 G n0 z# L
searchScore();
' _* c, Q- }. {/ \) g: S; M' l} else if ("提交".equals(str)) { //当点击提交时,用tryCommit()方法对其进行处理
; s [$ h" u3 c. l' D# l( dtryCommit(); } else if ("退出".equals(str)) {
9 o$ W# k2 k: K4 M/ S7 G3 e s1 T0 jthis.setVisible(false);9 \; a! ^) w" v) v' y" o: D. g
}
- l+ l1 v# K, x5 k+ O+ i q}
3 `4 o {' d( O& J4 s! }public void tryCommit() {1 z& b; o) }7 Q5 r X
if (cols < 1)5 R2 A& `) S; [2 Y) y
return;
+ |! @: k. [6 u9 _% ~' D' W: w. h' p$ Tsql = "insert into scorexx values(?,?,?,?)";, z# J% Q# u+ S, ]
try {
- V6 A: U( Z3 `0 H: @6 Ops = con.prepareStatement(sql);
5 I" M4 a# V( e) T( E5 H% L3 z# `5 DString[] value = new String[4];( t* z: _7 [4 N
for (int k = 0; k < rowCount; k++) {" W Y; K7 m3 @/ o
for (int i = 1; i <= cols + 1; i++) {
; F$ G0 R1 q% R( i' Q9 \! D7 n6 l// 如果成绩为空,取消提交操作,并给出提示。
( ?. e8 P2 m+ a5 u* r% @if (jtAddScore.getValueAt(k, i - 1) != null) {
* T7 _/ l4 E' ]4 A! h8 Qvalue[i - 1] = jtAddScore.getValueAt(k, i - 1)9 C2 a4 k5 B% P6 V: C
.toString();. p- g, E0 d0 A. v
} else {8 o% b. d" t. W g
if (i == 4)6 ?2 X, n7 J; Z& n/ P) m% M8 i
JOptionPane
$ m; O+ n! d7 J4 w6 f.showMessageDialog(null, "无效成绩输入!成绩不能为空");1 ~( x/ g/ l% {
return;/ h) W* J8 P0 d) R
}! d6 I. N# L) l9 M
// 如果成绩字段里面不是数字,则取消本次操作??* K6 L' H/ y, I4 S% n" t* V) q
// String str = "^[0-9]{1}[0-9]?[0-9]?$";. t2 c- [- @7 Y. B
// if (i == 4) {
% t6 P! r o. i; }# v* q// if(jtAddScore.getValueAt(k, i - 1) instanceof Integer){9 X% K1 \+ k! Z% Z: ~5 T
// if(
1 j! M7 |6 T3 `# C9 ^- w// Integer.parseInt(value[3])<0||Integer.parseInt(value[3])>100)4 S5 @0 _' k: i% E4 o& ?
// {JOptionPane.showMessageDialog(null, "成绩只能大于0且小于100");0 V& j0 l6 Z! }+ ~8 E# g
// return;}9 I3 ~; `& y$ X( d" T$ D& C: z/ v0 }
// }
[& M/ a$ m2 C; O- u, K# F* N/ F/ z2 h% j// }
o3 a4 ]' M; a4 v! Y7 ~. nSystem.out.print(value[i - 1]);0 ~6 x1 g; n" ?9 l
}. H. |: l) W5 W) f6 `
ps.setString(1, value[0]);& u2 G8 I- W, q( U( U# Z2 p+ C
ps.setString(2, value[1]);1 F8 l/ Y$ H1 b; q8 T+ t1 r
ps.setString(3, value[2]);
) D3 v8 H1 [# M& D2 xps.setInt(4, Integer.parseInt(value[3]));* [( @8 {5 F6 |/ N! `1 w1 r2 v( k
ps.executeUpdate(); g, v* W) P# `9 n
con.commit();
: S* { p4 t& ^* W3 H' ]System.out.println();8 a% g3 f |9 }) z
}( l; B) r' R% w
JOptionPane7 A) s9 U1 }7 |
.showMessageDialog(null, "提交成功:更新 " + rowCount + " 条记录!");
& w6 B6 _5 u2 H- g u+ A} catch (Exception ew) {
I+ c$ B; G2 P: `ew.printStackTrace(); } }
5 \5 m* S+ t+ D' Q( H, J5 U3 L// 自动添加课程下拉列表中的课程
9 P4 r, E% x4 a8 `: e# y: npublic void addCourseToJCheckBoxCourse() {! v9 s4 d6 t4 d1 `
String courseName = null;
7 }/ e0 @9 Q' g7 ?5 ~sql = "select cname from coursexx";' d+ R* N* `/ L
try {# @$ ^& B) w" a* Z& R
ps = con.prepareStatement(sql);$ ]/ \! x% J7 P9 w
rs = ps.executeQuery();) w/ U7 F9 P3 \2 z# `* P
ResultSetMetaData meta = rs.getMetaData();
1 |+ F9 Y) Y. Cint cols = meta.getColumnCount();" u1 G2 r3 p$ R8 p# e
while (rs.next()) {) H# {% g+ c% E/ ~ U1 r1 W
for (int i = 1; i <= cols; i++) {
/ T" U+ e; G) B: @% Y. o5 U! vcourseName = rs.getString(i);
4 d' j+ E5 A) Q& HSystem.out.println(courseName);
) Q" _3 z& L y' zjcbCourse.addItem(courseName);- w8 ]1 E$ L3 K$ d: S7 u" O
rowCount++;
4 R2 \5 J0 A7 t2 i# g" o3 l}
P; T6 Z- f8 w, M. P}1 d5 k7 i% A c' m" d' ?
con.commit();
7 C; J8 E. S( ~( m} catch (Exception ers) {& j5 F6 n: g) \2 ~2 A+ C2 `) y% g
ers.printStackTrace();+ e5 \& j1 @" r6 J7 u$ s0 u. ]
try {
( d% |) y/ V6 L# c8 ]con.rollback();' |3 P+ i6 P, @* N- q; _7 j; C' K7 d
} catch (Exception er) {
* Z' O$ d' u5 K2 ler.printStackTrace();
4 p) o6 ]9 E) N- \2 Z}
) k5 b- j' B, A. g. f5 G4 p} } 6 w4 f) f( U a& r
public void searchScore() {+ y/ B6 m6 T/ v- a! T- n& Q
// 每次查询之前清空之前的结果
/ }* L6 z) T9 b( m- C' ~2 n v+ ofor (int k = 0; k < 50; k++) {
( r( _* N3 {8 @& `3 }/ _4 q' I2 V6 H) C+ |for (int i = 0; i < cols; i++) {
6 R/ }' O" I8 ]0 M; u& ZjtAddScore.setValueAt(null, k, i);
) h; o: J; C" p) o! y} }! M* C- ]2 Z% V/ l- }) }7 @
// Method1:JDBC Method2:hibernate
2 p6 D1 O1 l" O0 LString cName = jcbCourse.getSelectedItem().toString().trim();" M' u* T) U. \+ {
sql = "select x.sname,k.term,k.cno from "% q/ d! ~( _$ n% _( c1 d& @) U* K/ \
+ "choicesxx x join Coursexx k ": E- l+ l0 l1 G/ b5 z
+ "on x.cno=k.cno and k.cname=?";
8 ?6 f/ i( X, r6 J+ V/ l1 d, ctry {/ a0 U. `( X1 {& e
ps = con.prepareStatement(sql);
# S+ f& J g5 f5 k, \ps.setString(1, cName);9 y7 v# Z3 d7 y- {
rs = ps.executeQuery();
2 h3 W& I: }& i5 g0 p" ?2 \0 T3 Y$ ymeta = rs.getMetaData();" f% g, e- c& D# l4 l, R S E3 ^
cols = meta.getColumnCount();' _9 q4 {9 W8 O. G. I
boolean flag = true;
) p+ g# s( H2 j; VString result = null; i$ ]# W9 t' M2 M* \, T
int k = 1;0 E/ D" T4 U. o$ ]
while (rs.next()) {& `, m: `, j( L( Y
for (int i = 1; i <= cols; i++) {' W( _6 N! A' y9 T% I- y
result = rs.getString(i);
# i, r1 ]- q1 X# l/ kjtAddScore.setValueAt(result, k - 1, i - 1);
+ l5 _( W2 U" [% t9 L7 K}3 A* r% x) N' \ I4 ?6 ~) ?( P
rowCount = k++;# `$ R" b. c }4 I
} } catch (Exception exx) {
H( R) t" A% \9 v+ \exx.printStackTrace();
+ a, U6 Y ~# V2 M1 X2 i}
2 _, G3 X6 |% W, A1 ?} / _% C: w: t. ~ g
//产生用户界面 public void CreateAddScoreGUI() {" E3 f) G9 G1 ^; b4 y
this.setLayout(new FlowLayout()); JLabel jlCourse = new JLabel("课程:");; U" B# _. b% Z7 W1 l( d: C P
jcbCourse.addActionListener(this);; N7 D; ?1 M& y; q
jcbCourse.addItem(new String("请选择课程"));
# l( t& u- O QaddCourseToJCheckBoxCourse();
]5 s; B6 z$ W5 x2 n* s+ y, ithis.add(jlCourse);2 F% C: R$ D, O- z
this.add(jcbCourse); // JLabel jlClassName = new JLabel("班级:");& A# `2 a1 U: i
// jcbClassName.addActionListener(this);* ]; J! A x* K9 G/ C
// jcbClassName.addItem(new String("请选择班级")); b& b" k' U/ F) E
// addClassNameToJCheckBoxClassName();
; a; z+ [# k5 f) \! r) T5 d// this.add(jlClassName);
, v2 `0 |* V; [0 z// this.add(jcbClassName); // JLabel jlTerm = new JLabel("学期:");
, L% R* R- [8 K// jcbTerm.addActionListener(this);) E; s* k, t1 P; c( `9 B
// jcbTerm.addItem(new String("请选择学期"));$ i0 y# N& R1 x9 e2 d. o' R4 c
// addTermToJCheckBoxTerm();
( X+ ]- {- Y8 {8 ?// this.add(jlTerm);0 \% G5 d: i& x0 D1 ^; b
// this.add(jcbTerm); JButton jbSearch = new JButton("查询");; Y& z$ x( S; g( a" m$ X3 |
jbSearch.addActionListener(this);
7 q' v7 S7 r( B# X# TJButton jbOK = new JButton("提交");
! Y/ }1 H. J1 R: L, L3 J- ujbOK.addActionListener(this);! j8 { v6 j0 f- M5 u0 R8 K% X
JButton jbExit = new JButton("退出");
9 M& t2 _" N( `jbExit.addActionListener(this);, h- G2 H/ [$ W
this.add(jbSearch); o. A$ z0 r, f/ W) S1 `
this.add(jbOK);5 ^4 w8 U" B9 A+ [3 N$ ~- T/ z! g' r
this.add(jbExit); this.add(jspScore); this.setVisible(true);
# h% b. i$ c8 Q: h3 hthis.setSize(500, 600); } public static void main(String[] args) {
' q/ X; s# V* \) o' Jnew AddScore();
/ |5 l7 Y6 M% `: _3 U$ n7 V}
8 j* ]7 Y3 z k5 Y! y1 r( d} 3 q/ F! g# m8 s/ f: t
|