📄 vipshoppingframe.java
字号:
import java.sql.*;
import javax.swing.*;
//import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.lang.*;
public class VipShoppingFrame extends JDialog
{
private Connection Shoppingconnection;
private Statement Shoppingstatement;
private Statement VIPNamestatement;
private Statement InsertInputstatement;
private ResultSet ShoppingresultSet;
private ResultSet VIPNameresultSet;
private ResultSetMetaData rsMetaData;
//GUI变量定义
private JLabel VipSerialLabel ;
private JLabel VipNameLabel ;
private JLabel VipCommoditySerialLabel ;
private JLabel VipCommodityNameLabel ;
private JLabel VipCommodityPriceLabel ;
private JLabel VipCommodityRealPriceLabel ;
private JTextField VipSerialField;
private JTextField VipNameField;
private JTextField VipCommoditySerialField;
private JTextField VipCommodityNameField;
private JTextField VipCommodityPriceField;
private JTextField VipCommodityRealPriceField;
private JButton SubmmitButton ;
private JButton cancelButton ;
private JFrame parent;
public VipShoppingFrame(JFrame parent, boolean modal)
{
super(parent, modal);
this.parent = parent;
pack();
initComponents();
DatabaseConnection();
}
private void initComponents()
{
GridBagLayout layout = new GridBagLayout();
Container contents = getContentPane();
contents.setLayout(layout);
GridBagConstraints constraints = new GridBagConstraints();
setTitle ("VIP购物信息录入窗口");
addWindowListener(new WindowAdapter ()
{
public void windowClosing(WindowEvent event)
{
setVisible(false);
dispose();
}
});
VipSerialField = new JTextField(); // needed below
VipSerialField.setPreferredSize(new Dimension(150, 25));
VipSerialLabel = new JLabel();
VipSerialLabel.setText("V I P 号: ");
VipSerialLabel.setLabelFor(VipSerialField);
constraints.weightx = 100;
constraints.weighty = 100;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
contents.add(VipSerialLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 0;
constraints.gridwidth = 2;
constraints.gridheight = 1;
contents.add(VipSerialField, constraints);
VipSerialField.addKeyListener (new java.awt.event.KeyAdapter()
{
public void keyReleased(KeyEvent e)
{
ShowVipName();
}
});
//2
VipNameField = new JTextField(); // needed below
VipNameField.setPreferredSize(new Dimension(150, 25));
VipNameField.setEditable(false);
//VipNameField.setBorder(BorderFactory.createLoweredBevelBorder());
VipNameLabel = new JLabel();
VipNameLabel.setText("会 员 姓 名: ");
VipNameLabel.setLabelFor(VipNameField);
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 1;
constraints.gridheight = 1;
contents.add(VipNameLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 1;
constraints.gridwidth = 2;
constraints.gridheight = 1;
contents.add(VipNameField, constraints);
//3
VipCommoditySerialField = new JTextField(); // needed below
VipCommoditySerialField.setPreferredSize(new Dimension(150, 25));
VipCommoditySerialLabel = new JLabel();
VipCommoditySerialLabel.setText("商 品 标 号: ");
VipCommoditySerialLabel.setLabelFor(VipCommoditySerialField);
constraints.gridx = 0;
constraints.gridy = 2;
constraints.gridwidth = 1;
constraints.gridheight = 1;
contents.add(VipCommoditySerialLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 2;
constraints.gridwidth = 2;
constraints.gridheight = 1;
contents.add(VipCommoditySerialField, constraints);
VipCommoditySerialField.addKeyListener (new java.awt.event.KeyAdapter()
{
public void keyReleased(KeyEvent e)
{
ShowPrice();
}
});
//4
VipCommodityNameField = new JTextField(); // needed below
VipCommodityNameField.setPreferredSize(new Dimension(150, 25));
VipCommodityNameField.setEditable(false);
VipCommodityNameLabel = new JLabel();
VipCommodityNameLabel.setText("商 品 名 称: ");
VipCommodityNameLabel.setLabelFor(VipCommodityNameField);
constraints.gridx = 0;
constraints.gridy = 3;
constraints.gridwidth = 1;
constraints.gridheight = 1;
contents.add(VipCommodityNameLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 3;
constraints.gridwidth = 2;
constraints.gridheight = 1;
contents.add(VipCommodityNameField, constraints);
//5
VipCommodityPriceField = new JTextField(); // needed below
VipCommodityPriceField.setPreferredSize(new Dimension(150, 25));
VipCommodityPriceField.setEditable(false);
VipCommodityPriceLabel = new JLabel();
VipCommodityPriceLabel.setText("商 品 价 格: ");
VipCommodityPriceLabel.setLabelFor(VipCommodityPriceField);
constraints.gridx = 0;
constraints.gridy = 4;
constraints.gridwidth = 1;
constraints.gridheight = 1;
contents.add(VipCommodityPriceLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 4;
constraints.gridwidth = 2;
constraints.gridheight = 1;
contents.add(VipCommodityPriceField, constraints);
//6
VipCommodityRealPriceField = new JTextField(); // needed below
VipCommodityRealPriceField.setPreferredSize(new Dimension(150, 25));
VipCommodityRealPriceField.setEditable(false);
VipCommodityRealPriceLabel = new JLabel();
VipCommodityRealPriceLabel.setText("实 收 价 格: ");
VipCommodityRealPriceLabel.setLabelFor(VipCommodityRealPriceField);
constraints.gridx = 0;
constraints.gridy = 5;
constraints.gridwidth = 1;
constraints.gridheight = 1;
contents.add(VipCommodityRealPriceLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 5;
constraints.gridwidth = 2;
constraints.gridheight = 1;
contents.add(VipCommodityRealPriceField, constraints);
JPanel buttonPanel = createButtonPanel(); // sets global loginButton
constraints.gridx = 0;
constraints.gridy = 6;
constraints.gridwidth = 3;
constraints.gridheight = 2;
contents.add(buttonPanel, constraints);
setSize( 450, 280 );
setResizable(false);
setLocation(200,100);
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, 0));
//panel.setSize( 490, 40 );
//panel.setBorder(BorderFactory.createEtchedBorder());
// login button (global variable)
SubmmitButton = new JButton();
SubmmitButton.setText("确 定");
SubmmitButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
VipShoppingInfoInput();
}
});
panel.add(SubmmitButton);
// space
panel.add(Box.createRigidArea(new Dimension(200,0)));
// cancel button
JButton cancelButton = new JButton();
cancelButton.setText("返 回");
cancelButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
dispose();
}
});
panel.add(cancelButton);
// space
Vector buttons = new Vector(2);
buttons.add(SubmmitButton);
buttons.add(cancelButton);
equalizeComponentSizes(buttons);
buttons.removeAllElements(); // simplify gc
return panel;
} // createButtonPanel()
private void equalizeComponentSizes(java.util.List components) {
// Get the largest width and height
int i = 0;
Dimension maxPreferred = new Dimension(0,0);
JComponent oneComponent = null;
Dimension thisPreferred = null;
for (i = 0; i < components.size(); ++i) {
oneComponent = (JComponent)components.get(i);
thisPreferred = oneComponent.getPreferredSize();
maxPreferred.width =
Math.max(maxPreferred.width, (int)thisPreferred.getWidth());
maxPreferred.height =
Math.max(maxPreferred.height, (int)thisPreferred.getHeight());
}
// reset preferred and maximum size since BoxLayout takes both
// into account
for (i = 0; i < components.size(); ++i) {
oneComponent = (JComponent)components.get(i);
oneComponent.setPreferredSize((Dimension)maxPreferred.clone());
oneComponent.setMaximumSize((Dimension)maxPreferred.clone());
}
} // equalizeComponentSizes()
private void ShowPrice()
{
if(VipCommoditySerialField.getText().equals(""))
{
return ;
}
try
{
String CommodityQuery = "select 商品名称,商品价格,商品折扣 from COMMODITY_ID where 商品编号 = "+VipCommoditySerialField.getText()+"";
String CommodityName;
String CommodityPrice;
String CommodityDiscount;
String CommodityRealPrice;
float price;
float discount;
float realprice;
Shoppingstatement = Shoppingconnection.createStatement();
ShoppingresultSet = Shoppingstatement.executeQuery( CommodityQuery );
boolean moreRecords = ShoppingresultSet.next();
if ( !moreRecords )
{
JOptionPane.showMessageDialog( this,"对不起,无此商品号,请重新输入" );
//setTitle( "无记录显示" );
//VipCommoditySerialField.setText("");
VipCommodityNameField.setText("");
VipCommodityPriceField.setText("");
VipCommodityRealPriceField.setText("");
return;
}
CommodityName = ShoppingresultSet.getString( 1 );
CommodityPrice = ShoppingresultSet.getString( 2 );
CommodityDiscount = ShoppingresultSet.getString( 3 );
price = Float.parseFloat(CommodityPrice);
discount = Float.parseFloat(CommodityDiscount);
realprice = price*discount;
CommodityRealPrice = Float.toString(realprice);
VipCommodityNameField.setText(CommodityName);
VipCommodityPriceField.setText(CommodityPrice);
VipCommodityRealPriceField.setText(CommodityRealPrice);
}
catch ( SQLException sqlex )
{
sqlex.printStackTrace();
}
}
private void ShowVipName()
{
if(VipSerialField.getText().equals(""))
{
return ;
}
String VipNameQuery = "select 姓名 from VIPID where VIP编号 = "+VipSerialField.getText()+"";
String VipName;
try
{
VIPNamestatement = Shoppingconnection.createStatement();
VIPNameresultSet = VIPNamestatement.executeQuery( VipNameQuery );
boolean moreRecords1 = VIPNameresultSet.next();
if ( !moreRecords1 )
{
JOptionPane.showMessageDialog( this,"对不起,无此会员号,请重新输入" );
//setTitle( "无记录显示" );
//VipSerialField.setText("");
VipNameField.setText("");
return;
}
VipName = VIPNameresultSet.getString( 1 );
VipNameField.setText(VipName);
}
catch ( SQLException sqlex )
{
sqlex.printStackTrace();
}
}
private void VipShoppingInfoInput()
{
if(
VipSerialField.getText().equals( "" )||
VipNameField.getText().equals("")||
VipCommoditySerialField.getText().equals( "" )||
VipCommodityNameField.getText().equals( "" )||
VipCommodityPriceField.getText().equals( "" )||
VipCommodityRealPriceField.getText().equals( "" ))
{
JOptionPane.showMessageDialog( this,"请填会员购物资料" );
//setTitle( "无记录显示" );
return;
}
try
{
String ShoppingInput = "insert into CONSUME VALUES("
+VipSerialField.getText()+",'"+VipNameField.getText()+"',"
+VipCommoditySerialField.getText()+",'"+VipCommodityNameField.getText()+"',"
+VipCommodityPriceField.getText()+","+VipCommodityRealPriceField.getText()+")";
Shoppingstatement = Shoppingconnection.createStatement();
int insert = Shoppingstatement.executeUpdate( ShoppingInput );//executeUpdate返回一个整型值
if (insert == 1)
{
JOptionPane.showMessageDialog( this,"VIP用户购物信息登记成功!" );
VipSerialField.setText("");
VipNameField.setText("");
VipCommoditySerialField.setText("");
VipCommodityNameField.setText("");
VipCommodityPriceField.setText("");
VipCommodityRealPriceField.setText("");
}
}
catch ( SQLException sqlex )
{
sqlex.printStackTrace();
}
}
private void DatabaseConnection()
{
String url = "jdbc:odbc:VipQuery";
String username = "";
String password = "";
//加载驱动程序以连接数据库
try
{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
Shoppingconnection = DriverManager.getConnection( url, username, password );
}
//捕获加载驱动程序异常
catch ( ClassNotFoundException cnfex )
{
System.err.println("装载 JDBC/ODBC 驱动程序失败。" );
cnfex.printStackTrace();
System.exit( 1 ); // terminate program
}
//捕获连接数据库异常
catch ( SQLException sqlex )
{
System.err.println( "无法连接数据库" );
sqlex.printStackTrace();
System.exit( 1 ); // terminate program
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -