📄 um.java
字号:
package atm;
import oo.Sql;
//import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
//import java.awt.Dimension;
import java.awt.GridBagLayout;
import javax.swing.JComboBox;
import java.awt.GridBagConstraints;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class UM extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JComboBox jComboBox = null;
private JTextField jTextField = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JLabel jLabel = null;
private JScrollPane jScrollPane = null;
private JTable jTable = null;
private JButton jButton3 = null;
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
gridBagConstraints11.gridx = 3;
gridBagConstraints11.gridy = 1;
GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
gridBagConstraints21.gridx = 4;
gridBagConstraints21.gridy = 0;
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.fill = GridBagConstraints.VERTICAL;
gridBagConstraints1.weightx = 1.0;
jLabel = new JLabel();
jLabel.setText("选择查询条件:");
GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
gridBagConstraints3.gridx = 4;
gridBagConstraints3.gridy = 1;
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.gridx = 3;
gridBagConstraints2.gridy = 0;
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.VERTICAL;
gridBagConstraints.weightx = 1.0;
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
jPanel.add(jLabel, new GridBagConstraints());
jPanel.add(getJComboBox(), gridBagConstraints);
jPanel.add(getJTextField(), gridBagConstraints1);
jPanel.add(getJButton(), gridBagConstraints2);
jPanel.add(getJButton1(), gridBagConstraints3);
jPanel.add(getJButton3(), gridBagConstraints21);
jPanel.add(getJButton2(), gridBagConstraints11);
}
return jPanel;
}
/**
* This method initializes jComboBox
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBox() {
if (jComboBox == null) {
jComboBox = new JComboBox();
}
return jComboBox;
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setColumns(9);
}
return jTextField;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("查询");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
select();
}
});
}
return jButton;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
public void close(){
this.setVisible(false);
}
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setText("关闭");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
close();
}
});
}
return jButton1;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(getJTable());
}
return jScrollPane;
}
/**
* This method initializes jTable
*
* @return javax.swing.JTable
*/
private JTable getJTable() {
if (jTable == null) {
jTable = new JTable();
}
return jTable;
}
Sql dbc=new Sql();
/**
* This method initializes jButton3
*
* @return javax.swing.JButton
*/
private JButton getJButton3() {
if (jButton3 == null) {
jButton3 = new JButton();
jButton3.setText("删除");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
del();
}
});
}
return jButton3;
}
public void del(){
//Sql dbc = new Sql();
dbc.getConn();
int row = jTable.getSelectedRow();
System.out.println(row);
if (row < 0) {
javax.swing.JOptionPane.showMessageDialog(null, "您还没有选中要删除的信息!");
return;
}
Object value = jTable.getValueAt(row, 0);
String id = value.toString();
System.out.println(id);
int returnValue;
returnValue = JOptionPane.showConfirmDialog(this, "确实要删除该记录吗?", "确认删除", JOptionPane.YES_NO_OPTION);
if (returnValue == JOptionPane.YES_OPTION){
if (dbc.executeUpdateSQL("DELETE FROM userInfo WHERE customerID ='" + id+"'")) {
javax.swing.JOptionPane.showMessageDialog( this,"顾客编号为" + id + "的顾客信息已成功被删除!请刷新!");
} else {
javax.swing.JOptionPane.showMessageDialog(this, "删除失败!请再试。");
}
}else
return ;
}
/**
* @param args
*/
Connection con=null;
private JButton jButton2 = null;
public void showTable(){
con=dbc.getConn();
int count=0;
ResultSet rs=dbc.executeQuery("select * from userInfo");
try{
while(rs.next()){
count++;
}
rs.close();
Object[][] cells=new Object[count][9];
int i=0;
rs=dbc.executeQuery("select * from userInfo");
while(rs.next()){
cells[i]=new Object[]{rs.getString(1),rs.getString(2),
rs.getString(3),rs.getString(4),
rs.getString(5)
};
i++;
}
String[] colnames={"顾客编号","顾客姓名","身份证号","联系电话","居住地址"};
jTable=new JTable(cells,colnames);
jScrollPane.setViewportView(jTable);
}catch(SQLException ex){
javax.swing.JOptionPane.showMessageDialog(null, ex.getMessage().toString());
}
}
public void select(){
if(jTextField.getText().equals("")){
javax.swing.JOptionPane.showMessageDialog(this,"请输入查询信息");
return ;
}
String sql="";
if(((String)jComboBox.getSelectedItem()).equals("顾客编号")){
sql="select * from userInfo where customerID='"+jTextField.getText()+"'";
}else if(((String)jComboBox.getSelectedItem()).equals("顾客姓名")){
sql="select * from userInfo where customerName='"+jTextField.getText()+"'";
}else
if(((String)jComboBox.getSelectedItem()).equals("身份证")){
sql="select * from userInfo where PID='"+jTextField.getText()+"'";
}else {
}
con=dbc.getConn();
int count=0;
ResultSet rs=dbc.executeQuery(sql);
try{
while(rs.next()){
count++;
}
rs.close();
Object[][] cells=new Object[count][9];
int i=0;
rs=dbc.executeQuery(sql);
while(rs.next()){
cells[i]=new Object[]{rs.getString(1),rs.getString(2),
rs.getString(3),rs.getString(4),
rs.getString(5)
};
i++;
}
String[] colnames={"顾客编号","顾客姓名","身份证号","联系电话","居住地址"};
jTable=new JTable(cells,colnames);
jScrollPane.setViewportView(jTable);
}catch(SQLException ex){
javax.swing.JOptionPane.showMessageDialog(null, ex.getMessage().toString());
}
}
/**
* This method initializes jButton2
*
* @return javax.swing.JButton
*/
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setText("刷新");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
initialize1();
}
});
}
return jButton2;
}
/*public static void main(String[] args) {
// TODO 自动生成方法存根
SwingUtilities.invokeLater(new Runnable() {
public void run() {
UM thisClass = new UM();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public UM() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(500, 249);
this.setContentPane(getJContentPane());
this.setTitle("用户信息");
jComboBox.addItem("顾客编号");
jComboBox.addItem("顾客姓名");
jComboBox.addItem("身份证");
this.showTable();
}
private void initialize1() {
this.setSize(500, 249);
this.setContentPane(getJContentPane());
this.setTitle("用户信息");
this.showTable();
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJPanel(), BorderLayout.NORTH);
jContentPane.add(getJScrollPane(), BorderLayout.CENTER);
}
return jContentPane;
}
} // @jve:decl-index=0:visual-constraint="10,10"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -