📄 vipbuy.java
字号:
package classfile;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.*;
import javax.swing.table.AbstractTableModel;
import java.util.Vector;
class VIPBuy extends JFrame{
private int flag = 0;
private int flagvipno = 1;
JLabel jlvipid,jlsid,jlscount;
JTextField jtvipid,jtsid,jtscount;
JButton jbimp,jbuy,jcancel;
MyTableModel model = new MyTableModel();
JScrollPane js;
static final String[] title = {"VIP编号","操作员","商品编号","商品名称","商品单价","购买数量","VIP折扣","合计金额"};
JTable t;
VIPBuy(JDesktopPane jd){
if((jd.getAllFrames().length)>=1)
return;
final JInternalFrame vipinf = new JInternalFrame("VIP购物信息录入窗口",false,true,false,true);
final Container cVip = getContentPane();
cVip.setLayout(new FlowLayout());
jlvipid = new JLabel("V I P 号: ");
jlsid = new JLabel("商品编号: ");
jlscount = new JLabel("商品数量: ");
jbimp = new JButton("显示VIP详细信息");
jtvipid = new JTextField(15);
jtsid = new JTextField(10);
jtscount = new JTextField(10);
jbuy = new JButton("购买");
jcancel = new JButton("取消");
cVip.add(jlvipid);
cVip.add(jtvipid);
cVip.add(new JLabel(" "));
cVip.add(jbimp);
cVip.add(jlsid);
cVip.add(jtsid);
cVip.add(new JLabel(" "));
cVip.add(jlscount);
cVip.add(jtscount);
vipinf.add(cVip);
jd.add(vipinf);
update(cVip);
cVip.add(jbuy);
cVip.add(jcancel);
jtvipid.addFocusListener(new FocusAdapter(){
public void focusLost(FocusEvent eJtvipid){
if(jtvipid.getText().length()>0){
MakeDatebase conn = new MakeDatebase();
try{
String sql = null;
System.out.println (flag);
System.out.println (flagvipno);
conn.connectionSql();
if(flag == Integer.parseInt(jtvipid.getText())){
return;
}
if(flag == flagvipno){
if(JOptionPane.showConfirmDialog(null,"VIP号:(" + flag + ")购物商品还未结帐,保存记录(是)/ 删除记录(否)" ,"友情提示",JOptionPane.YES_NO_OPTION)==JOptionPane.NO_OPTION){
sql = "delete from temp where vip_no =" + flag;
conn.updateDate(sql);
update(cVip);
}
}
try{
sql = "select * from VIPID where vip_no =" + jtvipid.getText();
conn.selectDate(sql);
if(conn.rct.next()){
flag = Integer.parseInt(jtvipid.getText());
}
else{
JOptionPane.showMessageDialog(null,"此VIP号不存在");
return;
}
}
catch(Exception eC){
System.out.println ("读取VIP号时出错");
}
sql = "select * from temp where vip_no=" + jtvipid.getText();
conn.selectDate(sql);
if(conn.rct.next()){
System.out.println (flag);
System.out.println (flagvipno);
if(JOptionPane.showConfirmDialog(null,"此VIP号中有挂单记录,继续上次的购物记录(是)/删除原数据重新购买(否)","友情提示",JOptionPane.YES_NO_OPTION)==JOptionPane.NO_OPTION){
sql = "delete from temp where vip_no =" + jtvipid.getText();
conn.updateDate(sql);
}
else{
flag = 0;
flagvipno = 1;
}
flagvipno = Integer.parseInt(jtvipid.getText());
return;
}
}
catch(Exception eJtvipid1){
System.out.println ("读取数据时出错!");
}
finally{
update(cVip);
conn.closeConnection();
}
}
}
});
jtscount.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==10){
MakeDatebase conn = new MakeDatebase();
try{
try{
Integer.parseInt(jtvipid.getText());
}
catch(Exception eCheck){
JOptionPane.showMessageDialog(null,"VIP编号为数字!");
return;
}
conn.connectionSql();
String sql = "select vip_no from VIPID where vip_no =" + jtvipid.getText();
System.out.println (sql);
conn.selectDate(sql);
if(!conn.rct.next()){
JOptionPane.showMessageDialog(null,"你输入的VIP号并不存在!");
return;
}
try{
Integer.parseInt(jtsid.getText());
}
catch(Exception eCheck){
JOptionPane.showMessageDialog(null,"商品编号输入有误!");
return;
}
conn.connectionSql();
sql = "select sid from commodiy_id where sid =" + jtsid.getText();
System.out.println (sql);
conn.selectDate(sql);
if(!conn.rct.next()){
JOptionPane.showMessageDialog(null,"你输入的商品编号并不存在!");
return;
}
try{
Integer.parseInt(jtscount.getText());
}
catch(Exception eCheck){
JOptionPane.showMessageDialog(null,"购买数量输入有误!");
return;
}
try{
flagvipno = Integer.parseInt(jtvipid.getText());
sql = "insert into [temp](vip_no,username,sid,sname,sprice,scount,sdiscount,sincome)(select vip_no,'" + LoginJFrame.usernamelogin + "',sid,sname,sprice," + jtscount.getText() +" ,sdiscount,sprice*sdiscount/10*" + jtscount.getText() + " from commodiy_id,VIPID where vip_no = " + jtvipid.getText() + " and sid = " + jtsid.getText() + ")";
System.out.println (sql);
conn.updateDate(sql);
jtsid.setText("");
jtscount.setText("");
update(cVip);
}
catch(Exception eCheck){
System.out.println ("添加失败");
}
}
catch(Exception eCheck){
System.out.println ("读取VIP号是出错!!");
}
finally{
conn.closeConnection();
}
}
}
});
jbuy.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
MakeDatebase conn = new MakeDatebase();
try{
conn.connectionSql();
String sql = "insert into consume(vip_no,username,sid,sname,sprice,scount,sdiscount,sincome,sdate)(select vip_no,username,sid,sname,sprice,scount,sdiscount,sincome,getdate() from [temp]) delete from [temp] where vip_no = " + Integer.parseInt(jtvipid.getText());
System.out.println (sql);
conn.updateDate(sql);
MyTableModel model = ((MyTableModel)t.getModel());
int size = model.getRowCount();
for(int i=0;i<size;i++){
String no = model.getValueAt(i,2).toString();
String num = model.getValueAt(i,5).toString();
sql = "Update commodiy_id set scount=scount-"+num+" where sid="+no;
conn.updateDate(sql);
}
update(cVip);
JOptionPane.showMessageDialog(null,"购买成功");
flag = 0;
flagvipno = 1;
}catch(Exception ee){
System.out.println ("支付失败");
}
finally{
conn.closeConnection();
}
}
});
jcancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent eJcancel){
try{
vipinf.setClosed(true);
}
catch(Exception eJb2){
System.out.println ("关闭窗体失败");
}
}
});
this.addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent e){
System.out.println ("dddd");
}
public void windowClosing(WindowEvent e){
System.out.println ("cccc");
}
});
vipinf.setSize(480,305);
vipinf.setResizable(false);
vipinf.setVisible(true);
}
void update(Container cVip){
String sql=null;
try{
if(jtvipid.getText().equals(""))
sql = "select * from temp where vip_no=11111";
else
sql = "select * from temp where vip_no= " + Integer.parseInt(jtvipid.getText());
MakeDatebase conn = new MakeDatebase();
System.out.println (sql);
conn.rct = conn.sta.executeQuery(sql);
model.setData(title,conn.rct);
model.fireTableStructureChanged();
t = new JTable(model);
js = new JScrollPane(t);
js.setPreferredSize(new Dimension(460,170));
cVip.add(js);
conn.closeConnection();
}catch(Exception e){
System.out.println ("error");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -