//需求分析:写一个成绩管理系统。其中的一个功能是实现成绩录入。要求能根据课程名称在系统里找到学生和课程信息;在结果区域中添加成绩。 //规则:要求对输入成绩的操作要有有效性检验。成绩只接受大于0且小于100的数字;同一记录不能反复提交;当点击“提交”,告知用户操作是否成功,并给出相应信息;课程名下拉列表中的课程列表根据数据库中的课程表,自动生成。
9 i: T- l9 `3 U) g/ p; M4 b8 G//代码如下:+ K+ S* y4 T1 p& K- S9 u
import java.awt.*;
* Y" E9 ` p! ?" ?9 k% u( Pimport java.awt.event.*;
7 B }. [- W' s9 \import java.sql.*;
import javax.swing.*;
1 \* X3 S+ a9 J8 g5 K9 g( Simport javax.swing.table.DefaultTableModel; import school.schoolPicture.JdbcUtil; public class AddScore extends JPanel implements ActionListener {
: |9 W8 }! c4 T( ]' NString[] 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();$ |* B8 W- o n# T' P" D7 \8 q( i# N
Connection con = JDBCUtil.getConnection();
) z2 \5 H, x" p: H* z// Connection con2 = JdbcUtil.getConnection();
$ c7 Q3 p4 _ D0 k- m6 mPreparedStatement ps = null;
2 E$ a4 y' ~3 S& w& ]0 k& WResultSet rs = null;
( ]" j9 M ^1 v' G. QResultSetMetaData meta = null;# a! }8 K) ]/ J7 |" u8 ]- V
String sql = null;
7 S) {: h3 Q: K2 `) m7 hint cols = 0;8 N% H0 {) R6 a; \4 p- H
int rowCount = 0; public AddScore() {- M! i6 x3 k/ |, u5 {
CreateAddScoreGUI();0 q7 F. Y6 _, q
} public void actionPerformed(ActionEvent e) {
N; Y4 [+ u ^/ ^, e# W0 RString str = e.getActionCommand();" d$ @" D$ P' z! H4 [& o/ P1 P" r
if ("查询".equals(str)) { //当点击查询时,用searchScore()方法对其进行处理
# a9 \) s. ~1 e9 o: S! ]3 v& AsearchScore();
) i! P1 e# f# m& _4 p( Z8 o} else if ("提交".equals(str)) { //当点击提交时,用tryCommit()方法对其进行处理
4 R; F% s8 \" R0 w0 q dtryCommit(); } else if ("退出".equals(str)) {8 E- N* g6 F. O8 g3 ?1 v d/ e
this.setVisible(false);
) Z0 n) j8 q/ R4 @}7 _6 ` I/ @8 q
}
2 z/ o1 V; L& l5 c& J8 a3 vpublic void tryCommit() {
3 w2 s( N+ C! A' Y/ L: Bif (cols < 1)
. y9 N) O, b& o* ^return;0 [9 ]2 l" ]* J
sql = "insert into scorexx values(?,?,?,?)";
: o* M5 l5 t6 m: ptry {/ ^3 z3 T( Q0 G) O" c6 x
ps = con.prepareStatement(sql);/ {9 B; {- x/ B: ?( r+ b5 P
String[] value = new String[4];
8 z4 a8 ?8 j3 v% T7 U: U, ^. E0 |: Pfor (int k = 0; k < rowCount; k++) {
9 O6 ?' s$ X5 x: d) e- n2 n% n1 pfor (int i = 1; i <= cols + 1; i++) { @2 t3 |2 }/ U# \3 S
// 如果成绩为空,取消提交操作,并给出提示。
: m4 n1 I6 A8 }if (jtAddScore.getValueAt(k, i - 1) != null) {
. d+ K0 O b+ q7 i' `! evalue[i - 1] = jtAddScore.getValueAt(k, i - 1): e% `0 [* O+ I( V) T
.toString();# Y# |2 x- b' D- g' E8 ?
} else {
5 m# H5 [, Y2 n1 uif (i == 4)
7 J1 f: J, {, G7 v0 m4 w9 dJOptionPane
p' J/ J, x: g: M* U9 s.showMessageDialog(null, "无效成绩输入!成绩不能为空");; R& r) t" _1 `0 |& K3 D1 q
return;% F5 i, g8 ]/ ^3 H
}6 |1 {2 j$ \ T6 R B7 y4 |
// 如果成绩字段里面不是数字,则取消本次操作??4 \8 ?8 z; `% D7 R
// String str = "^[0-9]{1}[0-9]?[0-9]?$";
$ E4 d0 K* r, t. B2 ?// if (i == 4) {
. }% F* ?$ @8 q( C9 D6 S, E// if(jtAddScore.getValueAt(k, i - 1) instanceof Integer){
( t( k+ I- X3 |6 W6 V/ v; K/ H// if(
8 R9 _ T( o1 O7 _1 t// Integer.parseInt(value[3])<0||Integer.parseInt(value[3])>100)/ d& ^) g" s3 a9 g
// {JOptionPane.showMessageDialog(null, "成绩只能大于0且小于100");
5 y$ D0 w* j- ^/ R) s' x% @6 d// return;}' k" ]3 n, w, d c+ q
// }: j2 Q" S# F2 ?
// }
/ t6 Z" e, r/ L. ~System.out.print(value[i - 1]);
2 Q9 T# Z( h1 w0 o- b}0 W6 S; {5 y4 v$ {8 h
ps.setString(1, value[0]);
1 j6 Q! z- j: t. qps.setString(2, value[1]); a/ j' h/ W: \/ C& T) o: }, K5 W
ps.setString(3, value[2]);0 j# C+ w/ b: K$ s# n
ps.setInt(4, Integer.parseInt(value[3]));
7 D/ j+ s. v1 H( w5 jps.executeUpdate();
$ r2 o8 i" ~% w1 ucon.commit(); _- Y( v- ?" Y6 ?/ |+ l4 h
System.out.println();
0 A& L9 Z3 k) g7 }) S}" L3 i3 q; h! A" }
JOptionPane
& r- V: `+ N+ e.showMessageDialog(null, "提交成功:更新 " + rowCount + " 条记录!");
! U! d* W( {1 {2 y/ x/ U} catch (Exception ew) {
) q) L6 t# l% W r7 Sew.printStackTrace(); } } ; u6 g8 y8 o' Y
// 自动添加课程下拉列表中的课程1 H2 d) d* ~: z4 ?+ q X2 e
public void addCourseToJCheckBoxCourse() {: e. P+ y. I+ ]& J3 S$ _: N
String courseName = null;8 N: }! m+ a% F8 r
sql = "select cname from coursexx";
) @4 t2 H4 i4 |. U9 O; Gtry {
. g* k! _- T) H% q8 L' dps = con.prepareStatement(sql);
6 `- E$ D4 M( w+ prs = ps.executeQuery();
! @2 g6 O y. |3 N4 ]9 DResultSetMetaData meta = rs.getMetaData();, e }/ B; j9 H( |2 G' l. d
int cols = meta.getColumnCount();; U7 o1 h) I5 K |5 G; v0 u5 L1 Y
while (rs.next()) {
' p5 b0 f b. s6 Hfor (int i = 1; i <= cols; i++) {/ L1 U' x4 Z+ i. z# k
courseName = rs.getString(i);
# L# i3 y0 T( O& W) t5 c; KSystem.out.println(courseName);
2 T. U: W( k' G; L9 [jcbCourse.addItem(courseName);; _( B! G7 X- @$ Q5 t. o/ Z
rowCount++;: [& H* |7 T8 f6 L$ y! u
}
6 r3 {0 ~0 v/ O! |}
. \* ~- A( ^8 J& ~1 E* e0 bcon.commit();, b" ?' i) _ \: e5 M
} catch (Exception ers) {
: p; h3 V5 e& u0 i8 X4 Zers.printStackTrace();
8 J! m( m6 [- d% W4 jtry {# b; f+ H0 h' J: X C
con.rollback();
* V2 N5 N9 X3 R4 B" G( [+ d: y2 v/ [* t+ ^} catch (Exception er) {! |' P. _4 M' r; D
er.printStackTrace();( A% i6 O" c5 Z0 ]7 K6 `
}
9 e/ F: n$ |5 R} } 5 s$ G; E) w4 y% b
public void searchScore() {( q+ i& y! I1 g- m# {9 T4 a
// 每次查询之前清空之前的结果
$ O' h8 J2 ^( e2 F% _& {* I* Zfor (int k = 0; k < 50; k++) {
* v) q: _& `) _6 Wfor (int i = 0; i < cols; i++) {
8 K! a9 H' q' b* D* |' @- I0 }jtAddScore.setValueAt(null, k, i);; j- j) T# B! w; Y' q
} }; V+ r: L% L$ s9 `& v+ a" I0 F, H0 `
// Method1:JDBC Method2:hibernate7 _9 S2 x/ v8 ]5 e: K. i
String cName = jcbCourse.getSelectedItem().toString().trim();6 E, k( X. o+ }: S! R/ Z1 g
sql = "select x.sname,k.term,k.cno from "6 h. G* [9 ?1 Z4 x9 h6 N% t2 X
+ "choicesxx x join Coursexx k "
1 x: [3 D+ F6 d+ v4 j _% P+ "on x.cno=k.cno and k.cname=?";
% z" ?7 m+ B6 v$ x* P3 Wtry { I1 Q) S0 N$ @# N
ps = con.prepareStatement(sql);7 r9 V9 U* O- ?' \9 V! E
ps.setString(1, cName);
: o, _3 b5 i5 drs = ps.executeQuery();. q; Y) Q; N5 e2 l) n7 v: A9 K
meta = rs.getMetaData();6 M) @# ?% c' T( U0 G
cols = meta.getColumnCount();
) M5 n/ S: f) F9 Cboolean flag = true;% [9 C9 z7 Y7 Q( v
String result = null;# j( D" t& h3 g+ E" f" v! }
int k = 1;
2 I5 ?$ {7 r$ }( ~9 k9 f# Swhile (rs.next()) {
# ]. f" X7 V2 y. Z- qfor (int i = 1; i <= cols; i++) {
4 }# I9 c; T: H3 P6 p; f Zresult = rs.getString(i);0 B2 @9 c! N2 ]* ]- ]8 e
jtAddScore.setValueAt(result, k - 1, i - 1);3 r! @0 l5 ^, W+ `2 R/ |
}% ^" d/ W6 V5 [$ h
rowCount = k++;+ g! Q6 T% [0 o5 Z( o
} } catch (Exception exx) {
8 y: |7 O( `* P1 x6 Mexx.printStackTrace();6 Q) }1 H7 p; R
}
% u. a2 c: z! a8 `, S}
7 C1 R8 |( R9 p. [2 \//产生用户界面 public void CreateAddScoreGUI() {. B4 _1 p# `' t% B
this.setLayout(new FlowLayout()); JLabel jlCourse = new JLabel("课程:");3 \6 K, p' ]0 R* A$ g1 R9 M1 ?; J
jcbCourse.addActionListener(this);
8 u5 F$ x+ Y- W5 HjcbCourse.addItem(new String("请选择课程"));4 U1 e0 p9 m7 S( s1 ~& R
addCourseToJCheckBoxCourse();
0 ?' x/ T1 C; cthis.add(jlCourse); }( V/ F2 J- k( a( a
this.add(jcbCourse); // JLabel jlClassName = new JLabel("班级:");
- O0 I3 N- w/ R! q- K// jcbClassName.addActionListener(this);$ w" X2 }# Q* R
// jcbClassName.addItem(new String("请选择班级"));
% a5 K/ m' C9 G0 p2 ?// addClassNameToJCheckBoxClassName();9 I" O$ F' R p/ ~4 H
// this.add(jlClassName);
* |# _6 D( c! S+ H& U1 ]9 E# T6 L// this.add(jcbClassName); // JLabel jlTerm = new JLabel("学期:");0 F* N! P0 x7 f& [! f" D
// jcbTerm.addActionListener(this);8 H. Q. [2 f" ~- H7 S) x5 w4 O7 v
// jcbTerm.addItem(new String("请选择学期"));
6 y* g, L- |$ O9 Q2 o- O// addTermToJCheckBoxTerm();
G, J' T2 J3 K+ V* @$ X// this.add(jlTerm);
, ~, }6 e0 x' ?/ I9 T9 I// this.add(jcbTerm); JButton jbSearch = new JButton("查询");3 a1 J' e. y' f7 }: ^4 a6 q/ `
jbSearch.addActionListener(this);
, n1 M( D$ z6 kJButton jbOK = new JButton("提交");
9 A$ l' }! h7 x! W5 djbOK.addActionListener(this);/ V/ Z5 x+ {( X. i+ t4 X$ \
JButton jbExit = new JButton("退出");" E4 M: S; B5 B
jbExit.addActionListener(this);
z- Z# j7 ?, {this.add(jbSearch);* e I. S* R- v7 M2 _
this.add(jbOK);
; x) X' w8 c$ Vthis.add(jbExit); this.add(jspScore); this.setVisible(true);. M q/ |4 ~+ w3 q3 `
this.setSize(500, 600); } public static void main(String[] args) {8 G* C5 L5 a. h
new AddScore();
3 D g4 u; x% G' o- g4 K}9 c# ]8 p: D( J/ p
} 5 s) i+ y9 k) y1 X( C7 y
|