⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 editsell.java

📁 一个基于JAVA技术的销售信息管理系统
💻 JAVA
字号:
import java.awt.Toolkit;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.swtdesigner.SWTResourceManager;


public class EditSell extends Dialog {

	private Combo combo_5;
	private Combo combo_4;
	private Combo combo_3;
	protected Object result;

	protected Shell shell;

	private MessageBox messageBox;
	private Combo combo_2;
	private Combo combo_1;
	private Combo combo;
	private Text text_2;
	private Text text_1;
	private Text text;
	private String id;
	private String pwd;
	private Date datetime;
	private int sid;
	private int productID;
	private int customerID;
	private int staffID;
	private double price;
	private int sellCount;
	private boolean flag = true;

	/**
	 * Create the dialog
	 * @param parent
	 * @param style
	 */
	public EditSell(Shell parent, int style,String id,String pwd) {
		super(parent, style);
	}

	/**
	 * Create the dialog
	 * @param parent
	 */
	public EditSell(Shell parent,String id,String pwd,int sid) {
		this(parent, SWT.NONE,id,pwd);
		this.id		=	id;
		this.pwd	=	pwd;

		this.sid		=	sid;
	}

	public void setCenter(){
		// 得到屏幕的宽度和高度
		int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
		int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
		// 得到Shell窗口的宽度和高度
		int shellHeight = this.shell.getBounds().height;
		int shellWidth = this.shell.getBounds().width;
		// 如果窗口大小超过屏幕大小,让窗口与屏幕等大
		if (shellHeight > screenHeight)
			shellHeight = screenHeight;
		if (shellWidth > screenWidth)
			shellWidth = screenWidth;
		// 让窗口在屏幕中间显示
		this.shell.setLocation(((screenWidth - shellWidth) / 2),
				((screenHeight - shellHeight) / 2));
	}
	
	/**
	 * Open the dialog
	 * @return the result
	 */
	public Object open() {
		createContents();
		setCenter();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}


	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setSize(346, 385);
		shell.setText("修改销售信息");
		//
		final Label label = new Label(shell, SWT.CENTER);
		label.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
		label.setText("产品名称");
		label.setBounds(55, 62, 65, 17);

		final Label label_1 = new Label(shell, SWT.CENTER);
		label_1.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
		label_1.setText("客户姓名");
		label_1.setBounds(55, 99, 65, 17);

		final Label label_2 = new Label(shell, SWT.CENTER);
		label_2.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
		label_2.setText("销售人员");
		label_2.setBounds(55, 136, 65, 17);

		final Label label_3 = new Label(shell, SWT.CENTER);
		label_3.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
		label_3.setText("销售日期");
		label_3.setBounds(55, 174, 65, 17);

		final Label label_4 = new Label(shell, SWT.CENTER);
		label_4.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
		label_4.setText("销售单价");
		label_4.setBounds(55, 228, 65, 17);

		final Label emailLabel = new Label(shell, SWT.CENTER);
		emailLabel.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
		emailLabel.setText("成交量");
		emailLabel.setBounds(55, 266, 65, 17);

		combo = new Combo(shell, SWT.READ_ONLY);
		combo.setVisibleItemCount(10);
		combo.setBounds(140, 60, 111, 22);

		combo_1 = new Combo(shell, SWT.READ_ONLY);
		combo_1.setVisibleItemCount(10);
		combo_1.setBounds(140, 99, 111, 22);

		combo_2 = new Combo(shell, SWT.READ_ONLY);
		combo_2.setVisibleItemCount(10);
		combo_2.setBounds(140, 136, 111, 22);

		combo_3 = new Combo(shell, SWT.READ_ONLY);
		combo_3.setBounds(257, 62, 53, 20);
		combo_3.setVisible(false);

		combo_4 = new Combo(shell, SWT.READ_ONLY);
		combo_4.setBounds(257, 99, 53, 20);
		combo_4.setVisible(false);

		combo_5 = new Combo(shell, SWT.READ_ONLY);
		combo_5.setBounds(257, 136, 53, 20);
		combo_5.setVisible(false);
		
