import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import com.microsoft.jdbc.sqlserver.SQLServerDriver;
import java.applet.Applet;
import javax.swing.*;
import java.util.*;
//import javax.swing.table.*;
public class newCustomer extends JFrame implements ActionListener
{
JFrame frm=new JFrame("新用户");
Label lab1=new Label();
Label lab2=new Label();
Label lab3=new Label();
Label lab4=new Label();
Label lab5=new Label();
Label lab6=new Label();
TextField txf1=new TextField();
TextField txf2=new TextField();
TextField txf3=new TextField();
TextField txf4=new TextField();
TextField txf5=new TextField();
TextField txf6=new TextField();
Button btn1=new Button("添加");
Button btn2=new Button("返回上层菜单");
Button btn3=new Button("修改");
public static void main(String args[])
{
newCustomer frm=new newCustomer();
}
public newCustomer()
{
frm.setSize(700,600);//窗口设置
frm.setBackground(new Color(59,104,229));
frm.setLayout(null);
frm.setLocation(250,250);
frm.setVisible(true);
lab1.setText("姓名");//创建标签
lab1.setAlignment(Label.CENTER);
lab1.setForeground(Color.black);
lab1.setBounds(50,100,100,50);
Font fnt1=new Font("宋体",Font.PLAIN,20);
lab1.setFont(fnt1);
frm.add(lab1);
lab2.setText("性别");
lab2.setAlignment(Label.CENTER);
lab2.setForeground(Color.black);
lab2.setBounds(350,100,100,50);
Font fnt2=new Font("宋体",Font.PLAIN,20);
lab2.setFont(fnt2);
frm.add(lab2);
lab3.setText("证件号码");
lab3.setAlignment(Label.CENTER);
lab3.setForeground(Color.black);
lab3.setBounds(50,200,100,50);
Font fnt3=new Font("宋体",Font.PLAIN,20);
lab3.setFont(fnt3);
frm.add(lab3);
lab4.setText("是否会员");
lab4.setAlignment(Label.CENTER);
lab4.setForeground(Color.black);
lab4.setBounds(350,200,100,50);
Font fnt4=new Font("宋体",Font.PLAIN,20);
lab4.setFont(fnt4);
frm.add(lab4);
lab5.setText("消费金额");
lab5.setAlignment(Label.CENTER);
lab5.setForeground(Color.black);
lab5.setBounds(50,300,100,50);
Font fnt5=new Font("宋体",Font.PLAIN,20);
lab5.setFont(fnt5);
frm.add(lab5);
lab6.setText("折扣");
lab6.setAlignment(Label.CENTER);
lab6.setForeground(Color.black);
lab6.setBounds(350,300,100,50);
Font fnt6=new Font("宋体",Font.PLAIN,20);
lab6.setFont(fnt6);
frm.add(lab6);
txf1.setFont(new Font("宋体",Font.PLAIN,20));
txf2.setFont(new Font("宋体",Font.PLAIN,20));
txf3.setFont(new Font("宋体",Font.PLAIN,20));
txf4.setFont(new Font("宋体",Font.PLAIN,20));
txf5.setFont(new Font("宋体",Font.PLAIN,20));
txf6.setFont(new Font("宋体",Font.PLAIN,20));
txf1.setBounds(200,100,100,50);
txf2.setBounds(500,100,100,50);
txf3.setBounds(200,200,100,50);
txf4.setBounds(500,200,100,50);
txf5.setBounds(200,300,100,50);
txf6.setBounds(500,300,100,50);
frm.add(txf1);
frm.add(txf2);
frm.add(txf3);
frm.add(txf4);
frm.add(txf5);
frm.add(txf6);
btn1.setFont(new Font("宋体",Font.PLAIN,20));
btn2.setFont(new Font("宋体",Font.PLAIN,15));
btn3.setFont(new Font("宋体",Font.PLAIN,20));
btn1.setBounds(200,400,100,50);
btn2.setBounds(500,400,100,50);
btn3.setBounds(350,400,100,50);
frm.add(btn1);
frm.add(btn2);
frm.add(btn3);
btn1.addActionListener(this);//把frm向btn1注册
btn2.addActionListener(this);
btn3.addActionListener(this);
frm.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)//触发查询事件
{
Button btnSql=(Button) e.getSource();
if (btnSql==btn2)
{
dispose();
new housing();
}
else
if (btnSql==btn1)
{
excuteSql();
}
else
if (btnSql==btn3);
{
Update();
}
}
public void excuteSql()
{
if(txf1.getText().equals("")||txf2.getText().equals("")||txf3.getText().equals("")||txf4.getText().equals("")||txf5.getText().equals(""))
{
JOptionPane.showMessageDialog(null,"登记失败!信息不能为空!");
}
else
if (txf1.getText()!=null&&txf2.getText()!=null&&txf3.getText()!=null)
{
try
{
String sql="insert into Customer(Cname,Csex,Cno,Cvip,Ccost,Cdiscount) values('"+txf1.getText()+"','"+txf2.getText()+"','"+txf3.getText()+"','"+txf4.getText()+"','"+txf5.getText()+"','"+txf6.getText()+"')";
Connection con=getConnection();
Statement stm=con.createStatement();
stm.executeUpdate(sql);
JOptionPane.showMessageDialog(null,"添加成功");
con.close();
}
catch (Exception err)
{
err.printStackTrace();
}
}
}
public void Update()
{
if(txf1.getText().equals("")&&txf2.getText().equals("")&&txf3.getText().equals(""))
{
JOptionPane.showMessageDialog(null,"更新失败!信息不能为空!");
}
else
if (txf1.getText()!=null&&txf2.getText()!=null&&txf3.getText()!=null)
{
try
{
String sql="update Customer set Cname='"+txf1.getText()+"' where Csex='"+txf2.getText()+"' and Cno='"+txf3.getText()+"'";
Connection con=getConnection();
Statement stm=con.createStatement();
stm.executeUpdate(sql);
JOptionPane.showMessageDialog(null,"更新成功!");
con.close();
}
catch (Exception err)
{
err.printStackTrace();
}
}
}
public static Connection getConnection()
{
try
{
String url="jdbc:microsoft:sqlserver://localhost:1433;"+"DatabaseName=housing";
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
return DriverManager.getConnection(url,"sa","123456");
}
catch(Exception err)
{
err.printStackTrace();
}
return null;
}
}