//需求分析:寫一個成績管理系統(tǒng)痴奏。其中的一個功能是實現(xiàn)成績錄入劲妙。要求能根據(jù)課程名稱在系統(tǒng)里找到學(xué)生和課程信息掖桦;在結(jié)果區(qū)域中添加成績限府。
//規(guī)則:要求對輸入成績的操作要有有效性檢驗夺颤。成績只接受大于0且小于100的數(shù)字;同一記錄不能反復(fù)提交胁勺;當點擊“提交”世澜,告知用戶操作是否成功,并給出相應(yīng)信息署穗;課程名下拉列表中的課程列表根據(jù)數(shù)據(jù)庫中的課程表寥裂,自動生成嵌洼。
//代碼如下:
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import school.schoolPicture.JdbcUtil;
public class AddScore extends JPanel implements ActionListener {
String[] columnNames = new String[] { "姓名", "學(xué)期", "課程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();
Connection con = JDBCUtil.getConnection();
// Connection con2 = JdbcUtil.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
ResultSetMetaData meta = null;
String sql = null;
int cols = 0;
int rowCount = 0;
public AddScore() {
CreateAddScoreGUI();
}
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if ("查詢".equals(str)) {
//當點擊查詢時,用searchScore()方法對其進行處理
searchScore();
} else if ("提交".equals(str)) {
//當點擊提交時封恰,用tryCommit()方法對其進行處理
tryCommit();
} else if ("退出".equals(str)) {
this.setVisible(false);
}
}
public void tryCommit() {
if (cols < 1)
return;
sql = "insert into scorexx values(?,?,?,?)";
try {
ps = con.prepareStatement(sql);
String[] value = new String[4];
for (int k = 0; k < rowCount; k++) {
for (int i = 1; i <= cols + 1; i++) {
// 如果成績?yōu)榭章檠∠峤徊僮鳎⒔o出提示诺舔。
if (jtAddScore.getValueAt(k, i - 1) != null) {
value[i - 1] = jtAddScore.getValueAt(k, i - 1)
.toString();
} else {
if (i == 4)
JOptionPane
.showMessageDialog(null, "無效成績輸入鳖昌!成績不能為空");
return;
}
// 如果成績字段里面不是數(shù)字,則取消本次操作??
// String str = "^[0-9]{1}[0-9]?[0-9]?$";
// if (i == 4) {
// if(jtAddScore.getValueAt(k, i - 1) instanceof Integer){
// if(
// Integer.parseInt(value[3])<0||Integer.parseInt(value[3])>100)
// {JOptionPane.showMessageDialog(null, "成績只能大于0且小于100");
// return;}
// }
// }
System.out.print(value[i - 1]);
}
ps.setString(1, value[0]);
ps.setString(2, value[1]);
ps.setString(3, value[2]);
ps.setInt(4, Integer.parseInt(value[3]));
ps.executeUpdate();
con.commit();
System.out.println();
}
JOptionPane
.showMessageDialog(null, "提交成功:更新 " + rowCount + " 條記錄低飒!");
} catch (Exception ew) {
ew.printStackTrace();
}
}
// 自動添加課程下拉列表中的課程
public void addCourseToJCheckBoxCourse() {
String courseName = null;
sql = "select cname from coursexx";
try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
ResultSetMetaData meta = rs.getMetaData();
int cols = meta.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= cols; i++) {
courseName = rs.getString(i);
System.out.println(courseName);
jcbCourse.addItem(courseName);
rowCount++;
}
}
con.commit();
} catch (Exception ers) {
ers.printStackTrace();
try {
con.rollback();
} catch (Exception er) {
er.printStackTrace();
}
}
}
public void searchScore() {
// 每次查詢之前清空之前的結(jié)果
for (int k = 0; k < 50; k++) {
for (int i = 0; i < cols; i++) {
jtAddScore.setValueAt(null, k, i);
}
}
// Method1:JDBC Method2:Hibernate
String cName = jcbCourse.getSelectedItem().toString().trim();
sql = "select x.sname,k.term,k.cno from "
+ "choicesxx x join Coursexx k "
+ "on x.cno=k.cno and k.cname=?";
try {
ps = con.prepareStatement(sql);
ps.setString(1, cName);
rs = ps.executeQuery();
meta = rs.getMetaData();
cols = meta.getColumnCount();
boolean flag = true;
String result = null;
int k = 1;
while (rs.next()) {
for (int i = 1; i <= cols; i++) {
result = rs.getString(i);
jtAddScore.setValueAt(result, k - 1, i - 1);
}
rowCount = k++;
}
} catch (Exception exx) {
exx.printStackTrace();
}
}
//產(chǎn)生用戶界面
public void CreateAddScoreGUI() {
this.setLayout(new FlowLayout());
JLabel jlCourse = new JLabel("課程:");
jcbCourse.addActionListener(this);
jcbCourse.addItem(new String("請選擇課程"));
addCourseToJCheckBoxCourse();
this.add(jlCourse);
this.add(jcbCourse);
// JLabel jlClassName = new JLabel("班級:");
// jcbClassName.addActionListener(this);
// jcbClassName.addItem(new String("請選擇班級"));
// addClassNameToJCheckBoxClassName();
// this.add(jlClassName);
// this.add(jcbClassName);
// JLabel jlTerm = new JLabel("學(xué)期:");
// jcbTerm.addActionListener(this);
// jcbTerm.addItem(new String("請選擇學(xué)期"));
// addTermToJCheckBoxTerm();
// this.add(jlTerm);
// this.add(jcbTerm);
JButton jbSearch = new JButton("查詢");
jbSearch.addActionListener(this);
JButton jbOK = new JButton("提交");
jbOK.addActionListener(this);
JButton jbExit = new JButton("退出");
jbExit.addActionListener(this);
this.add(jbSearch);
this.add(jbOK);
this.add(jbExit);
this.add(jspScore);
this.setVisible(true);
this.setSize(500, 600);
}
public static void main(String[] args) {
new AddScore();
}
}