之前做了一个外卖点餐系统之后点此查看详情,这段时间在这个系统的基础上改了改帮几位同学实现了其他的几个系统,本文所述的学生课程成绩管理系统就是其中之一。
一、数据库表及字段
1、学生表

2、课程表

3、成绩表

4、管理员表

二、系统运行界面图(部分)
1、登陆界面

2、管理员首界面

3、成绩统计界面





并将统计结果写入外部文件:



4、学生管理界面

管理中心里面的功能基本都差不多,所以就列举上面几个,下面展示主要代码。
三、部分核心代码
1、登录功能
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=student_course_grade";
String username = "sa";
String password = "123";
Connection connection = null;
PreparedStatement ps=null;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel2;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JScrollPane jScrollPane1;
private JTextField jTextField1;
private javax.swing.JToggleButton jToggleButton1;
public guanlidenglu() {
initComponents();
}
private void connectdb() {
try {
Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver" );
connection = DriverManager.getConnection(url, username, password );
}
catch ( ClassNotFoundException cnfex ) {
System.err.println(
"装载 JDBC/ODBC 驱动程序失败。" );
cnfex.printStackTrace();
System.exit( 1 );
}
catch ( SQLException sqlex ) {
System.err.println( "无法连接数据库" );
sqlex.printStackTrace();
System.exit( 1 );
}
}
private void initComponents() {
jPanel2 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jToggleButton1 = new javax.swing.JToggleButton();
jButton3 = new javax.swing.JButton();
jPasswordField1 = new javax.swing.JPasswordField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel2.setBackground(new java.awt.Color(120, 150, 190));
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "学生成绩管理系统", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("微软雅黑", 1, 24), new java.awt.Color(255, 255, 255))); // NOI18N
jPanel2.setAutoscrolls(true);
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jLabel1.setText("账号:");
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jLabel2.setText("密码:");
jTextField1.setColumns(20);
jScrollPane1.setViewportView(jTextField1);
jButton1.setText("登录");
jButton2.setText("注册");
jToggleButton1.setText("退出");
jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton1ActionPerformed(evt);
}
});
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try {
jButton1ActionPerformed(evt);
} catch (SQLException e) {
e.printStackTrace();
}
}
});
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("帮助");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jToggleButton1))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(162, 162, 162)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(jButton1)
.addGap(85, 85, 85)
.addComponent(jButton2))
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jPasswordField1, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)))
.addContainerGap(171, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(69, 69, 69)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(51, 51, 51)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(36, 36, 36)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(41, 41, 41)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 118, Short.MAX_VALUE)
.addComponent(jToggleButton1))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3)
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(113, 113, 113)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(290, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(441, Short.MAX_VALUE))
);
pack();
}
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) throws SQLException {
String Ausername=jTextField1.getText();
String Apassword=jPasswordField1.getText();
connectdb();
String sql="select adminName,adminPassword from admin where adminName='"+Ausername+"' and adminPassword='"+Apassword+"'";
ps=connection.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
if(rs.next()) {
guanlidenglu.this.setVisible(false);
guanli Guanli=new guanli();
Guanli.setVisible(true);
}
else {
JOptionPane.showMessageDialog(null,"密码或用户名错误!!!!");
}
connection.close();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(null,"请联系BOSS申请管理员!!!");
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(guanlidenglu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(guanlidenglu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(guanlidenglu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(guanlidenglu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new guanlidenglu().setVisible(true);
}
});
}
2、成绩统计功能
Connection conn = null;
Statement stmt = null;
PreparedStatement pstm =null;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField jTextField1;
private javax.swing.JToggleButton jToggleButton1;
private javax.swing.JToggleButton jToggleButton2;
public chengjitongji() {
initComponents();
}
private void connectdb() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=student_course_grade","sa", "123");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private void initComponents() {
jPanel2 = new javax.swing.JPanel();
jToggleButton1 = new javax.swing.JToggleButton();
jToggleButton2 = new javax.swing.JToggleButton();
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel2.setBackground(new java.awt.Color(120, 150, 190));
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "学生成绩管理系统", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("微软雅黑", 1, 24), new java.awt.Color(255, 255, 255))); // NOI18N
jPanel2.setAutoscrolls(true);
jToggleButton1.setText("退出");
jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton1ActionPerformed(evt);
}
});
jToggleButton2.setText("返回");
jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton2ActionPerformed(evt);
}
});
jButton1.setText("最高分");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable1);
jButton8.setText("刷新");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
jButton9.setText("平均分");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});
jButton10.setText("查询");
jButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton10ActionPerformed(evt);
}
});
jButton2.setText("最低分");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addGap(0, 699, Short.MAX_VALUE)
.addComponent(jToggleButton2)
.addGap(18, 18, 18)
.addComponent(jToggleButton1))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jScrollPane1)
.addContainerGap())
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton10)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton9)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton8)
.addGap(54, 54, 54))))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addGap(15, 15, 15)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton10)
.addComponent(jButton1)
.addComponent(jButton9)
.addComponent(jButton8)
.addComponent(jButton2))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jToggleButton1)
.addComponent(jToggleButton2))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(282, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(50, 50, 50)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(359, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String text=jTextField1.getText();
try {
connectdb();
String sql = "select top 1 grade.courseId,courseName,exameTime,cname,student.CNo,score from course,grade,student where courseName='"+text+"' and course.courseId=grade.courseId and student.CNo=grade.CNo order by score desc";
pstm = conn.prepareStatement(sql);
ResultSet rs = pstm.executeQuery();
int count = 0;
while (rs.next()) {
count++;
}
rs = pstm.executeQuery();
Object[][] info = new Object[count][6];
count = 0;
BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("F:\\最高分.txt"));
while (rs.next()) {
bufferedWriter.write("课程编号:"+Integer.valueOf(rs.getInt("courseId"))+"--"+"课程名字:"+rs.getString("courseName")+"--"+"考试时间:"+rs.getString("exameTime")+"--"+"学生姓名:"+rs.getString("cname")+"--"+"学号:"+rs.getString("CNo")+"--"+"最高分:"+Float.valueOf(rs.getFloat("score")));
bufferedWriter.newLine();
info[count][0] = Integer.valueOf(rs.getInt("courseId"));
info[count][1] = rs.getString("courseName");
info[count][2] = rs.getString("exameTime");
info[count][3] = rs.getString("cname");
info[count][4] = rs.getString("CNo");
info[count][5] = Float.valueOf(rs.getFloat("score"));
count++;
}
bufferedWriter.close();
String[] title = { "课程编号", "课程名称", "考试时间", "学生姓名", "学号", "分数"};
this.jTable1 = new JTable(info, title);
this.jScrollPane1.getViewport().add(jTable1);
conn.close();
} catch (SQLException | IOException e) {
e.printStackTrace();
}
finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
try {
connectdb();
String sql = "select * from grade";
pstm = conn.prepareStatement(sql);
ResultSet rs = pstm.executeQuery();
int count = 0;
while (rs.next()) {
count++;
}
rs = pstm.executeQuery();
Object[][] info = new Object[count][4];
count = 0;
while (rs.next()) {
info[count][0] = Float.valueOf(rs.getFloat("courseId"));
info[count][1] = rs.getString("CNo");
info[count][2] = rs.getString("exameTime");
info[count][3] = Float.valueOf(rs.getFloat("score"));
count++;
}
String[] title = { "课程编号", "学号", "考试时间","分数" };
this.jTable1 = new JTable(info, title);
this.jScrollPane1.getViewport().add(jTable1);
conn.close();
}catch (SQLException sqle) {
JOptionPane.showMessageDialog(null, "数据操作错误", "错误", JOptionPane.ERROR_MESSAGE);
}
}
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
String text=jTextField1.getText();
try {
connectdb();
String sql = "select grade.courseId,exameTime,AVG(score) as avgScore from course,grade where courseName='"+text+"' and course.courseId=grade.courseId group by grade.courseId,exameTime;";
pstm = conn.prepareStatement(sql);
ResultSet rs = pstm.executeQuery();
int count = 0;
while (rs.next()) {
count++;
}
rs = pstm.executeQuery();
Object[][] info = new Object[count][4];
count = 0;
BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("F:\\平均分.txt"));
while (rs.next()) {
bufferedWriter.write("课程编号:"+Integer.valueOf(rs.getInt("courseId"))+"--"+"考试时间:"+rs.getString("exameTime")+"--"+"平均分:"+Float.valueOf(rs.getFloat("avgScore")));
bufferedWriter.newLine();
info[count][0] = Integer.valueOf(rs.getInt("courseId"));
info[count][1] = rs.getString("exameTime");
info[count][2] = Float.valueOf(rs.getFloat("avgScore"));
count++;
}
bufferedWriter.close();
String[] title = { "课程编号", "考试时间", "平均分"};
this.jTable1 = new JTable(info, title);
this.jScrollPane1.getViewport().add(jTable1);
conn.close();
} catch (SQLException | IOException e) {
e.printStackTrace();
}
finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
String text=jTextField1.getText();
try {
connectdb();
String sql = "select grade.courseId,courseName,exameTime,cname,student.CNo,score from course,grade,student where courseName='"+text+"' and course.courseId=grade.courseId and student.CNo=grade.CNo order by score desc";
pstm = conn.prepareStatement(sql);
ResultSet rs = pstm.executeQuery();
int count = 0;
while (rs.next()) {
count++;
}
rs = pstm.executeQuery();
Object[][] info = new Object[count][6];
count = 0;
while (rs.next()) {
info[count][0] = Integer.valueOf(rs.getInt("courseId"));
info[count][1] = rs.getString("courseName");
info[count][2] = rs.getString("exameTime");
info[count][3] = rs.getString("cname");
info[count][4] = rs.getString("CNo");
info[count][5] = Float.valueOf(rs.getFloat("score"));
count++;
}
String[] title = { "课程编号", "课程名称", "考试时间", "学生姓名", "学号", "分数"};
this.jTable1 = new JTable(info, title);
this.jScrollPane1.getViewport().add(jTable1);
conn.close();
}catch (SQLException sqle) {
JOptionPane.showMessageDialog(null, "数据操作错误", "错误", JOptionPane.ERROR_MESSAGE);
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String text=jTextField1.getText();
try {
connectdb();
String sql = "select top 1 grade.courseId,courseName,exameTime,cname,student.CNo,score from course,grade,student where courseName='"+text+"' and course.courseId=grade.courseId and student.CNo=grade.CNo order by score";
pstm = conn.prepareStatement(sql);
ResultSet rs = pstm.executeQuery();
int count = 0;
while (rs.next()) {
count++;
}
rs = pstm.executeQuery();
Object[][] info = new Object[count][6];
count = 0;
BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("F:\\最低分.txt"));
while (rs.next()) {
bufferedWriter.write("课程编号:"+Integer.valueOf(rs.getInt("courseId"))+"--"+"课程名字:"+rs.getString("courseName")+"--"+"考试时间:"+rs.getString("exameTime")+"--"+"学生姓名:"+rs.getString("cname")+"--"+"学号:"+rs.getString("CNo")+"--"+"最低分:"+Float.valueOf(rs.getFloat("score")));
bufferedWriter.newLine();
bufferedWriter.close();
info[count][0] = Integer.valueOf(rs.getInt("courseId"));
info[count][1] = rs.getString("courseName");
info[count][2] = rs.getString("exameTime");
info[count][3] = rs.getString("cname");
info[count][4] = rs.getString("CNo");
info[count][5] = Float.valueOf(rs.getFloat("score"));
count++;
}
String[] title = { "课程编号", "课程名称", "考试时间", "学生姓名", "学号", "分数"};
this.jTable1 = new JTable(info, title);
this.jScrollPane1.getViewport().add(jTable1);
conn.close();
} catch (SQLException | IOException e) {
e.printStackTrace();
}
finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
private void jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt) {
chengjitongji.this.setVisible(false);
guanli Guanli=new guanli();
Guanli.setVisible(true);
}
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(chengjitongji.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(chengjitongji.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(chengjitongji.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(chengjitongji.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new chengjitongji().setVisible(true);
}
});
}
3、学生管理功能
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=student_course_grade";
String username = "sa";
String password = "123";
Connection conn = null;
Statement stmt = null;
PreparedStatement pstm =null;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField jTextField1;
private javax.swing.JToggleButton jToggleButton1;
private javax.swing.JToggleButton jToggleButton2;
public xueshengguanli() {
initComponents();
}
private void connectdb() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException cnfe) {
JOptionPane.showMessageDialog(null, "数据源错误", "错误", JOptionPane.ERROR_MESSAGE);
} catch (SQLException sqle) {
JOptionPane.showMessageDialog(null, "数据操作错误", "错误", JOptionPane.ERROR_MESSAGE);
}
}
private void initComponents() {
jPanel2 = new javax.swing.JPanel();
jToggleButton1 = new javax.swing.JToggleButton();
jToggleButton2 = new javax.swing.JToggleButton();
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();
jButton10 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel2.setBackground(new java.awt.Color(120, 150, 190));
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "学生成绩管理系统", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("微软雅黑", 1, 24), new java.awt.Color(255, 255, 255))); // NOI18N
jPanel2.setAutoscrolls(true);
jToggleButton1.setText("退出");
jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton1ActionPerformed(evt);
}
});
jToggleButton2.setText("返回");
jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton2ActionPerformed(evt);
}
});
jButton1.setText("修改");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable1);
jButton8.setText("刷新");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton8ActionPerformed(evt);
}
});
jButton9.setText("删除");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton9ActionPerformed(evt);
}
});
jButton10.setText("查询");
jButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton10ActionPerformed(evt);
}
});
jButton2.setText("增加");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addGap(0, 699, Short.MAX_VALUE)
.addComponent(jToggleButton2)
.addGap(18, 18, 18)
.addComponent(jToggleButton1))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jScrollPane1)
.addContainerGap())
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton10)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton9)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton8)
.addGap(54, 54, 54))))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addGap(15, 15, 15)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton10)
.addComponent(jButton1)
.addComponent(jButton9)
.addComponent(jButton8)
.addComponent(jButton2))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jToggleButton1)
.addComponent(jToggleButton2))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(282, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(50, 50, 50)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(359, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String No=JOptionPane.showInputDialog("请输入你要修改的编号:");
if (No.equals("")){
JOptionPane.showMessageDialog(null, "你输入了空值!!!");
return;
}
String Name=JOptionPane.showInputDialog("请输入新的姓名:");
if (Name.equals("")){
JOptionPane.showMessageDialog(null, "你输入了空值!!!");
return;
}
String price=JOptionPane.showInputDialog("请输入新的地址:");
if (price.equals("")){
JOptionPane.showMessageDialog(null, "你输入了空值!!!");
return;
}
String quality=JOptionPane.showInputDialog("请输入新的电话号码:");
if (quality.equals("")){
JOptionPane.showMessageDialog(null, "你输入了空值!!!");
return;
}
String nquality=JOptionPane.showInputDialog("请输入新的密码:");
if (nquality.equals("")){
JOptionPane.showMessageDialog(null, "你输入了空值!!!");
return;
}
try {
connectdb();
String sql = "update student set cname='"+Name+"',cad='"+price+"',cph='"+quality+"',cpassword='"+nquality+"' where CNo='"+No+"'";
stmt = conn.createStatement();
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "修改成功!!!");
conn.close();
}catch (SQLException e) {
e.printStackTrace();
}
finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String No=JOptionPane.showInputDialog("请输入学号:");
if (No.equals("")){
JOptionPane.showMessageDialog(null, "你输入了空值!!!");
return;
}
String Name=JOptionPane.showInputDialog("请输入名字:");
if (Name.equals("")){
JOptionPane.showMessageDialog(null, "你输入了空值!!!");
return;
}
String price=JOptionPane.showInputDialog("请输入地址:");
if (price.equals("")){
JOptionPane.showMessageDialog(null, "你输入了空值!!!");
return;
}
String per=JOptionPane.showInputDialog("请输入性别:");
if (per.equals("")){
JOptionPane.showMessageDialog(null, "你输入了空值!!!");
return;
}
String quality=JOptionPane.showInputDialog("请输入电话:");
if (quality.equals("")){
JOptionPane.showMessageDialog(null, "你输入了空值!!!");
return;
}
Integer randPassword=(int)(Math.random()*(999999)+1);
String stuPassword=String.format("%06d",randPassword);
try {
connectdb();
String sql = "insert into student(CNo,cname,cad,csex,cph,cpassword)"+ "values('"+No+"','"+Name+"','"+price+"','"+per+"','"+quality+"','"+stuPassword+"')";
stmt = conn.createStatement();
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "添加成功,初始密码为:"+stuPassword+"请牢记!!!");
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
try {
connectdb();
String sql = "select * from student";
pstm = conn.prepareStatement(sql);
ResultSet rs = pstm.executeQuery();
int count = 0;
while (rs.next()) {
count++;
}
rs = pstm.executeQuery();
Object[][] info = new Object[count][5];
count = 0;
while (rs.next()) {
info[count][0] = rs.getString("CNo");
info[count][1] = rs.getString("cname");
info[count][2] = rs.getString("cad");
info[count][3] = rs.getString("csex");
info[count][4] = rs.getString("cph");
count++;
}
String[] title = { "学生编号", "学生名称", "地址","性别","电话" };
this.jTable1 = new JTable(info, title);
this.jScrollPane1.getViewport().add(jTable1);
conn.close();
}catch (SQLException sqle) {
JOptionPane.showMessageDialog(null, "数据操作错误", "错误", JOptionPane.ERROR_MESSAGE);
}
}
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
String CNo=JOptionPane.showInputDialog("请输入你要删除的编号:");
if (CNo.equals("")){
JOptionPane.showMessageDialog(null, "你输入了空值!!!");
return;
}
try {
connectdb();
String sql = "delete from student where CNo='"+CNo+"';";
stmt = conn.createStatement();
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "删除成功!!!");
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
String text=jTextField1.getText();
try {
connectdb();
String sql = "select * from student where cname like '%"+text+"%' or CNo like '%\"+text+\"%'";
pstm = conn.prepareStatement(sql);
ResultSet rs = pstm.executeQuery();
int count = 0;
while (rs.next()) {
count++;
}
rs = pstm.executeQuery();
Object[][] info = new Object[count][8];
count = 0;
while (rs.next()) {
info[count][0] = rs.getString("CNo");
info[count][1] = rs.getString("cname");
info[count][2] = rs.getString("cad");
info[count][3] = rs.getString("csex");
info[count][4] = rs.getString("cph");
count++;
}
String[] title = { "学生编号", "学生名字", "学生地址","性别","电话号码" };
this.jTable1 = new JTable(info, title);
this.jScrollPane1.getViewport().add(jTable1);
conn.close();
}catch (SQLException sqle) {
JOptionPane.showMessageDialog(null, "数据操作错误", "错误", JOptionPane.ERROR_MESSAGE);
}
}
private void jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt) {
xueshengguanli.this.setVisible(false);
guanli Guanli=new guanli();
Guanli.setVisible(true);
}
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(xueshengguanli.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(xueshengguanli.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(xueshengguanli.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(xueshengguanli.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new xueshengguanli().setVisible(true);
}
});
}
因篇幅有限,只展示此三个主要模块的代码。以上三个模块的代码基本覆盖了整个系统代码的实现方法,其他模块的基本按照上述代码写法复制粘贴即可,如果有需要完整源代码的可以私我或者自行下载:https://download.csdn.net/download/weixin_46671666/73817543