		getAllInfor("Product",combo,combo_3);
		getAllInfor("Customer",combo_1,combo_4);
		getAllInfor("Staff",combo_2,combo_5);
		text = new Text(shell, SWT.BORDER);
		text.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
		text.setBounds(140, 171, 111, 25);

		text_1 = new Text(shell, SWT.BORDER);
		text_1.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
		text_1.setBounds(140, 225, 111, 25);

		text_2 = new Text(shell, SWT.BORDER);
		text_2.setFont(SWTResourceManager.getFont("", 11, SWT.NONE));
		text_2.setBounds(140, 263, 111, 25);

		getOneInfor(sid,combo,combo_1,combo_2,text,text_1,text_2);
		
		final Button button_2 = new Button(shell, SWT.NONE);
		button_2.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if(combo.getSelectionIndex() == -1 || combo_1.getSelectionIndex() == -1
						|| combo_2.getSelectionIndex() == -1)
					{
						messageBox = new MessageBox(shell,SWT.ICON_INFORMATION);
						messageBox.setMessage("请选择[产品名称][客户姓名][销售人员]");
						messageBox.setText("添加销售信息");
						messageBox.open();
						flag = false;
					}
				if(flag)
				    try{
				    	datetime = Date.valueOf(text.getText());
					  }
					  catch(Exception px)
					  {
							messageBox = new MessageBox(shell,SWT.ICON_INFORMATION);
							messageBox.setMessage("日期格式错误,请重新输入");
							messageBox.setText("添加销售信息");
							messageBox.open();
							flag = false;
					  }

				if(flag)
				try
				{
					price = Double.parseDouble(text_1.getText());
					sellCount = Integer.parseInt(text_2.getText());
				}
				catch(NumberFormatException m)
				{
					messageBox = new MessageBox(shell,SWT.ICON_INFORMATION);
					messageBox.setMessage("价格和销售量输入错误");
					messageBox.setText("添加销售信息");
					messageBox.open();
					flag = false;
				}
				if(flag)
				{
					productID = Integer.parseInt(combo_3.getItem(combo.getSelectionIndex()));
					customerID = Integer.parseInt(combo_4.getItem(combo_1.getSelectionIndex()));
					staffID = Integer.parseInt(combo_5.getItem(combo_2.getSelectionIndex()));

					messageBox = new MessageBox(shell,SWT.ICON_QUESTION|SWT.OK|SWT.CANCEL);
					messageBox.setMessage("产品名称:		" + combo.getItem(combo.getSelectionIndex()) +
							"\n客户姓名:		" + combo_1.getItem(combo_1.getSelectionIndex()) +
							"\n工作人员:	" + combo_2.getItem(combo_2.getSelectionIndex()) +
							"\n销售日期:	" + datetime +
							"\n单价(元):	" + price +
							"\n销售量:	" + sellCount +
							"\n\n请确认销售信息是否正确");
					messageBox.setText("新客户添加确认");
					if(messageBox.open() == SWT.OK)
					{
						if(updateSell(sid,productID,customerID,staffID,datetime,price,sellCount))
						{
							messageBox = new MessageBox(shell,SWT.ICON_INFORMATION);
							messageBox.setMessage("销售信息添加完成");
							messageBox.setText("完成");
							messageBox.open();
						}
						else
						{
							messageBox = new MessageBox(shell,SWT.ICON_ERROR);
							messageBox.setMessage("请确认输入信息是否正确");
							messageBox.setText("添加销售信息失败");
							messageBox.open();
							flag = false;
						}
					}
				}

