📄 vip.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import java.text.DecimalFormat;
public class Vip extends JFrame
{
private JLabel label1,label2,label3,label4,label5,label6;
private JButton button1,button2;
private JTextField textField1,textField2,textField3,textField4,textField5,textField6;
private JTable table;
private GridBagLayout layout;
private GridBagConstraints constraints;
private Container container;
private JPanel panel;
private String name,money,url,vip_id,vip_num,name1,vip_num1;
private DecimalFormat nodigits;
private Statement statement;
private Connection connection;
private Integer vip_discount;
private Boolean b1=true,b2=true;
public Vip()
{
super("VIP管理");
nodigits = new DecimalFormat("0");
container=getContentPane();
layout=new GridBagLayout();
table=new JTable();
panel=new JPanel();
panel.setLayout( layout );
constraints = new GridBagConstraints();
label1=new JLabel("姓名");
label2=new JLabel("购书额");
label4=new JLabel("购书记录");
label5=new JLabel("VIP号");
label6=new JLabel("目前折扣数");
textField1=new JTextField(6);
textField2=new JTextField(4);
textField3=new JTextField(6);
textField4=new JTextField(4);
textField4.setEditable(false);
textField5=new JTextField(4);
textField6=new JTextField(4);
textField6.setEditable(false);
button1=new JButton("添加VIP");
button2=new JButton("查询记录");
addComponent(label1,0,0,2,1);
addComponent(textField1,0,2,2,1);
addComponent(label2,0,4,2,1);
addComponent(textField2,0,6,2,1);
addComponent(label5,0,8,2,1);
addComponent(textField5,0,10,2,1);
addComponent(button1,2,0,6,1);
addComponent(label6,6,15,2,1);
addComponent(textField6,6,17,2,1);
addComponent(label4,6,0,15,1);
addComponent(button2,2,6,12,1);
container.add(panel,BorderLayout.NORTH);
container.add(table,BorderLayout.SOUTH);
button1.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
if(b1){
textField5.setEditable(false);
textField2.setEditable(true);
textField1.setText("");
textField2.setText("");
textField5.setText("");
textField6.setText("");
b1=false;
b2=true;
}
else{
name=textField1.getText();
money=textField2.getText();
vip_discount= 100- Integer.parseInt(nodigits.format( (Integer.parseInt(money)-500)/1000+1 ))*2;
if (vip_discount<80) vip_discount=80;
textField6.setText(""+vip_discount);
try{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
url = "jdbc:odbc:myaccess";
connection = DriverManager.getConnection( url );
statement = connection.createStatement();
String sql1="Insert into VIP(Vip_name,total,discount) Values('"
+name+"','"+money+"','"+vip_discount+"')";
statement.executeUpdate( sql1);
}
catch( Exception ex ){
System.out.println( ex );
}
try{
String sql2=" select Vip_id from VIP where Vip_id>=all(select Vip_id from VIP)";
ResultSet rs1 = statement.executeQuery(sql2);
rs1.next();
ResultSetMetaData rsmd1 = rs1.getMetaData();
String vip_id = new String(rs1.getString("Vip_id"));
textField5.setText(vip_id);
}
catch( Exception ex ){
System.out.println( ex );
}
b1=true;
}
}
}
);
button2.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
if(b2){
textField2.setEditable(false);
textField5.setEditable(true);
textField1.setText("");
textField2.setText("");
textField5.setText("");
textField6.setText("");
b2=false;
b1=true;
}
else {
try{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
url = "jdbc:odbc:myaccess";
connection = DriverManager.getConnection( url );
statement = connection.createStatement();
name1=textField1.getText();
vip_num=textField5.getText();
String sql3="Select discount from VIP where ";
if( name1.compareTo( "" ) != 0 )
sql3 += "Vip_name='" +name1+"'";
else{ if( vip_num.compareTo( "" ) != 0 )
sql3 += "Vip_id=" + Integer.parseInt(vip_num);
}
System.out.println(sql3);
ResultSet rs2 = statement.executeQuery(sql3);
rs2.next();
ResultSetMetaData rsmd2= rs2.getMetaData();
String vip_discount1 = new String(rs2.getString("discount"));
System.out.println(vip_discount1);
textField6.setText(vip_discount1);
if( name1.compareTo("") !=0){
String sql4="Select vip_id from VIP where vip_name='"+name1 + "'";
ResultSet rs4= statement.executeQuery(sql4);
rs4.next();
ResultSetMetaData rsmd4= rs4.getMetaData();
vip_num1 = new String(rs4.getString("vip_id"));
if(vip_num.compareTo("")!=0 && vip_num.compareTo(vip_num1)!=0)
JOptionPane.showMessageDialog(null,"输入信息不匹配");
}
else vip_num1=new String(vip_num);
String sql5="Select total from VIP where vip_id="+vip_num1;
ResultSet rs5= statement.executeQuery(sql5);
rs5.next();
ResultSetMetaData rsmd5= rs5.getMetaData();
String total = new String(rs5.getString("total"));
textField2.setText(total);
String sql = "Select t1.Trade_date, B.Book_name, B.Price, t2.count, t2.discount from Book_info B,Trade t1,Trade_book t2,VIP V where t1.Trade_id = t2.Trade_id and t2.book_id=B.book_id and t1.Buyer=V.vip_id";
// String sql = "Select x.Trade_date, y.count, y.discount from Trade x,Trade_book y,VIP where x.Trade_id = y.Trade_id and VIP.vip_id=x.Buyer";
if( name1.compareTo( "" ) != 0 )
sql += " and V.vip_name='" +name1+"'";
if( vip_num.compareTo( "" ) != 0 )
sql += " and t1.Buyer=" + Integer.parseInt(vip_num);
System.out.println( sql );
ResultSet rs = statement.executeQuery( sql );
boolean moreRecords = rs.next(); // 定位到第一条记录
if ( !moreRecords ){
JOptionPane.showMessageDialog( null,"对不起,没有你要找的!" );
}
else{
Vector columnHeads = new Vector();
Vector rows = new Vector();
ResultSetMetaData rsmd = rs.getMetaData();
for( int i = 1; i <= rsmd.getColumnCount(); i++ ){
columnHeads.addElement( rsmd.getColumnName( i ) );
}
do{
rows.addElement( getNextRow( rs, rsmd ) );
}while( rs.next() );
table = new JTable( rows, columnHeads );
}
container.add( table, BorderLayout.SOUTH );
container.add( new JScrollPane( table,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED ) );
table.repaint();
container.validate();
rs.close();
connection.close();
}
catch( Exception ex ){
System.out.println( ex );
}
b2=true;
}
}
}
);
setSize(500,450);
setVisible(true);
}
private Vector getNextRow( ResultSet rs,ResultSetMetaData rsmd ) throws SQLException{
Vector currentRow = new Vector();
int i;
for ( i = 1; i <= rsmd.getColumnCount(); i++ )
currentRow.addElement( rs.getString(i) );
return currentRow; //返回一条记录
}
private void addComponent(Component component,int row,int column,int width,int height)
{
constraints.gridx=column;
constraints.gridy=row;
constraints.gridwidth=width;
constraints.gridheight=height;
layout.setConstraints(component,constraints);
panel.add(component);
}
public static void main(String args[])
{
Vip application=new Vip();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -