?? 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 + -