				if(flag)
					clearAll();
				flag = true;
			}
		});
		button_2.setText("提 交");
		button_2.setBounds(55, 300, 65, 23);

		final Button button_3 = new Button(shell, SWT.NONE);
		button_3.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				text.setText("");
				text_1.setText("");
				text_2.setText("");
				combo.deselectAll();
				combo_1.deselectAll();
				combo_2.deselectAll();
			}
		});
		button_3.setText("清 空");
		button_3.setBounds(126, 300, 65, 23);

		final Button button_4 = new Button(shell, SWT.NONE);
		button_4.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				shell.dispose();
			}
		});
		button_4.setText("取 消");
		button_4.setBounds(220, 300, 65, 23);

		final Label label_5 = new Label(shell, SWT.NONE);
		label_5.setFont(SWTResourceManager.getFont("新宋体", 14, SWT.BOLD));
		label_5.setAlignment(SWT.CENTER);
		label_5.setText("请修改销售信息");
		label_5.setBounds(0, 18, 340, 24);

		final Label label_6 = new Label(shell, SWT.NONE);
		label_6.setText("例如 2008-06-26");
		label_6.setBounds(140, 202, 123, 17);

	}

	private void clearAll(){
		text.setText("");
		text_1.setText("");
		text_2.setText("");
		combo.deselectAll();
		combo_1.deselectAll();
		combo_2.deselectAll();
		combo_3.deselectAll();
		combo_4.deselectAll();
		combo_5.deselectAll();
	}

	public void getAllInfor(String table,Combo combo,Combo comboID){//sql
		Connection con;
		Statement stmt;
		try{
			combo.removeAll();
			comboID.removeAll();
			DBCon dbc = new DBCon();
			con = DriverManager.getConnection(dbc.url,id,pwd);
			stmt = con.createStatement();
			String query = "select ID,Name from " + table +" order by Name";
			ResultSet rs = stmt.executeQuery(query);
			while( rs.next()){
				combo.add(rs.getString("Name"));
				comboID.add(rs.getString("ID"));
			}
			dbc.dbClose(con,rs);
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	

	public void getOneInfor(int sell_id,Combo combo_p,Combo combo_c,Combo combo_s,Text text_d,Text text_p,Text text_c){//sql
		Connection con;
		Statement stmt;
		try{
			combo_p.deselectAll();
			combo_c.deselectAll();
			combo_s.deselectAll();
			DBCon dbc = new DBCon();
			con = DriverManager.getConnection(dbc.url,id,pwd);
			stmt = con.createStatement();
			String query = "select Product.Name as PN,Customer.Name as CN,Staff.Name as SN,Sell.SellDateTime as SSDT," +
					"Sell.Price as SP,Sell.SellCount as SSC " +
					"from Sell,Product,Customer,Staff " +
					"where Sell.ID = " +sell_id +" and " +
							"ProductID = Product.ID and CustomerID = Customer.ID and StaffID = Staff.ID;";
			ResultSet rs = stmt.executeQuery(query);
			while( rs.next()){
				
				for(int i = 0;i<combo_p.getItemCount();i++)
					if(combo_p.getItem(i).equals(rs.getString("PN")))
						combo_p.select(i);
				for(int i = 0;i<combo_c.getItemCount();i++)
					if(combo_c.getItem(i).equals(rs.getString("CN")))
						combo_c.select(i);
				for(int i = 0;i<combo_s.getItemCount();i++)
					if(combo_s.getItem(i).equals(rs.getString("SN")))
						combo_s.select(i);
				
				text_d.setText(rs.getString("SSDT").substring(0, 10));
				text_p.setText(rs.getString("SP"));
				text_c.setText(rs.getString("SSC"));
			}
			dbc.dbClose(con,rs);
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

	public boolean updateSell(int sid,int pname,int cname,int sname,Date date,double price,int count){//sql
		Connection con;
		try{
			DBCon dbc = new DBCon();
			con = DriverManager.getConnection(dbc.url,id,pwd);
			CallableStatement cs = con.prepareCall("{call dbo.sp_UpdateSell(?,?,?,?,?,?,?)}");
			cs.setInt(1, sid);
			cs.setInt(2, pname);
			cs.setInt(3, cname);
			cs.setInt(4, sname);
			cs.setDate(5, date);
			cs.setDouble(6, price);
			cs.setInt(7, count);
			cs.execute();
			dbc.dbClose(con);
			return true;
		}
		catch(Exception e){
			return false;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -