?? studentuniversquerydialog.java
字號:
package com.hb.stumanagesys.universquery;
import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import com.hb.jdbcconnct.JdbcConnct;
//import com.jgoodies.forms.layout.FormLayout;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class StudentUniversQueryDialog extends javax.swing.JDialog {
{
//Set Look & Feel
try {
javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getCrossPlatformLookAndFeelClassName());
} catch(Exception e) {
e.printStackTrace();
}
}
private JPanel studentUniversPanel1;//定義學生綜合管理面板
private JLabel sUMLabel1;
private ButtonGroup sUMbuttonGroup1;
private JButton sUMButton1;//定義 查詢 button
private JButton quitButton;//定義退出button
private JCheckBox misCheckBox;//定義模糊查詢的combox
private JComboBox conditionComboBox;//定義查詢條件的下拉框
private JTable sUMTable1;//顯示數據的表格
private JScrollPane sUMScrollPane1;
private JTextField sUMTextField1;//查詢條件輸入的文本框
DefaultTableModel sUMTable1Model; //表的model
private JTabbedPane stuUniversQueryTabbedPane;
private JPanel stuQureyPanel;
/**
* 程序的入口方法
*/
public static void main(String[] args) {
//新建一個JFrame 用來構造對話框
JFrame frame = new JFrame();
StudentUniversQueryDialog inst = new StudentUniversQueryDialog(frame);
inst.setVisible(true);//設置文本框可見
}
public StudentUniversQueryDialog(JFrame frame) {
super(frame);
initGUI();
}
private void initGUI() {
try {
{
this.setTitle("\u5b66\u751f\u7efc\u5408\u67e5\u8be2");
{
{
sUMbuttonGroup1 = new ButtonGroup();
}
studentUniversPanel1 = new JPanel();
getContentPane().add(studentUniversPanel1, BorderLayout.CENTER);
//studentUniversPanel1.setLayout(studentUniversPanelLayout);
studentUniversPanel1.setBackground(new java.awt.Color(
185,
185,
255));
studentUniversPanel1.setLayout(null);
{
stuUniversQueryTabbedPane = new JTabbedPane();
studentUniversPanel1.add(stuUniversQueryTabbedPane);
stuUniversQueryTabbedPane.setBounds(21, 0, 546, 427);
{
stuQureyPanel = new JPanel();
stuUniversQueryTabbedPane.addTab("學生查詢", null, stuQureyPanel, null);
stuQureyPanel.setPreferredSize(new java.awt.Dimension(541, 401));
stuQureyPanel.setLayout(null);
{
sUMLabel1 = new JLabel();
stuQureyPanel.add(sUMLabel1);
sUMLabel1.setText("\u6309\uff1a");
sUMLabel1.setBounds(21, 28, 35, 28);
}
{
sUMScrollPane1 = new JScrollPane();
stuQureyPanel.add(sUMScrollPane1);
sUMScrollPane1.setBounds(21, 77, 497, 273);
{
sUMTable1Model = new DefaultTableModel(
new String[][] {},
new String[] { "學號", "姓名", "性別", "年齡",
"出生日期", "政治面貌", "聯系方式", "籍貫",
"入學時間", "狀態", "班級名稱", "專業名稱",
"教師名稱" }) {
public boolean isCellEditable(
int row,
int column) {
return false;
}
};
sUMTable1 = new JTable();
sUMScrollPane1.setViewportView(sUMTable1);
BoxLayout sUMTable1Layout = new BoxLayout(
sUMTable1,
javax.swing.BoxLayout.Y_AXIS);
sUMTable1.setLayout(sUMTable1Layout);
sUMTable1.setModel(sUMTable1Model);
sUMTable1.getTableHeader()
.setPreferredSize(
new java.awt.Dimension(505, 14));
BorderLayout tableHeaderLayout = new BorderLayout();
sUMTable1.getTableHeader().setLayout(
tableHeaderLayout);
sUMTable1.setBounds(32, 78, 501, 195);
sUMTable1.setDoubleBuffered(true);
//sUMTable1.setCellSelectionEnabled(true);
}
}
{
ComboBoxModel conditionComboBoxModel = new DefaultComboBoxModel(
new String[] { "全部", "學號", "姓名", "年齡" });
conditionComboBox = new JComboBox();
stuQureyPanel.add(conditionComboBox);
conditionComboBox
.setModel(conditionComboBoxModel);
conditionComboBox.setBounds(77, 28, 98, 28);
conditionComboBox
.addActionListener(new ActionListener() {
public void actionPerformed(
ActionEvent evt) {
conditionComboBoxActionPerformed(evt);
}
});
}
{
sUMTextField1 = new JTextField();
stuQureyPanel.add(sUMTextField1);
sUMTextField1.setBounds(196, 28, 98, 28);
sUMTextField1.setEditable(false);
}
{
misCheckBox = new JCheckBox();
stuQureyPanel.add(misCheckBox);
misCheckBox.setText("\u6a21\u7cca\u67e5\u8be2");
misCheckBox.setBounds(315, 35, 70, 21);
misCheckBox.setEnabled(false);
}
{
sUMButton1 = new JButton();
stuQureyPanel.add(sUMButton1);
sUMButton1.setText("\u67e5\u8be2");
sUMButton1.setBounds(406, 35, 70, 21);
sUMButton1
.addActionListener(new ActionListener() {
public void actionPerformed(
ActionEvent evt) {
sUMButton1ActionPerformed(evt);
}
});
}
{
quitButton = new JButton();
stuQureyPanel.add(quitButton);
quitButton.setText("\u9000\u51fa");
quitButton.setBounds(441, 364, 70, 21);
quitButton
.addActionListener(new ActionListener() {
public void actionPerformed(
ActionEvent evt) {
quitButtonActionPerformed(evt);
}
});
}
}
}
}
}
this.setSize(600,475);
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 綜合查詢
*/
public void initialStudentTable(String sql1)
{
//實例化一個 vector
Vector vecData = new Vector();
int row = 0;
try
{
//創建并獲得一個連接
Connection conn = JdbcConnct.fetchConnection();
//創建statement對象
Statement stmt = conn.createStatement();
//查詢的sql語句
String sql = "select studentid,stuname,gender,age,birthday," +
"polistatus,contactway,native,entertime,status,classname,majorname,teachername from stu_view"+sql1;
//獲取結果集
ResultSet rs = stmt.executeQuery(sql);
//對結果集處理。
while(rs.next()){
String polis = rs.getString("polistatus");
//將選擇的政治面貌從數據庫的數字對應出文字顯示
if (polis == null) {
polis = "無";
} else if (polis.equals("1")) {
polis = "共青團員";
} else if (polis.equals("2")) {
polis = "黨員";
} else if (polis.equals("3")) {
polis = "群眾";
} else {
polis = "其他黨派";
}
//將選擇的學生狀態從數據庫的數字對應出文字顯示
String status = rs.getString("status");
// System.out.println(polis);
if (status == null) {
status = "無";
} else if (status.equals("1")) {
status = "在讀";
} else if (status.equals("2")) {
status = "退學";
} else if (status.equals("3")) {
status = "休學";
} else if (status.equals("4")) {
status = "畢業";
} else {
status = "其它";
}
//將選擇的性別從數據庫中的字母對應出文字顯示
String gender = rs.getString("gender");
System.out.println(gender);
//得到性別
if(null==gender)
gender = "null";
if(gender.equals("M"))
gender = "男";
else if(gender.equals("W"))
gender = "女";
//將信息加入向量中
vecData.addElement((String)rs.getString("studentid"));
vecData.addElement((String)rs.getString("stuname"));
vecData.addElement(gender);
vecData.addElement((String)rs.getString("age"));
vecData.addElement((String)rs.getString("birthday"));
vecData.addElement(polis);
vecData.addElement((String)rs.getString("contactway"));
vecData.addElement((String)rs.getString("native"));
vecData.addElement((String)rs.getString("entertime"));
vecData.addElement(status);
vecData.addElement((String)rs.getString("classname"));
vecData.addElement((String)rs.getString("majorname"));
vecData.addElement((String)rs.getString("teachername"));
}
//獲取數據的行數
row = vecData.size()/13;
//判斷是否存在
if(row!=0){
String [] title=new String[]{ "學號", "姓名", "性別", "年齡", "出生日期",
"政治面貌", "聯系方式", "籍貫", "入學時間", "狀態" , "班級名稱" , "專業名稱" , "教師名稱" };
String[][] aa = new String [row][13] ;
//把數據存放到數組中
for(int i=0;i<row;i++)
{
for(int k=0;k<13;k++)
{
aa[i][k] = (String)vecData.elementAt(i*13+k);
}
}
//定義dataModel把數據存放到表格中,并設置表格不可編輯
DefaultTableModel dataModel = new DefaultTableModel(aa,title){
//設置table不可編輯
public boolean isCellEditable(int row,int column){
return false;
}
};
sUMTable1.setModel(dataModel);
sUMTable1.setRowHeight(20);
//System.out.println(dataModel);
// System.out.println( commentTable.getModel());
}
else //如果沒有數據,提示數據不存在。
{
sUMTable1.setModel(sUMTable1Model);
sUMTable1.setRowHeight(20);
JOptionPane.showMessageDialog(this, "對不起,您查找的數據不存在!!");
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "查詢出錯");
//e.printStackTrace();
}
}
/*
* 查詢模塊
*/
private void sUMButton1ActionPerformed(ActionEvent evt) {
//判斷是否模糊查詢
boolean flag = this.misCheckBox.isSelected();
//獲取選擇的查詢條件
String choice = (String) this.conditionComboBox.getSelectedItem();
//獲取輸入的查詢條件
String input = this.sUMTextField1.getText().trim();
if(flag == false)
{
//判斷選擇是否為全部
if(choice.equals("全部"))
{
//查詢全部的sql語句
String sql = " order by studentid asc";
//查詢數據,并不查詢的數據在表格中顯示
initialStudentTable(sql);
}
//判斷選擇是否為學號
else if(choice.equals("學號"))
{
//按全部查詢的sql語句
String sql = " where studentid ="+input;
//查詢數據,并不查詢的數據在表格中顯示
initialStudentTable(sql);
}
//判斷選擇是否為姓名
else if(choice.equals("姓名"))
{
//按姓名查詢的sql語句
String sql = " where stuname '"+input+"'";
////查詢數據,并不查詢的數據在表格中顯示
initialStudentTable(sql);
}
//判斷選擇是否為年齡
else if(choice.equals("年齡"))
{
//按照年齡查詢全部的sql語句
String sql = " where age ="+input;
////查詢數據,并不查詢的數據在表格中顯示
initialStudentTable(sql);
}
}
else //如果是模糊查詢
{
if(choice.equals("學號"))
{
//按照學號模糊查詢的sql語句
String sql = " where studentid like '%"+input+"%'";
////查詢數據,并不查詢的數據在表格中顯示
initialStudentTable(sql);
}
else if(choice.equals("姓名"))
{
//按照姓名模糊查詢的sql語句
String sql = " where stuname like '%"+input+"%'";
////查詢數據,并不查詢的數據在表格中顯示
initialStudentTable(sql);
}
else if(choice.equals("年齡"))
{
//按照年齡模糊查詢的sql語句
String sql = " where age like'%"+input+"%'";
////查詢數據,并不查詢的數據在表格中顯示
initialStudentTable(sql);
}
}
}
private void conditionComboBoxActionPerformed(ActionEvent evt) {
//定義choice從conditionComboBox接收數據
String choice = (String) this.conditionComboBox.getSelectedItem();
//當選擇全部時 sUMTextField1 不可用
if(choice.endsWith("全部"))
{
this.misCheckBox.setSelected(false);
this.sUMTextField1.setEnabled(false);
this.misCheckBox.setEnabled(false);
}
//當choice為其它時 可以填入相應的信息
else
{
this.sUMTextField1.setEnabled(true);
this.sUMTextField1.setEditable(true);
this.misCheckBox.setEnabled(true);
}
}
/*
* 退出按鈕
*/
private void quitButtonActionPerformed(ActionEvent evt)
{
this.dispose();//退出
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -