📄 search.java
字号:
package com.hb.stumanagesys;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
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.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
/**
* 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 SteppedComboBoxExample extends JFrame {
// public SteppedComboBoxExample() {
// super("SteppedComboBox Example");
//
// String[] str = {
// "A",
// "abcdefghijklmnopqrstuvwxyz",
// "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
// "0123456789"
// };
//
// Component combo = new SteppedComboBox();
// Dimension d = combo.getPreferredSize();
// combo.setPreferredSize(new Dimension(50, d.height));
// combo.setPopupWidth(d.width);
//
// getContentPane().setLayout(new FlowLayout());
// getContentPane().add(combo);
// }
// public static void main (String args[]) {
// SteppedComboBoxExample f = new SteppedComboBoxExample();
// f.addWindowListener(new WindowAdapter() {
// public void windowClosing(WindowEvent e) {
// System.exit(0);
// }
// });
// f.setSize (300, 100);
// f.show();
// }
// }
public class search extends javax.swing.JFrame {
private JPanel jPanel;
private JComboBox ComboBox;
private JTextField jTextField;
private JCheckBox jCheckBox;
private JComboBox twoComboBox;
private JScrollPane jjScrollPane;
private JTextField jjTextField;
private ButtonGroup buttonGroup1;
private JLabel jjgLabel;
private JTable jTable1;
private JButton clearButton;
private JButton addButton;
private JButton sureButton;
private JScrollPane jScrollPane;
private JList jList1;
private JRadioButton orRadioButton;
private JRadioButton andRadioButton;
private JRadioButton nullRadioButton;
private JLabel jgLabel;
private static Connection conn = null;
private static ResultSet rset = null;
private static PreparedStatement pstmt = null;
private static PreparedStatement pstmtselect = null;
private static Statement stmt = null;
private String[][] row;
private String[] column = { "xkids", "xkh", "xkm", "xf" };
private String[] column1 = { "", "", "", "" };
private char w;
public Vector ar = null;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
search inst = new search();
inst.setVisible(true);
conn = search.fetchConnection();
}
public search() {
super();
initGUI();
this.ar = new Vector(60);
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
{
buttonGroup1 = new ButtonGroup();
}
jPanel = new JPanel();
getContentPane().add(jPanel, BorderLayout.CENTER);
// jPanel.setLayout(jPanelLayout);
jPanel.setPreferredSize(new java.awt.Dimension(518, 273));
jPanel.setFont(new java.awt.Font("宋体", 0, 12));
jPanel.setLayout(null);
{
ComboBoxModel ComboBoxModel = new DefaultComboBoxModel(
new String[] { "xkh", "xkm", "xf" });
ComboBox = new JComboBox();
jPanel.add(ComboBox);
ComboBox.setModel(ComboBoxModel);
ComboBox.setFont(new java.awt.Font("宋体", 0, 12));
ComboBox.setBounds(21, 21, 77, 35);
ComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
ComboBoxActionPerformed(evt);
}
});
}
{
jCheckBox = new JCheckBox();
jPanel.add(jCheckBox);
jCheckBox.setText("\u8303\u56f4:");
jCheckBox.setBounds(14, 77, 63, 28);
jCheckBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jCheckBoxActionPerformed(evt);
}
});
}
{
ComboBoxModel twoComboBoxModel = new DefaultComboBoxModel(
new String[] { ">", "<", "=" });
twoComboBox = new JComboBox();
jPanel.add(twoComboBox);
twoComboBox.setModel(twoComboBoxModel);
twoComboBox.setBounds(133, 21, 98, 35);
// twoComboBox.addActionListener(new ActionListener() {
}
{
jjScrollPane = new JScrollPane();
jPanel.add(jjScrollPane);
jjScrollPane.setBounds(357, 91, 168, 203);
{
// row = new String[5][4];
TableModel jTable1Model = new DefaultTableModel(row,
column);
jTable1 = new JTable();
jjScrollPane.setViewportView(jTable1);
jTable1.setModel(jTable1Model);
jTable1.setPreferredSize(new java.awt.Dimension(231,
266));
}
}
{
jgLabel = new JLabel();
jPanel.add(jgLabel);
jgLabel.setText("\u81f3");
jgLabel.setFont(new java.awt.Font("宋体", 0, 12));
jgLabel.setBounds(112, 112, 42, 35);
}
{
nullRadioButton = new JRadioButton();
jPanel.add(nullRadioButton);
nullRadioButton.setText("none");
buttonGroup1.add(nullRadioButton);
nullRadioButton.setSelected(true);
nullRadioButton.setBounds(7, 161, 56, 28);
}
{
andRadioButton = new JRadioButton();
jPanel.add(andRadioButton);
andRadioButton.setText("and");
buttonGroup1.add(andRadioButton);
andRadioButton.setBounds(63, 161, 63, 28);
}
{
orRadioButton = new JRadioButton();
jPanel.add(orRadioButton);
orRadioButton.setText("or");
buttonGroup1.add(orRadioButton);
orRadioButton.setBounds(133, 161, 63, 28);
}
{
jScrollPane = new JScrollPane();
jPanel.add(jScrollPane);
jScrollPane.setBounds(14, 210, 245, 98);
{
ListModel jList1Model = new DefaultComboBoxModel(
column1);
jList1 = new JList();
jScrollPane.setViewportView(jList1);
jList1.setModel(jList1Model);
jList1.setBounds(105, 91, 245, 98);
}
}
{
sureButton = new JButton();
jPanel.add(sureButton);
sureButton.setText("\u786e\u5b9a");
sureButton.setFont(new java.awt.Font("宋体", 0, 12));
sureButton.setBounds(14, 322, 63, 28);
sureButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
sureButtonActionPerformed(evt);
}
});
}
{
addButton = new JButton();
jPanel.add(addButton);
addButton.setText("\u6dfb\u52a0");
addButton.setFont(new java.awt.Font("宋体", 0, 12));
addButton.setBounds(182, 322, 63, 28);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
addButtonActionPerformed(evt);
}
});
}
{
clearButton = new JButton();
jPanel.add(clearButton);
clearButton.setText("\u6e05\u7a7a");
clearButton
.setPreferredSize(new java.awt.Dimension(63, 28));
clearButton.setEnabled(false);
clearButton.setFont(new java.awt.Font("宋体", 0, 12));
clearButton.setBounds(106, 323, 63, 28);
clearButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
clearButtonActionPerformed(evt);
}
});
}
{
jjgLabel = new JLabel();
jPanel.add(jjgLabel);
jjgLabel.setText("\u67e5\u8be2\u7ed3\u679c\u660e\u7ec6:");
jjgLabel.setBounds(357, 28, 91, 28);
}
{
jTextField = new JTextField();
jPanel.add(jTextField);
jTextField.setBounds(14, 112, 77, 35);
}
{
jjTextField = new JTextField();
jPanel.add(jjTextField);
jjTextField.setEnabled(false);
jjTextField.setBounds(147, 112, 98, 35);
}
}
pack();
this.setSize(557, 383);
} catch (Exception e) {
e.printStackTrace();
}
}
private static Connection fetchConnection() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String user = "xuyun";
String password = "123";
String url = "jdbc:oracle:thin:@192.168.111.12:1521:orcl";
try {
return DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
System.out.println("连接数据库失败");
}
} catch (ClassNotFoundException e) {
System.out.println("加载驱动器时失败");
}
return null;
}
private void addButtonActionPerformed(ActionEvent evt) {
//取得用户选择的字段 学科名,学科号
String a = this.ComboBox.getSelectedItem().toString();
String logicstr = "";
//取得用户选择的匹配符 none,and,or
if (this.nullRadioButton.isSelected())
logicstr = this.nullRadioButton.getText();
else if (this.andRadioButton.isSelected())
logicstr = this.andRadioButton.getText();
else if (this.orRadioButton.isSelected())
logicstr = this.orRadioButton.getText();
//获取查询范围
if (jCheckBox.isSelected()) {
jjTextField.setEnabled(true);
String s1 = this.jTextField.getText();
String s2 = this.jjTextField.getText();
if (a.equals("xkh")) {
try {
int m = Integer.parseInt(jTextField.getText());
int n = Integer.parseInt(jjTextField.getText());
if (m == n) {
JOptionPane.showMessageDialog(this, "输入不能重复");
return;
} else if (m > n) {
JOptionPane.showMessageDialog(this, "输入范围不正确");
return;
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this, "学科号不能为字符串或空");
return;
}
}
if (a.equals("xf")) {
try {
int m = Integer.parseInt(jTextField.getText());
int n = Integer.parseInt(jjTextField.getText());
if (m == n) {
JOptionPane.showMessageDialog(this, "输入不能重复");
return;
} else if (m > n) {
JOptionPane.showMessageDialog(this, "输入范围前一个值大于后一个值,请重输。");
return;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "学分不能为字符串或空");
return;
}
}
if (logicstr.equals("none")) {
String sub = a + " " + "between" + " " + s1 + "and" + s2;
ar.add(sub);
} else {
String sub = logicstr + " " + a + " " + "between" + " " + s1
+ "and" + s2;
ar.add(sub);
}
//向List控件中写数据
this.jList1.setListData(ar);
jTextField.setText("");
jjTextField.setText("");
clearButton.setEnabled(true);
} else if ((twoComboBox.getSelectedItem().equals("Like"))
|| (twoComboBox.getSelectedItem().equals(">"))
|| (twoComboBox.getSelectedItem().equals("<"))
|| (twoComboBox.getSelectedItem().equals("="))) {
if (jTextField.getText().equals("")) {
JOptionPane.showMessageDialog(this, "请输入查询条件!");
return;
}
String b = this.twoComboBox.getSelectedItem().toString();
String s1 = this.jTextField.getText();
if (logicstr.equals("none")) {
if (b.equals("Like")) {
String sub = a + " " + "Like "+"'%"+s1+"'";
ar.add(sub);
} else {
ar.add(a + b + s1);
}
} else {
String sub = logicstr + " " + a + b + s1;
ar.add(sub);
}
//向List控件中写入数据
this.jList1.setListData(ar);
//清空按钮可用,范围为本框不可用,查询字段文本框置为空
clearButton.setEnabled(true);
jjTextField.setEnabled(false);
jTextField.setText("");
}
}
private void clearButtonActionPerformed(ActionEvent evt) {
this.ar.clear();
this.jList1.setListData(ar);
clearButton.setEnabled(false);
}
private void jCheckBoxActionPerformed(ActionEvent evt) {
if (!jjTextField.isEnabled())
jjTextField.setEnabled(true);
else
jjTextField.setEnabled(false);
twoComboBox.setEnabled(true);
}
private void ComboBoxActionPerformed(ActionEvent evt) {
if (this.ComboBox.getSelectedItem().toString().equals("xkm")) {
this.twoComboBox.removeAllItems();
this.twoComboBox.addItem("Like");
this.twoComboBox.addItem("=");
}
if (this.ComboBox.getSelectedItem().toString().equals("xkh")
|| (this.ComboBox.getSelectedItem().toString().equals("xf"))) {
this.twoComboBox.removeAllItems();
this.twoComboBox.addItem(">");
this.twoComboBox.addItem("<");
this.twoComboBox.addItem("=");
}
}
private void sureButtonActionPerformed(ActionEvent evt) {
//通过List控件返回值组合SQL语句,进行查询
try {int a =0;
System.out.println("before");
stmt = conn.createStatement(1004, 1007);
if (ar.isEmpty()) {
String sql1 = "select *from objects";
rset = stmt.executeQuery(sql1);
} else {
int i =0;
int t = ar.size();
for (i =0; i <t; i++){
String sql1 = "select * from objects where "+this.ar.get(i).toString();
rset = stmt.executeQuery(sql1);
}
}
int cnum = rset.getMetaData().getColumnCount();
int i = 0;
rset.last();
int n = rset.getRow();
rset.beforeFirst();
row = new String[n][4];
System.out.println(n);
while (rset.next()){
for (int j = 0; j < cnum; j++) {
row[i][j] = rset.getString(j + 1);
}
i++;
a=1;
}
if(a!=1){
JOptionPane.showMessageDialog(this, "没有符合条件的记录!");
}
TableModel jTable1Model = new DefaultTableModel(row, column);
jTable1.setModel(jTable1Model); //刷新Table
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "输入有误!");
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -