欢迎来到Java学习网!
设为首页 | 加入收藏
当前位置: 首页 > Java其它 > Java源代码 >

//需求分析:写一个成绩管理系统。其中的一个功能是实现成绩录入。要求能根据课程名称在系统里找到学生和课程信息;在结果区域中添加成绩。

//规则:要求对输入成绩的操作要有有效性检验。成绩只接受大于0且小于100的数字;同一记录不能反复提交;当点击“提交”,告知用户操作是否成功,并给出相应信息;课程名下拉列表中的课程列表根据数据库中的课程表,自动生成。


//代码如下:
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[] { "姓名", "学期", "课程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++) {
// 如果成绩为空,取消提交操作,并给出提示。
if (jtAddScore.getValueAt(k, i - 1) != null) {
value[i - 1] = jtAddScore.getValueAt(k, i - 1)
.toString();
} else {
if (i == 4)
JOptionPane
.showMessageDialog(null, "无效成绩输入!成绩不能为空");
return;
}
// 如果成绩字段里面不是数字,则取消本次操作??
// 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() {
// 每次查询之前清空之前的结果
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();
}
}

 

//产生用户界面

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("学期:");
// jcbTerm.addActionListener(this);
// jcbTerm.addItem(new String("请选择学期"));
// 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();
}
}

    相关教程
    推荐内容