/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* course.java
*
* Created on Nov 25, 2010, 10:58:36 AM
*/
/**
*
* @author jalando-on-ke
*/
import java.sql.*;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
public class course extends javax.swing.JFrame {
String sql1="";
String sql2="";
ResultSet rs1,rs2;
/** Creates new form course */
public course() {
initComponents();
loadCourse();
}
/** 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.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
cmbCourse = new javax.swing.JComboBox();
jLabel1 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
tblStudent = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
cmbCourse.setMaximumRowCount(4);
cmbCourse.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
cmbCourseKeyPressed(evt);
}
public void keyReleased(java.awt.event.KeyEvent evt) {
cmbCourseKeyReleased(evt);
}
});
jLabel1.setText("Course");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(58, 58, 58)
.addComponent(jLabel1)
.addGap(54, 54, 54)
.addComponent(cmbCourse, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(152, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(29, 29, 29)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(cmbCourse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(51, Short.MAX_VALUE))
);
getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);
tblStudent.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Student_id", "Name", "Year", "Gender"
}
));
jScrollPane1.setViewportView(tblStudent);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);
pack();
}// private void cmbCourseKeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
display(cmbCourse.getSelectedItem().toString());
}
private void cmbCourseKeyReleased(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
display(cmbCourse.getSelectedItem().toString());
}
/**
* @param args the command line arguments
*/
public void loadCourse(){
try{
SetConnection conn=new SetConnection();
Connection connect =SetConnection.conn;
Statement stm=connect.createStatement();
sql1="Select distinct course_no from course order by course_no";
rs1=stm.executeQuery(sql1);
cmbCourse.removeAllItems();
while(rs1.next()){
cmbCourse.addItem(rs1.getString(1).trim());
}
connect.close();
}catch(SQLException sqlException){}
}
public void display(String course){
int count=0;
try{
SetConnection conn=new SetConnection();
Connection connect =SetConnection.conn;
Statement stm=connect.createStatement();
sql1="Select count(*) from student where course_no ='"+course+"' order by name";
sql2="Select * from student where course_no ='"+course+"' order by name";
rs1=stm.executeQuery(sql1);
if(rs1.next()){
count=Integer.parseInt(rs1.getString(1));
}
int rowcount=0;
if(count>0){
DefaultTableModel ct =(DefaultTableModel)
tblStudent.getModel();
rs2=stm.executeQuery(sql2);
while(rs2.next()){
Vector v=new Vector();
v.add(rs2.getString(1).toString());
v.add(rs2.getString(2).toString());
// v.add(rs2.getString(3).toString());
v.add(rs2.getString(4).toString());
v.add(rs2.getString(5).toString());
ct.setRowCount(rowcount);
ct.addRow(v);
rowcount++;
}
}
else{
DefaultTableModel t = (DefaultTableModel)
tblStudent.getModel();
t.setRowCount(0);
}
connect.close();
}catch(SQLException sqlException){}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new course().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox cmbCourse;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tblStudent;
// End of variables declaration
}
...and that concludes it...