package com.yy.view;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.*;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
import com.yy.control.ProductService;
import com.yy.date.DB_Conn;
import com.yy.entity.Product;
public class OutProductFrame extends JFrame implements ActionListener {
JTable showOutProduct;
JLabel jlProductNo,jlProductName,jlProductPrice,jlProductDeliveryDate,jlProducttype;
JTextField tfProductNo,tfProductName,tfProductPrice,tfProductDeliveryDate;
JButton jbaddProduct,jbdeleteProductbyno,jbupdateProduct,jbinquiresAllProduct,jbout;
JPanel text;
Box baseBox,inputBox;
ProductService ps=new ProductService();
ArrayList<Product> products=new ArrayList<Product>() ;
JComboBox conBox;
String[] el={"水果","蔬菜"};
Object productshow[][]=new Object[20][5];
Object[] tableTitle={"商品编号","商品名称","销售价格","销售日期","商品类型"};
JScrollPane showInfor;
public OutProductFrame(){
init();
setTitle("销售管理系统");
Toolkit tool=getToolkit();
Dimension dim=tool.getScreenSize();
setBounds(0,0,dim.width/2,dim.height/2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void init(){
this.setLayout(new BorderLayout());
showOutProduct=new JTable(productshow,tableTitle);
showInfor=new JScrollPane(showOutProduct);
jlProductNo = new JLabel("商品编号");
jlProductName = new JLabel("商品名称");
jlProductPrice = new JLabel("销售价格");
jlProductDeliveryDate = new JLabel("销售日期");
jlProducttype = new JLabel("商品类型");
tfProductNo = new JTextField(10);
tfProductName = new JTextField(10);
tfProductPrice = new JTextField(10);
tfProductDeliveryDate = new JTextField(10);
Box inputBox1 = Box.createHorizontalBox();
inputBox1.add(jlProductNo);
inputBox1.add(tfProductNo);
Box inputBox2 = Box.createHorizontalBox();
inputBox2.add(jlProductName);
inputBox2.add(tfProductName);
Box inputBox3 = Box.createHorizontalBox();
inputBox3.add(jlProductPrice);
inputBox3.add(tfProductPrice);
Box inputBox4 = Box.createHorizontalBox();
inputBox4.add(jlProductDeliveryDate);
inputBox4.add(tfProductDeliveryDate);
Box inputBox5 = Box.createHorizontalBox();
inputBox5.add(jlProducttype);
conBox=new JComboBox(el);
conBox.addItem("日用品");
inputBox5.add(conBox);
Box tfBox=Box.createVerticalBox();//Vertical 垂直方向上的
tfBox.add(inputBox1);
tfBox.add(inputBox2);
tfBox.add(inputBox3);
tfBox.add(inputBox4);
tfBox.add(inputBox5);
jbaddProduct=new JButton("新增");
//badd.addActionListener(this);
jbdeleteProductbyno=new JButton("删除");
//bdelete.addActionListener(this);
jbinquiresAllProduct=new JButton("查看");
//blook.addActionListener(this);
jbupdateProduct=new JButton("修改");
jbout=new JButton("退出");
Box buttonBox=Box.createHorizontalBox();
buttonBox.add(jbaddProduct);
jbaddProduct.addActionListener(this);
buttonBox.add(jbdeleteProductbyno);
jbdeleteProductbyno.addActionListener(this);
buttonBox.add(jbupdateProduct);
jbupdateProduct.addActionListener(this);
buttonBox.add(jbinquiresAllProduct);
jbinquiresAllProduct.addActionListener(this);
buttonBox.add(jbout);
jbout.addActionListener(this);
tfBox.add(buttonBox);
baseBox=Box.createHorizontalBox();
baseBox.add(tfBox);
baseBox.add(showInfor);
//JPanel show=new JPanel();
add(baseBox,BorderLayout.WEST);
showOutProduct.repaint();
baseBox=Box.createVerticalBox();
baseBox.add(tfBox);
baseBox.add(showInfor);
add(baseBox,BorderLayout.CENTER);
jbaddProduct.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Product p=new Product();
DB_Conn dbconn1=new DB_Conn();
String ProductNo=tfProductNo.getText();
String ProductName=tfProductName.getText();
String ProductPrice=tfProductPrice.getText();
String ProductDeliveryDate=tfProductDeliveryDate.getText();
String Producttype=tfProducttype.getText();
String sql="insert into 商品销售 values('"
+ ProductNo+"','"
+ ProductName+"','"
+ ProductPrice+"','"
+ ProductDeliveryDate+"','"
+ Producttype+"')";
System.out.println(sql);
try {
boolean flag=dbconn1.getSm().execute(sql);
if(flag)
System.out.println("success insert");
else
System.out.println("fail");
}
catch (SQLException e1) {
e1.printStackTrace();
}
}
});
baseBox=Box.createVerticalBox();
baseBox.add(tfBox);
baseBox.add(showInfor);
add(baseBox,BorderLayout.CENTER);
}
public void listInit(){
Iterator it=products.iterator();
int i=0;
while(it.hasNext()){
Product p=(Product)it.next();
productshow[i][0]=p.getProductNo();
productshow[i][1]=p.getProductName();
productshow[i][2]=p.getProductPrice();
productshow[i][3]=p.getProductDeliveryDate();
productshow[i][4]=p.getProducttype();
i++;
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jbaddProduct){
Product p=new Product();
p.setProductNo(Integer.parseInt(tfProductNo.getText()));
p.setProductName(tfProductName.getText());
p.setProductPrice(Float.parseFloat(tfProductPrice.getText()));
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
try {
p.setProductDeliveryDate(sdf.parse(tfProductDeliveryDate.getText()));
} catch (java.text.ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String Producttype=tfProducttype.getText();
p.setProducttype(Producttype);
products.add(p);
listInit();
showOutProduct.repaint();
}
if(e.getSource()==jbdeleteProductbyno){
int productNo=Integer.parseInt(tfProductNo.getText());
Iterator it=products.iterator();
while (it.hasNext()){
Product s=(Product)it.next();
if(ps.getProducts()==products){
products.remove(ps);
break;
}
}
listInit();
int size=products.size();
productshow[size][0]=null;
productshow[size][1]=null;
productshow[size][2]=null;
productshow[size][3]=null;
productshow[size][4]=null;
showOutProduct.repaint();
}
if(e.getSource()==jbout){
this.dispose();
}
}
}
}
package com.yy.date;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class DB_Conn {
private String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private String userName="sa";
private String userPwd="123";
private String dbName="HrSys";
private String url="jdbc:sqlserver://localhost:1433;DatabaseName=HrSys";
private Connection Conn=null;
public Statement Sm=null;
public DB_Conn(){
try {
Class.forName(driverName);
Conn=DriverManager.getConnection(url, userName, userPwd);
Sm=Conn.createStatement();
System.out.println("Success");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public Connection getConn() {
return Conn;
}
public void setConn(Connection conn) {
Conn = conn;
}
public Statement getSm() {
return Sm;
}
public void setSm(Statement sm) {
this.Sm = sm;
}
public void DB_close(){
try {
Conn.close();
Sm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}