📄 stuclass.java
字号:
/*
* Stuclass.java
*
* Created on 2007年11月17日, 上午11:25
*/
package javaapplication1;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
/**
*
* @author mis05
*/
public class Stuclass extends javax.swing.JPanel {
/** Creates new form Stuclass */
public Stuclass() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jButton1.setFont(new java.awt.Font("宋体", 0, 14));
jButton1.setText("\u6dfb\u52a0");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addclass(evt);
}
});
jLabel1.setFont(new java.awt.Font("宋体", 1, 18));
jLabel1.setText("\u8bfe\u7a0b\u7f16\u53f7");
jLabel2.setFont(new java.awt.Font("宋体", 1, 18));
jLabel2.setText("\u5b66\u751f\u7f16\u53f7");
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(63, 63, 63)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(jLabel2)
.add(jLabel1))
.add(30, 30, 30)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jTextField2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 93, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(138, 138, 138))
.add(layout.createSequentialGroup()
.add(121, 121, 121)
.add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 66, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(213, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(69, 69, 69)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel2)
.add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 28, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(33, 33, 33)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 28, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jLabel1))
.add(32, 32, 32)
.add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 31, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(78, Short.MAX_VALUE))
);
}// </editor-fold>//GEN-END:initComponents
private void addclass(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addclass
Statement sql;
try{
Conect conect = new Conect();
sql = conect.dbConn.createStatement();
String string=null;
String string1=null;
int temp = 0;
String number = jTextField2.getText();
String classNo = jTextField1.getText();
if( number.equals("") )
{
JOptionPane.showMessageDialog(null,"清输入选课学生编号","错误",JOptionPane.ERROR_MESSAGE);
}
if( classNo.equals("") )
{
JOptionPane.showMessageDialog(null,"清输入选课课程号","错误",JOptionPane.ERROR_MESSAGE);
}
else
{
ResultSet rs = sql.executeQuery("select 学生编号 from 学生 where 学生编号 = '"+ number +"'");
if( rs.next() ) temp ++;
else JOptionPane.showMessageDialog(null,"不存在此学生","错误",JOptionPane.ERROR_MESSAGE);
rs = sql.executeQuery("select 课程编号 from 课程 where 课程编号 = '"+ classNo +"'");
if( rs.next() ) temp ++;
else JOptionPane.showMessageDialog(null,"不存在此课程","错误",JOptionPane.ERROR_MESSAGE);
rs = sql.executeQuery("select 学生编号,课程编号 from 选课 where 课程编号 = '"+ classNo +"' and 学生编号 = '"+ number +"'");
if( rs.next() ) JOptionPane.showMessageDialog(null,"你已经选择了此课程","错误",JOptionPane.ERROR_MESSAGE);
else temp ++;
StringBuffer buffer1 = new StringBuffer();
StringBuffer buffer2 = new StringBuffer();
String peopleCount = null;
String peopleMax = null;
rs = sql.executeQuery("select 已选人数 from 课程 where 课程编号 = '"+classNo+"'");
rs.next();
buffer1.append( rs.getObject(1) );
peopleCount = buffer1.toString();
rs = sql.executeQuery("select 容量 from 课程 where 课程编号 ='"+classNo+"'");
rs.next();
buffer2.append( rs.getObject(1) );
peopleMax = buffer2.toString();
if( peopleMax.equals( peopleCount ) )
{
JOptionPane.showMessageDialog(null,"此课程人数已满","失败",JOptionPane.INFORMATION_MESSAGE);
}
else
{
temp ++;
}
if( temp == 4 )
{
rs = sql.executeQuery("select 课程名称 from 课程 where 课程编号='"+number+"'");
while( rs.next() )
{
string1 = (String) rs.getObject(1);
}
int reg;
reg = sql.executeUpdate("insert into 选课 (学生编号,课程编号,课程名称) values('"+number+"','"+classNo+"','"+string1+"')");
reg = sql.executeUpdate("UPDATE 课程 SET 已选人数 = 已选人数+1 WHERE 课程编号 = '"+classNo+"'");
JOptionPane.showMessageDialog(null,"选课成功","成功",JOptionPane.INFORMATION_MESSAGE);
}
}
}
catch(SQLException sqlException){
JOptionPane.showMessageDialog(null,sqlException.getMessage(),"Database Error",JOptionPane.ERROR_MESSAGE);
System.exit(1);}
catch(ClassNotFoundException classNotFound){
JOptionPane.showMessageDialog(null,classNotFound.getMessage(),"Driver Not Found",JOptionPane.ERROR_MESSAGE);
System.exit(1); }// TODO add your handling code here:
}//GEN-LAST:event_addclass
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration//GEN-END:variables
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -