日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > windows >内容正文

windows

JAVA学习作品之销售管理系统V1.0

發(fā)布時(shí)間:2023/12/31 windows 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA学习作品之销售管理系统V1.0 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

? ? ?這套管理系統(tǒng)是本人學(xué)習(xí)JAVA一個(gè)月的作品,開發(fā)周期近10天左右,代碼量約1500行,基于Eclipse JDK1.7,運(yùn)用了Swing/SWT 開發(fā)工具Windowbuilder,配套的還包括JDBC驅(qū)動(dòng)、以及微軟的SQL Server數(shù)據(jù)庫。源碼全部開放,本人多輪測試,結(jié)果OK。由于本人初學(xué)JAVA,代碼漏洞及不足之處難免,還請多多指正!實(shí)用方面,如果加入掃碼器接口,軟件應(yīng)該會(huì)更完美!只是條件有限,僅為學(xué)習(xí)。

第一部分:首先看下軟件信息,大小600k不到,雙擊運(yùn)行,前提JDK1.7及以上環(huán)境配置好, SQL Server數(shù)據(jù)庫安裝好,數(shù)據(jù)庫配置3個(gè)table如下圖:

?

?

雙擊.jar程序,打開系統(tǒng),界面如圖:

工具欄兩個(gè)選項(xiàng):數(shù)據(jù)庫管理、關(guān)于,主界面5大功能塊進(jìn)貨、定價(jià)、銷售、報(bào)廢、報(bào)表。其他(未開發(fā)功能)。

點(diǎn)開工具欄數(shù):據(jù)庫管理,可以選擇清除系統(tǒng)中舊數(shù)據(jù),防止系統(tǒng)運(yùn)行幾年后數(shù)據(jù)累積增導(dǎo)致運(yùn)行速度變慢:

點(diǎn)擊兩年前數(shù)據(jù),彈出對話框,提示是否刪除yyyy-mm-dd之前的數(shù)據(jù),點(diǎn)擊確定彈出清理成功!

點(diǎn)擊關(guān)于,顯示版本等信息:

點(diǎn)擊主界面進(jìn)貨,進(jìn)入進(jìn)貨管理界面,輸入我們進(jìn)貨的信息:品名(商品名稱)、數(shù)量、單位(銷售單位,不支持單位換算)、總價(jià)(該數(shù)量下的總金額),錄入如下信息

?

點(diǎn)擊提交按鈕,提示成功!(如果商品之前有記錄過,輸入商品名后,按Enter鍵,會(huì)自動(dòng)帶出商品單位)。 我們繼續(xù)多輸入一些其他商品。。。

?

點(diǎn)擊定價(jià),進(jìn)入定價(jià)界面,界面左邊兩個(gè)按鈕。 查找按鈕:首先選擇日期區(qū)間(系統(tǒng)默認(rèn)一個(gè)月,我們調(diào)整為最近三天),起始和結(jié)束日期,對這一時(shí)間區(qū)間進(jìn)貨的商品指定銷售價(jià)格

?

我們點(diǎn)擊查找按鈕,顯示我們2016/03/20-2016/03/28的所有進(jìn)貨商品,系統(tǒng)自動(dòng)計(jì)算出進(jìn)貨的價(jià)格,以此作為銷售價(jià)格的參考

?

再看左下角的統(tǒng)一按比例加價(jià)(進(jìn)貨單價(jià)*比例)和 統(tǒng)一按固定值加價(jià)(進(jìn)貨單價(jià)+固定值),我們模擬比例加價(jià)25%,點(diǎn)擊確定按鈕,看下銷售單價(jià)變化

?

當(dāng)然銷售單價(jià)可以手動(dòng)改動(dòng),比如我們將雀巢咖啡的價(jià)格修改為22元/盒,直接更改后按Enter即可

確定好銷售價(jià)格后,我們點(diǎn)提交按鈕,提示提交成功!

?

再點(diǎn)擊我們的銷售功能,進(jìn)入銷售管理界面:下拉框直接選擇商品,系統(tǒng)自動(dòng)帶出商品單位,如果上一步你有維護(hù)價(jià)格,系統(tǒng)也會(huì)自動(dòng)帶出銷售價(jià)格,你只需要輸入數(shù)量即可。

當(dāng)然,如果沒有維護(hù)銷售價(jià)格,你可以手動(dòng)輸入單價(jià),單價(jià)可以被手動(dòng)調(diào)整

比如客戶要買薯片2個(gè),選擇百事薯片,系統(tǒng)帶出價(jià)格4.75(可以被修改),輸入數(shù)量2個(gè),點(diǎn)擊>>按鈕,加入購物車,總價(jià)9.5,我們多購買一些商品

?

如果我想將真彩水筆從購物車移除,選中點(diǎn)擊刪除按鈕即可

如果同一商品多次加入購物車,系統(tǒng)會(huì)提示重復(fù)加入,請刪除后,重新輸入數(shù)量再次加入。最后,我們點(diǎn)擊提交按鈕,系統(tǒng)會(huì)計(jì)算購物車的總金額,提示是否提交

點(diǎn)是(Y),顯示提交結(jié)果,交易成功。

?

回到主界面,點(diǎn)擊報(bào)廢功能,如有過期等原因?qū)е聢?bào)廢,這個(gè)功能最實(shí)用了。選擇商品,輸入數(shù)量,按Enter鍵,顯示成本,點(diǎn)擊提交,顯示報(bào)廢成功!

?

再回到主界面,進(jìn)入報(bào)表功能,這部分稍微復(fù)雜點(diǎn),首先看報(bào)表界面。包含三大功能:記錄、庫存、利潤。 在未鎖定日期區(qū)間前,你不能進(jìn)行任何操作。

?

我們選擇最近一個(gè)月的記錄,然后鎖定日期,展開左邊功能節(jié)點(diǎn)——記錄:進(jìn)貨記錄、銷售記錄、報(bào)廢記錄。 ? 庫存:當(dāng)前庫存(與日期區(qū)間無關(guān))。 ?最后是利潤分析(計(jì)算銷售收入、成本之凈利潤。?

?比如我們點(diǎn)擊銷售記錄:

?

如果在實(shí)際中,進(jìn)貨、報(bào)廢、或者銷售錄入出錯(cuò)了,我們都可以在這里鎖定日期,找到記錄然后選中,鼠標(biāo)右鍵選擇刪除記錄

?

點(diǎn)擊刪除記錄,提示刪除的數(shù)據(jù)不加入報(bào)表計(jì)算,確認(rèn)刪除,提示刪除成功

?

我們再看看當(dāng)前剩余商品庫存,幫助核對商品數(shù)量

?

最后是利潤分析,這份報(bào)表十分重要

最后從這份報(bào)表來驗(yàn)證我們的操作結(jié)果:

1.康師傅紅燒牛肉面:銷售了5袋,每袋銷售價(jià)格2.5,進(jìn)貨價(jià)格2.0,銷售利潤2.5,沒有報(bào)廢記錄,凈利潤2.5

2.百事薯片:銷售2份,每份銷售價(jià)格4.75,進(jìn)貨價(jià)格3.8,銷售利潤1.9,其中有一份因?yàn)檫^期導(dǎo)致成本增加3.8,所以凈利潤虧1.9

........

最后總計(jì)銷售利潤和報(bào)廢成本,得到凈利潤

?

好了,功能部分終于說完了。直接貢獻(xiàn)代碼部分,不用多做說明了,本人注釋一向清楚:

?

第二部分:源碼

主界面:

?

package org.screen;import java.awt.event.ActionListener; import java.text.SimpleDateFormat; import java.util.Date; import java.util.GregorianCalendar;import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPopupMenu; import javax.swing.SwingConstants;import java.awt.Color; import java.awt.EventQueue; import java.awt.Font; import java.awt.event.ActionEvent; import javax.swing.JToolBar;public class screen {private JFrame frame;private JButton button_4;private JButton button_5;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() { //允許多次執(zhí)行public void run() {try {screen window = new screen(); //new一個(gè)窗口(全屏)window.frame.setVisible(true); //設(shè)置fram可見} catch (Exception e) {e.printStackTrace();}}});}/*** Create the application.*/public screen() {initialize(); }/*** Initialize the contents of the frame.*/private void initialize() {frame = new JFrame("零售業(yè)管理系統(tǒng)");frame.getContentPane().setFocusTraversalPolicyProvider(true);int screenWidth = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width; //根據(jù)用戶屏幕,自動(dòng)調(diào)整屏幕元素位置int screenHeight = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;frame.setBounds(0, 0, screenWidth, screenHeight); //設(shè)置fram的位置和長寬frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //定義close動(dòng)作frame.getContentPane().setLayout(null); JLabel lblNewLabel = new JLabel("零售業(yè)管理系統(tǒng) V1.0"); //設(shè)置系統(tǒng)標(biāo)題lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);lblNewLabel.setForeground(Color.BLUE);lblNewLabel.setFont(new Font("仿宋", Font.BOLD | Font.ITALIC, 36));lblNewLabel.setBounds((screenWidth-400)/2, (screenHeight-350)/2 - 80, 400, 50);frame.getContentPane().add(lblNewLabel); JButton newButton = new JButton("進(jìn)貨"); //點(diǎn)擊進(jìn)貨按鈕,調(diào)用進(jìn)貨管理程序界面newButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {stock_screen.main(null);}});newButton.setFont(new Font("仿宋", Font.BOLD, 20)); //設(shè)置按鈕字體,大小,位置newButton.setBounds((screenWidth-400)/2, (screenHeight-350)/2, 400, 50);frame.getContentPane().add(newButton); //放入fram容器中JButton button = new JButton("定價(jià)"); //設(shè)置銷售按鈕button.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {pricing_screen.main(null);}});button.setFont(new Font("仿宋", Font.BOLD, 20));button.setBounds((screenWidth-400)/2, (screenHeight-350)/2+60*1, 400, 50);frame.getContentPane().add(button);JButton button_1 = new JButton("銷售"); //設(shè)置報(bào)廢按鈕button_1.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {sales_screen.main(null);}});button_1.setFont(new Font("仿宋", Font.BOLD, 20));button_1.setBounds((screenWidth-400)/2, (screenHeight-350)/2+60*2, 400, 50);frame.getContentPane().add(button_1);JButton button_2 = new JButton("報(bào)廢"); //設(shè)置庫存按鈕button_2.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {waste_screen.main(null);}});button_2.setFont(new Font("仿宋", Font.BOLD, 20));button_2.setBounds((screenWidth-400)/2, (screenHeight-350)/2+60*3, 400, 50);frame.getContentPane().add(button_2);JButton button_3 = new JButton("報(bào)表"); //設(shè)置利潤按鈕button_3.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {report_screen.main(null);}});button_3.setFont(new Font("仿宋", Font.BOLD, 20));button_3.setBounds((screenWidth-400)/2, (screenHeight-350)/2+60*4, 400, 50);frame.getContentPane().add(button_3);JButton btnNewButton = new JButton("其他");btnNewButton.setFont(new Font("仿宋", Font.BOLD, 20));btnNewButton.setFocusPainted(false);btnNewButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {JOptionPane.showMessageDialog(null, "功能暫未開放!");}});btnNewButton.setBounds((screenWidth-400)/2, (screenHeight-350)/2+60*5, 400, 50);frame.getContentPane().add(btnNewButton);//toolbar上增加兩個(gè)按鈕選項(xiàng) 數(shù)據(jù)管理和關(guān)于button_4 = new JButton("數(shù)據(jù)管理");button_4.addActionListener(new ActionListener() {//點(diǎn)擊按鈕事件public void actionPerformed(ActionEvent e) {JPopupMenu popupMenu = new JPopupMenu();//加載popupMenu選項(xiàng)JMenu menu = new JMenu("清空");//清理舊數(shù)據(jù),優(yōu)化數(shù)據(jù)庫JMenuItem item_1 = new JMenuItem("1年前數(shù)據(jù)");JMenuItem item_2 = new JMenuItem("2年前數(shù)據(jù)");JMenuItem item_3 = new JMenuItem("3年前數(shù)據(jù)");JMenuItem item_4 = new JMenuItem("4年前數(shù)據(jù)");JMenuItem item_5 = new JMenuItem("5年前數(shù)據(jù)");menu.add(item_1);menu.add(item_2);menu.add(item_3);menu.add(item_4);menu.add(item_5);popupMenu.add(menu);popupMenu.show(button_4, 0, button_4.getY() + button_4.getHeight());//選擇toolbar里item事件,執(zhí)行舊數(shù)據(jù)清理item_1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {GregorianCalendar gc=new GregorianCalendar(); //當(dāng)前日期的前一年gc.setTime(new Date());gc.add(1, -1); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 設(shè)置指定格式的當(dāng)前日期時(shí)間String date = df.format(gc.getTime()); new OperationSqlData().DeleteSqlDataByYear(date);}});item_2.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {GregorianCalendar gc=new GregorianCalendar(); //當(dāng)前日期的前一年gc.setTime(new Date());gc.add(1, -2); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 設(shè)置指定格式的當(dāng)前日期時(shí)間String date = df.format(gc.getTime()); new OperationSqlData().DeleteSqlDataByYear(date);}});item_3.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {GregorianCalendar gc=new GregorianCalendar(); //當(dāng)前日期的前一年gc.setTime(new Date());gc.add(1, -3); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 設(shè)置指定格式的當(dāng)前日期時(shí)間String date = df.format(gc.getTime()); new OperationSqlData().DeleteSqlDataByYear(date);}});item_4.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {GregorianCalendar gc=new GregorianCalendar(); //當(dāng)前日期的前兩年gc.setTime(new Date());gc.add(1, -4); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 設(shè)置指定格式的當(dāng)前日期時(shí)間String date = df.format(gc.getTime()); new OperationSqlData().DeleteSqlDataByYear(date);}});item_5.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {GregorianCalendar gc=new GregorianCalendar(); //當(dāng)前日期的前一年gc.setTime(new Date());gc.add(1, -5); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 設(shè)置指定格式的當(dāng)前日期時(shí)間String date = df.format(gc.getTime()); new OperationSqlData().DeleteSqlDataByYear(date);}});}});button_4.setBorder(null);button_4.setFont(new Font("仿宋", Font.PLAIN, 14));JToolBar toolBar = new JToolBar();toolBar.setToolTipText("配置");toolBar.setRollover(true);toolBar.add(button_4);toolBar.setBounds(0, 0, screenWidth, 25);frame.getContentPane().add(toolBar);//分隔功能欄toolBar.addSeparator();//加入開發(fā)相關(guān)信息button_5 = new JButton("關(guān)于");button_5.setBorder(null);button_5.setFont(new Font("仿宋", Font.PLAIN, 14));toolBar.add(button_5);frame.getContentPane().add(toolBar);button_5.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent arg0) {about_screen.main(null);}});} }

?

?

主界面——關(guān)于:

?

package org.screen;import java.awt.EventQueue;import javax.swing.JFrame; import javax.swing.JTextPane;public class about_screen {private JFrame frame;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {about_screen window = new about_screen();window.frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the application.*/public about_screen() {initialize();}/*** Initialize the contents of the frame.*/private void initialize() {frame = new JFrame();frame.setBounds(50, 80, 400, 200);JTextPane textPane = new JTextPane();textPane.setBounds(00, 00, 400, 200);frame.getContentPane().add(textPane);textPane.setText("此管理軟件,僅為個(gè)人學(xué)習(xí)Java語言作品之一,源碼全部開放,如有疑問聯(lián)系QQ,初學(xué)Java不足之處還請指正,相互學(xué)習(xí)!"+"\r"+"\r"+ "版本 V1.0"+"\r"+"2016/03/25"+"\r"+"CSDN:居士愛吃泡面"+"\r"+"QQ:461356592");textPane.setEditable(false); }}

?

?

進(jìn)貨管理:

?

?

package org.screen;import java.awt.EventQueue;import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import java.awt.Font; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.ActionEvent;public class stock_screen {private JFrame frame;private JTextField textField;private JTextField textField_1;private JTextField textField_2;private JTextField textField_3;private JButton btnNewButton;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {stock_screen window = new stock_screen(); //創(chuàng)建進(jìn)貨窗口界面window.frame.setVisible(true); //設(shè)置窗口可見} catch (Exception e) {e.printStackTrace();}}});}/*** Create the application.*/public stock_screen() {initialize();}/*** Initialize the contents of the frame.*/private void initialize() {frame = new JFrame("進(jìn)貨管理"); //new一個(gè)Jfram容器int screenWidth = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width/2;; //根據(jù)用戶屏幕,自動(dòng)調(diào)整控件的顯示位置int screenHeight = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height/2;;frame.setBounds(screenWidth/2, screenHeight/2, screenWidth, screenHeight);frame.getContentPane().setLayout(null); JLabel lblNewLabel = new JLabel("品名"); //設(shè)置label控件屬性,位置lblNewLabel.setFont(new Font("仿宋", Font.BOLD, 20));lblNewLabel.setBounds((screenWidth-180)/2, (screenHeight-180)/2, 50, 20);frame.getContentPane().add(lblNewLabel);JLabel lblNewLabel_1 = new JLabel("數(shù)量"); //設(shè)置label控件屬性,位置lblNewLabel_1.setFont(new Font("仿宋", Font.BOLD, 20));lblNewLabel_1.setBounds((screenWidth-180)/2, (screenHeight-180)/2+30*1, 50, 20);frame.getContentPane().add(lblNewLabel_1);JLabel lblNewLabel_2 = new JLabel("單位"); //設(shè)置label控件屬性,位置lblNewLabel_2.setFont(new Font("仿宋", Font.BOLD, 20));lblNewLabel_2.setBounds((screenWidth-180)/2, (screenHeight-180)/2+30*2, 50, 20);frame.getContentPane().add(lblNewLabel_2);JLabel lblNewLabel_3 = new JLabel("總價(jià)"); //設(shè)置label控件屬性,位置lblNewLabel_3.setFont(new Font("仿宋", Font.BOLD, 20));lblNewLabel_3.setBounds((screenWidth-180)/2, (screenHeight-180)/2+30*3, 50, 20);frame.getContentPane().add(lblNewLabel_3);textField = new JTextField(); //設(shè)置輸入Field控件屬性,位置textField.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {textField_2.setText(new OperationSqlData().getUnitByGoodsName(textField.getText()));}});textField.setBounds((screenWidth-180)/2+60, (screenHeight-180)/2, 180, 20);frame.getContentPane().add(textField);textField.setColumns(10);textField_1 = new JTextField(); //設(shè)置輸入Field控件屬性,位置textField_1.setBounds((screenWidth-180)/2+60, (screenHeight-180)/2+30*1, 120, 20);frame.getContentPane().add(textField_1);textField_1.setColumns(10);textField_2 = new JTextField(); //設(shè)置輸入ComboBox控件屬性,位置 ,默認(rèn)下拉框兩個(gè)選項(xiàng)textField_2.setBounds((screenWidth-180)/2+60, (screenHeight-180)/2+30*2, 120, 20);frame.getContentPane().add(textField_2);textField_2.setText(new OperationSqlData().getUnitByGoodsName(textField.getText()));textField_3 = new JTextField(); //設(shè)置輸入Field控件屬性,位置textField_3.setBounds((screenWidth-180)/2+60, (screenHeight-180)/2+30*3, 120, 20);frame.getContentPane().add(textField_3);textField_3.setColumns(10);btnNewButton = new JButton("提交");btnNewButton.setMnemonic(KeyEvent.VK_ENTER);btnNewButton.setFont(new Font("仿宋", Font.BOLD, 16));btnNewButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if (textField.getText().isEmpty()||textField_1.getText().isEmpty()||textField_3.getText().isEmpty()||textField_2.getText().isEmpty()) //檢查商品名字段是否為空{(diào)JOptionPane.showMessageDialog(null, "輸入欄位不能為空!");return;}int ret = new OperationSqlData().StockIn(textField.getText(), textField_1.getText(), (String) textField_2.getText(), textField_3.getText());if (ret == 1) //根據(jù)SQL返回值判斷執(zhí)行結(jié)果,并顯示對話框{JOptionPane.showMessageDialog(null, "存儲(chǔ)成功");textField.setText(null);textField_1.setText(null);textField_2.setText(null);;textField_3.setText(null);}else{JOptionPane.showMessageDialog(null, "存儲(chǔ)失敗!");} }});btnNewButton.setBounds((screenWidth-180)/2+60, (screenHeight-180)/2+30*3+40, 120, 20);frame.getContentPane().add(btnNewButton);} }


定價(jià)界面

?

?

package org.screen;import java.awt.EventQueue;import javax.swing.JFrame; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; import javax.swing.JScrollPane; import javax.swing.JButton; import java.awt.event.ActionListener; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.awt.event.ActionEvent; import javax.swing.JLabel; import javax.swing.JOptionPane;import java.awt.Font; import javax.swing.JTextField; import javax.swing.SpinnerDateModel; import javax.swing.SpinnerModel; import javax.swing.JSpinner;public class pricing_screen {private JFrame frame;private JTable table;private DefaultTableModel model;private JTextField textField;private JTextField textField_1;private JSpinner spinner;private JSpinner spinner_1;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {pricing_screen window = new pricing_screen();window.frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the application.*/public pricing_screen() {initialize();}/*** Initialize the contents of the frame.*/@SuppressWarnings("serial")private void initialize() {frame = new JFrame("銷售定價(jià)");int screenWidth = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width*2/3;int screenHeight = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height*2/3; frame.setBounds(screenWidth/6, screenHeight/6, screenWidth, screenHeight);frame.getContentPane().setLayout(null);// 依據(jù)model創(chuàng)建一個(gè)JtableString[] header = { "品名","總價(jià)值","庫存數(shù)量","單位","進(jìn)貨單價(jià)","銷售單價(jià)" };// 設(shè)置表頭Object[][] item = new Object[0][6];// 設(shè)置單身model = new DefaultTableModel(item, header) {public boolean isCellEditable(int row, int column) {if (column == 5)return true;elsereturn false;}};// 創(chuàng)建一個(gè)model,并設(shè)置model數(shù)據(jù)不可編輯table = new JTable(model);// 根據(jù)model創(chuàng)建一個(gè)Jtabletable.setFocusable(false);// 關(guān)閉鼠標(biāo)選中單個(gè)坐標(biāo)JScrollPane scrollPane = new JScrollPane(table);scrollPane.setBounds((screenWidth-780)/2+170, (screenHeight-440)/2-20, 600, 400);frame.getContentPane().add(scrollPane); JLabel label_4 = new JLabel("起始日期");label_4.setFont(new Font("仿宋", Font.BOLD, 14));label_4.setBounds((screenWidth-780)/2, (screenHeight-440)/2-20, 60, 20);frame.getContentPane().add(label_4);JLabel label_5 = new JLabel("結(jié)束日期");label_5.setFont(new Font("仿宋", Font.BOLD, 14));label_5.setBounds((screenWidth-780)/2, (screenHeight-440)/2-20+30, 60, 20);frame.getContentPane().add(label_5);//定義兩個(gè)spinner用于日期的調(diào)整GregorianCalendar gc=new GregorianCalendar(); //當(dāng)前日期的前一個(gè)月gc.setTime(new Date());gc.add(2, -1); SpinnerModel dateModel=null;dateModel = new SpinnerDateModel(gc.getTime(), null, null,Calendar.DAY_OF_MONTH);spinner = new JSpinner();spinner.setBounds((screenWidth-780)/2+65, (screenHeight-440)/2-20, 90, 20);frame.getContentPane().add(spinner);spinner.setModel(dateModel); spinner.setEditor(new JSpinner.DateEditor(spinner,"yyyy-MM-dd"));SpinnerModel dateModel_1=null;dateModel_1 = new SpinnerDateModel(new Date(), null, null,Calendar.DAY_OF_MONTH); spinner_1 = new JSpinner();spinner_1.setBounds((screenWidth-780)/2+65, (screenHeight-440)/2-20+30, 90, 20);frame.getContentPane().add(spinner_1);spinner_1.setModel(dateModel_1); spinner_1.setEditor(new JSpinner.DateEditor(spinner_1,"yyyy-MM-dd")); JButton button_1 = new JButton("查找");button_1.setFont(new Font("仿宋", Font.BOLD, 16));button_1.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {//點(diǎn)擊查找按鈕,根據(jù)起止日期,查詢庫存信息int r = model.getRowCount();while(r>0){model.removeRow(r-1);r--;}Object[] obj = new Object[4];SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 創(chuàng)建指定格式的當(dāng)前時(shí)間ArrayList<Object[]> list = new OperationSqlData().getStockRecordByDate(df.format(spinner.getValue())+" "+"00:00:00", df.format(spinner_1.getValue())+" "+"23:59:59",1 , true);int row = list.size();float cost = 0;//顯示到tablefor(int i=0; i<row; i++){obj = list.get(i);model.addRow(new Object[5]);int j = model.getRowCount();model.setValueAt(obj[0], j-1, 0);model.setValueAt(obj[3], j-1, 1);model.setValueAt(obj[1], j-1, 2);model.setValueAt(obj[2], j-1, 3);cost = Float.parseFloat(obj[3].toString())/Float.parseFloat(obj[1].toString());cost = (float)(Math.round(cost*100))/100;model.setValueAt(cost, j-1, 4);}}});button_1.setBounds((screenWidth-780)/2+30, (screenHeight-440)/2-20+70, 90, 20);frame.getContentPane().add(button_1); //定義兩種加價(jià)方式按比例和固定值,基于日期區(qū)間庫存成本定義銷售價(jià)格JLabel lblNewLabel = new JLabel("統(tǒng)一按比例加價(jià)");lblNewLabel.setFont(new Font("仿宋", Font.BOLD, 14));lblNewLabel.setBounds((screenWidth-780)/2-20, (screenHeight-440)/2-20+190, 110, 20);frame.getContentPane().add(lblNewLabel);JLabel label_1 = new JLabel("%");label_1.setBounds((screenWidth-780)/2+150, (screenHeight-440)/2-20+190, 20, 20);frame.getContentPane().add(label_1);JLabel label_2 = new JLabel("統(tǒng)一按固定值加價(jià)");label_2.setFont(new Font("仿宋", Font.BOLD, 14));label_2.setBounds((screenWidth-780)/2-20, (screenHeight-440)/2-20+220, 120, 20);frame.getContentPane().add(label_2);JLabel label_3 = new JLabel("¥");label_3.setBounds((screenWidth-780)/2+150, (screenHeight-440)/2-20+220, 20, 20);frame.getContentPane().add(label_3); textField = new JTextField();textField.setBounds((screenWidth-780)/2+105, (screenHeight-440)/2-20+190, 40, 20);frame.getContentPane().add(textField);textField.setColumns(10); textField_1 = new JTextField();textField_1.setBounds((screenWidth-780)/2+105, (screenHeight-440)/2-20+220, 40, 20);frame.getContentPane().add(textField_1);textField_1.setColumns(10);JButton btnNewButton = new JButton("確定");btnNewButton.setFont(new Font("仿宋", Font.BOLD, 16));btnNewButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {//兩種加價(jià)方式選其一if (!textField.getText().isEmpty() && !textField_1.getText().isEmpty()){JOptionPane.showMessageDialog(null, "加價(jià)方式(按比例/按固定值)二選一");return;}else if(!textField.getText().isEmpty()){int row = 0;row = model.getRowCount();for(int i=0; i<row; i++){//計(jì)算銷售價(jià)格:成本*比例 保留兩位小數(shù)的另一種方法float sales = Float.parseFloat(model.getValueAt(i, 4).toString())*(1+Float.parseFloat(textField.getText())/100);sales = (float)(Math.round(sales*100))/100;model.setValueAt(sales, i, 5);}}else if(!textField_1.getText().isEmpty()){int row;row = model.getRowCount();for(int i=0; i<row; i++){//計(jì)算銷售價(jià)格:成本+定值float sales = Float.parseFloat(model.getValueAt(i, 4).toString())+Float.parseFloat(textField_1.getText());sales = (float)(Math.round(sales*100))/100;model.setValueAt(sales, i, 5);} }}});btnNewButton.setBounds((screenWidth-780)/2+30, (screenHeight-440)/2-20+260, 90, 20);frame.getContentPane().add(btnNewButton); JButton button = new JButton("提交");button.setFont(new Font("仿宋", Font.BOLD, 16));button.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {new OperationSqlData().CommitSalesStockPricingToSql(model, table);}});button.setBounds((screenWidth-780)/2+400, (screenHeight-440)/2+400, 90, 20);frame.getContentPane().add(button);} }


銷售管理界面:

?

?

package org.screen;import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.table.DefaultTableModel; import javax.swing.JComboBox; import javax.swing.JTextField; import javax.swing.JTable; import javax.swing.JScrollPane; import java.awt.Font; import javax.swing.SwingConstants; import java.awt.event.KeyEvent;public class sales_screen {// 定義SWT控件private JFrame frame;private JTextField textField;private JTextField textField_1;private JTextField textField_3;private JComboBox<String> comboBox;private JTable table_1;private DefaultTableModel model;private int m = 0;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {sales_screen window = new sales_screen(); // 新建銷售窗口window.frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the application.*/public sales_screen() {initialize();}/*** Initialize the contents of the frame.*/@SuppressWarnings("serial")private void initialize() {frame = new JFrame("銷售管理"); // new一個(gè)fram容器// 自動(dòng)設(shè)置銷售管理窗口大小為用戶屏幕的一半,切位于屏幕中央int screenWidth = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width*2/3;int screenHeight = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height*2/3;frame.setBounds(screenWidth/6, screenHeight/6, screenWidth, screenHeight);frame.getContentPane().setLayout(null);JLabel lblNewLabel = new JLabel("品名");// 設(shè)置品名label屬性lblNewLabel.setFont(new Font("仿宋", Font.BOLD, 20));lblNewLabel.setBounds((screenWidth - 880) / 2, (screenHeight - 400) / 2+100, 50, 20);frame.getContentPane().add(lblNewLabel);JLabel lblNewLabel_1 = new JLabel("數(shù)量");// 設(shè)置單價(jià)label屬性lblNewLabel_1.setFont(new Font("仿宋", Font.BOLD, 20));lblNewLabel_1.setBounds((screenWidth - 880) / 2, (screenHeight - 400) / 2+140 , 50, 20);frame.getContentPane().add(lblNewLabel_1);JLabel lblNewLabel_2 = new JLabel("單價(jià)");// 設(shè)置數(shù)量label屬性lblNewLabel_2.setFont(new Font("仿宋", Font.BOLD, 20));lblNewLabel_2.setBounds((screenWidth - 880) / 2, (screenHeight - 400) / 2+180 , 50, 20);frame.getContentPane().add(lblNewLabel_2);textField_3 = new JTextField();textField_3.setHorizontalAlignment(SwingConstants.CENTER);textField_3.setEditable(false);textField_3.setBounds((screenWidth - 880) / 2 + 170, (screenHeight - 400) / 2 + 140, 60, 20);;frame.getContentPane().add(textField_3);textField_3.setColumns(10); comboBox = new JComboBox<String>();// 設(shè)置combox品名輸入屬性new OperationSqlData().getGoodsNameToCombox(comboBox);comboBox.setBounds((screenWidth - 880) / 2 + 55, (screenHeight - 400) / 2 + 100, 180, 20);frame.getContentPane().add(comboBox);comboBox.setSelectedItem(null);comboBox.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {String unit = new OperationSqlData().getUnitByGoodsName(comboBox.getSelectedItem().toString());String sales_price = new OperationSqlData().getSalesPriceByGoodsName(comboBox.getSelectedItem().toString()); textField.setText(sales_price);textField_3.setText(unit);textField_1.setText(null);}});textField_1 = new JTextField();textField_1.setBounds((screenWidth - 880) / 2 + 55, (screenHeight - 400) / 2 + 140, 110, 20);frame.getContentPane().add(textField_1);textField_1.setColumns(10);textField = new JTextField();// 設(shè)置(數(shù)量)Field輸入屬性textField.setBounds((screenWidth - 880) / 2 + 55, (screenHeight - 400) / 2 + 180, 110, 20);frame.getContentPane().add(textField);textField.setColumns(10);JButton btnNewButton_2 = new JButton(">>");// 設(shè)置>>按鈕屬性及事件處理btnNewButton_2.setFont(new Font("仿宋", Font.BOLD, 16));btnNewButton_2.setBounds((screenWidth - 880) / 2 + 55, (screenHeight - 400) / 2 + 220, 60, 20);frame.getContentPane().add(btnNewButton_2);btnNewButton_2.addActionListener(new ActionListener() {// 將用戶輸入的數(shù)據(jù),寫入Jtable中,計(jì)算總金額public void actionPerformed(ActionEvent arg0) {m = model.getRowCount();for(int q=0; q<m; q++){if(model.getValueAt(q, 0).toString().equals(comboBox.getSelectedItem().toString())){JOptionPane.showMessageDialog(null, "重復(fù)加入購物車!");return;}}if (comboBox.getSelectedItem().toString().isEmpty()||textField.getText().isEmpty()||textField_1.getText().isEmpty()){JOptionPane.showMessageDialog(null, "輸入欄位不能為空!");return;}model.addRow(new Object[4]);model.setValueAt(comboBox.getSelectedItem(), m, 0);model.setValueAt(textField_1.getText(), m, 1);model.setValueAt(textField_3.getText(), m, 2);model.setValueAt(textField.getText(), m, 3);model.setValueAt((float)(Math.round(Float.parseFloat(textField.getText()) * Float.parseFloat(textField_1.getText())*100))/100, m, 4);}}); JButton btnNewButton = new JButton("提交");// 設(shè)置提交按鈕屬性及事件處理btnNewButton.setFont(new Font("仿宋", Font.BOLD, 16));btnNewButton.setMnemonic(KeyEvent.VK_ENTER);btnNewButton.setBounds((screenWidth - 880) / 2 + 450, (screenHeight - 400) / 2 + 390, 80, 20);frame.getContentPane().add(btnNewButton);btnNewButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {new OperationSqlData().CommitSalesToSql(model, table_1);int j = model.getRowCount();while(j>0){model.removeRow(j-1);j--;}}});JButton btnNewButton_1 = new JButton("刪除");// 設(shè)置刪除按鈕屬性及事件處理btnNewButton_1.setFont(new Font("仿宋", Font.BOLD, 16));btnNewButton_1.setBounds((screenWidth - 880) / 2 + 580, (screenHeight - 400) / 2 + 390, 80, 20);frame.getContentPane().add(btnNewButton_1);btnNewButton_1.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {int row = table_1.getSelectedRow();if (row >= 0) {model.removeRow(row);}}});// 依據(jù)model創(chuàng)建一個(gè)JtableString[] header = { "品名", "數(shù)量", "單位", "單價(jià)", "總價(jià)" };// 設(shè)置表頭Object[][] item = new Object[0][5];// 設(shè)置單身model = new DefaultTableModel(item, header) {public boolean isCellEditable(int row, int column) {return false;}};// 創(chuàng)建一個(gè)model,并設(shè)置model數(shù)據(jù)不可編輯table_1 = new JTable(model);// 根據(jù)model創(chuàng)建一個(gè)Jtabletable_1.setFocusable(false);// 關(guān)閉鼠標(biāo)選中單個(gè)坐標(biāo)JScrollPane scrollPane = new JScrollPane(table_1);// 設(shè)置JScrollPane容器屬性,將Jtable放入JScrollPane中,實(shí)現(xiàn)數(shù)據(jù)超出上下左右滾動(dòng)條的功能scrollPane.setLocation((screenWidth - 880) / 2 + 250, (screenHeight - 440) / 2);scrollPane.setSize(600, 400);frame.getContentPane().add(scrollPane);} }


報(bào)廢界面:

?

?

package org.screen;import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.JComboBox; import java.awt.Font; import javax.swing.SwingConstants;import javax.swing.JButton; import java.awt.event.ActionListener; import java.math.BigDecimal; import java.math.RoundingMode; import java.awt.event.ActionEvent;public class waste_screen {private JFrame frame;private JComboBox<String> comboBox;private JTextField textField_1;private JTextField textField_2;private JButton btnNewButton;private JTextField textField_3;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {waste_screen window = new waste_screen(); //創(chuàng)建進(jìn)貨窗口界面window.frame.setVisible(true); //設(shè)置窗口可見} catch (Exception e) {e.printStackTrace();}}});}/*** Create the application.*/public waste_screen() {initialize();}/*** Initialize the contents of the frame.*/private void initialize() {frame = new JFrame("商品報(bào)廢"); //new一個(gè)Jfram容器int screenWidth = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width/2; //根據(jù)用戶屏幕,自動(dòng)調(diào)整控件的顯示位置int screenHeight = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height/2; frame.setBounds(screenWidth/2, screenHeight/2, screenWidth, screenHeight);frame.getContentPane().setLayout(null);JLabel lblNewLabel = new JLabel("品名"); //設(shè)置label控件屬性,位置lblNewLabel.setFont(new Font("仿宋", Font.BOLD, 20));lblNewLabel.setBounds((screenWidth-180)/2, (screenHeight-140)/2, 50, 20);frame.getContentPane().add(lblNewLabel);JLabel lblNewLabel_1 = new JLabel("數(shù)量"); //設(shè)置label控件屬性,位置lblNewLabel_1.setFont(new Font("仿宋", Font.BOLD, 20));lblNewLabel_1.setBounds((screenWidth-180)/2, (screenHeight-140)/2+40*1, 50, 20);frame.getContentPane().add(lblNewLabel_1);JLabel lblNewLabel_2 = new JLabel("成本"); //設(shè)置label控件屬性,位置lblNewLabel_2.setFont(new Font("仿宋", Font.BOLD, 20));lblNewLabel_2.setBounds((screenWidth-180)/2, (screenHeight-140)/2+40*2, 50, 20);frame.getContentPane().add(lblNewLabel_2);comboBox = new JComboBox<String>(); //設(shè)置comboBox控件屬性,位置new OperationSqlData().getGoodsNameToCombox(comboBox);comboBox.setSelectedItem(null);comboBox.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {//每次選擇商品,先清空輸入框,再抓取商品單位textField_1.setText(null);textField_2.setText(null);;textField_3.setText(null); String unit = new OperationSqlData().getUnitByGoodsName(comboBox.getSelectedItem().toString());textField_3.setText(unit);}});comboBox.setBounds((screenWidth-180)/2+60, (screenHeight-140)/2, 180, 20);frame.getContentPane().add(comboBox);textField_1 = new JTextField(); //設(shè)置輸入Field控件屬性,位置textField_1.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if (comboBox.getSelectedIndex() >= 0){//抓取報(bào)廢商品的庫存成本float price = new OperationSqlData().getStockCostByGoodsName(comboBox.getSelectedItem().toString());float t_price = Float.parseFloat(textField_1.getText())*price;//保留兩位小數(shù)BigDecimal bd = new BigDecimal(t_price);bd = bd.setScale(2, RoundingMode.HALF_UP);textField_2.setText(bd.toString());}}});textField_1.setBounds((screenWidth-180)/2+60, (screenHeight-140)/2+40*1, 120, 20);frame.getContentPane().add(textField_1);textField_1.setColumns(10);textField_2 = new JTextField();textField_2.setEditable(false);textField_2.setBounds((screenWidth-180)/2+60, (screenHeight-140)/2+40*2, 120, 20);frame.getContentPane().add(textField_2);textField_3 = new JTextField();textField_3.setHorizontalAlignment(SwingConstants.CENTER);textField_3.setEditable(false);textField_3.setBounds((screenWidth-180)/2+190, (screenHeight-140)/2+40*1, 50, 20);frame.getContentPane().add(textField_3);textField_3.setColumns(10); btnNewButton = new JButton("提交"); //設(shè)置提交按鈕屬性,位置和提交事件處理btnNewButton.setFont(new Font("仿宋", Font.BOLD, 16));btnNewButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if (textField_1.getText().isEmpty()||comboBox.getSelectedIndex()<0) //檢查商品名字段是否為空{(diào)JOptionPane.showMessageDialog(null, "輸入欄位不能為空!");return;}if (textField_2.getText().isEmpty()) //檢查商品名字段是否為空{(diào)JOptionPane.showMessageDialog(null, "先按Enter查看報(bào)廢成本!");return;} int ret =new OperationSqlData().StockIn(comboBox.getSelectedItem().toString(), "-"+textField_1.getText(), textField_3.getText(), "-"+textField_2.getText());if (ret == 1) //根據(jù)SQL返回值判斷執(zhí)行結(jié)果,并顯示對話框{JOptionPane.showMessageDialog(null, "報(bào)廢成功!");}else{JOptionPane.showMessageDialog(null, "報(bào)廢失敗!");}}});btnNewButton.setBounds((screenWidth-180)/2+60, (screenHeight-140)/2+40*3, 120, 20);frame.getContentPane().add(btnNewButton);} }


報(bào)表部分:

?

?

package org.screen;import java.awt.EventQueue; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar;import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.SpinnerDateModel; import javax.swing.SpinnerModel; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.table.DefaultTableModel; import javax.swing.JScrollPane; import javax.swing.tree.DefaultTreeModel;import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.JTable; import javax.swing.JSpinner; import javax.swing.JLabel; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPopupMenu; import javax.swing.JRadioButton; import java.awt.Font; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.ActionEvent;public class report_screen {private JFrame frame;private JTable table;private JTree tree;private JScrollPane scrollPane;private JScrollPane scrollPane_1;private JSpinner spinner;private JSpinner spinner_1; private JRadioButton checkBox;private String business;private DefaultTableModel model1;private DefaultTableModel model2;private DefaultTableModel model3;/*** Launch the application.*/public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {report_screen window = new report_screen();window.frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/*** Create the application.*/public report_screen() {initialize();}/*** Initialize the contents of the frame.*/@SuppressWarnings("serial")private void initialize() {frame = new JFrame();int screenWidth = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;int screenHeight = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height; frame.setBounds(0, 0, screenWidth, screenHeight);frame.getContentPane().setLayout(null);JLabel label = new JLabel("開始日期");label.setFont(new Font("仿宋", Font.BOLD, 16));label.setBounds(180, 20, 80, 20);frame.getContentPane().add(label); JLabel label_1 = new JLabel("結(jié)束日期");label_1.setFont(new Font("仿宋", Font.BOLD, 16));label_1.setBounds(390, 20, 80, 20);frame.getContentPane().add(label_1); checkBox = new JRadioButton("鎖定日期");checkBox.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {//鎖定后日期不可編輯if(checkBox.isSelected() == true){spinner.setEnabled(false);spinner_1.setEnabled(false);}else{spinner.setEnabled(true);spinner_1.setEnabled(true);}}});checkBox.setFont(new Font("仿宋", Font.PLAIN, 16));checkBox.setBounds(600, 20, 100, 20);frame.getContentPane().add(checkBox);checkBox.setSelected(false);GregorianCalendar gc=new GregorianCalendar(); //當(dāng)前日期的前一個(gè)月gc.setTime(new Date());gc.add(2, -1); SpinnerModel dateModel=null;dateModel = new SpinnerDateModel(gc.getTime(), null, null,Calendar.DAY_OF_MONTH);spinner = new JSpinner();spinner.setFont(new Font("仿宋", Font.BOLD, 14));spinner.setBounds(270, 20, 105, 20);frame.getContentPane().add(spinner);spinner.setModel(dateModel); spinner.setEditor(new JSpinner.DateEditor(spinner,"yyyy-MM-dd")); SpinnerModel dateModel_1=null;dateModel_1 = new SpinnerDateModel(new Date(), null, null,Calendar.DAY_OF_MONTH);spinner_1 = new JSpinner();spinner_1.setFont(new Font("仿宋", Font.BOLD, 14));spinner_1.setBounds(480, 20, 105, 20);frame.getContentPane().add(spinner_1);spinner_1.setModel(dateModel_1); spinner_1.setEditor(new JSpinner.DateEditor(spinner_1,"yyyy-MM-dd")); //new一個(gè)樹狀導(dǎo)航,選擇不同的報(bào)表種類tree = new JTree();tree.setShowsRootHandles(true);tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("報(bào)表") {{DefaultMutableTreeNode node_1;node_1 = new DefaultMutableTreeNode("記錄");node_1.add(new DefaultMutableTreeNode("進(jìn)貨記錄")); node_1.add(new DefaultMutableTreeNode("銷售記錄"));node_1.add(new DefaultMutableTreeNode("報(bào)廢記錄"));add(node_1);node_1 = new DefaultMutableTreeNode("庫存");node_1.add(new DefaultMutableTreeNode("當(dāng)前庫存"));add(node_1);node_1 = new DefaultMutableTreeNode("利潤");node_1.add(new DefaultMutableTreeNode("利潤統(tǒng)計(jì)"));add(node_1);}}));tree.addTreeSelectionListener(new TreeSelectionListener() {@Overridepublic void valueChanged(TreeSelectionEvent e) {DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();if(node == null)return;if (checkBox.isSelected()==false) //日期鎖定后允許執(zhí)行報(bào)表{JOptionPane.showMessageDialog(null, "請首先鎖定日期");return; }Object select_node = node.getUserObject();SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 創(chuàng)建指定格式的當(dāng)前時(shí)間switch (select_node.toString()) {case "進(jìn)貨記錄": //根據(jù)日期抓取進(jìn)貨記錄String[] header1 = { "日期", "品名", "進(jìn)貨數(shù)量", "單位", "進(jìn)貨單價(jià)", "進(jìn)貨總價(jià)"};// 設(shè)置表頭Object[][] item1 = new Object[0][6];// 設(shè)置單身model1 = new DefaultTableModel(item1, header1) {public boolean isCellEditable(int row, int column) {return false;}};// 創(chuàng)建一個(gè)model,并設(shè)置model數(shù)據(jù)不可編輯table = new JTable(model1);// 根據(jù)model創(chuàng)建一個(gè)Jtabletable.setFocusable(false);// 關(guān)閉鼠標(biāo)選中單個(gè)坐標(biāo) scrollPane_1.setViewportView(table);int r1 = model1.getRowCount();while(r1>0){model1.removeRow(r1-1);r1--;}Object[] obj1 = new Object[6];ArrayList<Object[]> list1 = new OperationSqlData().getStockRecordByDate(df.format(spinner.getValue())+" "+"00:00:00", df.format(spinner_1.getValue())+" "+"23:59:59",0 , false);int row1 = list1.size();float cost = 0;for(int i=0; i<row1; i++){obj1 = list1.get(i);model1.addRow(new Object[6]);int j = model1.getRowCount();model1.setValueAt(obj1[0], j-1, 0);model1.setValueAt(obj1[1], j-1, 1);model1.setValueAt(obj1[2], j-1, 2);model1.setValueAt(obj1[3], j-1, 3);cost = Float.parseFloat(obj1[4].toString())/Float.parseFloat(obj1[2].toString());cost = (float)(Math.round(cost*100))/100;model1.setValueAt(cost, j-1, 4);model1.setValueAt(obj1[4], j-1, 5);} business = "stock";reportMouseClick();break;case "銷售記錄"://根據(jù)日期抓取銷售記錄 String[] header2 = { "日期", "品名", "銷售數(shù)量", "單位", "銷售單價(jià)", "銷售總價(jià)"};// 設(shè)置表頭Object[][] item2 = new Object[0][6];// 設(shè)置單身model2 = new DefaultTableModel(item2, header2) {public boolean isCellEditable(int row, int column) {return false;}};// 創(chuàng)建一個(gè)model,并設(shè)置model數(shù)據(jù)不可編輯table = new JTable(model2);// 根據(jù)model創(chuàng)建一個(gè)Jtabletable.setFocusable(false);// 關(guān)閉鼠標(biāo)選中單個(gè)坐標(biāo) scrollPane_1.setViewportView(table);int r2 = model2.getRowCount();while(r2>0){model2.removeRow(r2-1);r2--;}Object[] obj2 = new Object[6];ArrayList<Object[]> list2 = new OperationSqlData().getSalesRecordByDate(df.format(spinner.getValue())+" "+"00:00:00", df.format(spinner_1.getValue())+" "+"23:59:59", false);int row2 = list2.size();for(int i=0; i<row2; i++){obj2 = list2.get(i);model2.addRow(new Object[6]);int j = model2.getRowCount();model2.setValueAt(obj2[0], j-1, 0);model2.setValueAt(obj2[1], j-1, 1);model2.setValueAt(obj2[2], j-1, 2);model2.setValueAt(obj2[3], j-1, 3);model2.setValueAt(obj2[4], j-1, 4);model2.setValueAt(obj2[5], j-1, 5);}business = "sales";reportMouseClick();break;case "報(bào)廢記錄": //根據(jù)日期抓取報(bào)廢記錄String[] header3 = { "日期", "品名", "報(bào)廢數(shù)量", "單位", "成本單價(jià)", "報(bào)廢總成本"};// 設(shè)置表頭Object[][] item3 = new Object[0][6];// 設(shè)置單身model3 = new DefaultTableModel(item3, header3) {public boolean isCellEditable(int row, int column) {return false;}};// 創(chuàng)建一個(gè)model,并設(shè)置model數(shù)據(jù)不可編輯table = new JTable(model3);// 根據(jù)model創(chuàng)建一個(gè)Jtabletable.setFocusable(false);// 關(guān)閉鼠標(biāo)選中單個(gè)坐標(biāo) scrollPane_1.setViewportView(table);int r3 = model3.getRowCount();while(r3>0){model3.removeRow(r3-1);r3--;}Object[] obj3 = new Object[6];ArrayList<Object[]> list3 = new OperationSqlData().getStockRecordByDate(df.format(spinner.getValue())+" "+"00:00:00", df.format(spinner_1.getValue())+" "+"23:59:59",-1 , false);int row3 = list3.size();float cost1 = 0;for(int i=0; i<row3; i++){obj3 = list3.get(i);model3.addRow(new Object[6]);int j = model3.getRowCount();model3.setValueAt(obj3[0], j-1, 0);model3.setValueAt(obj3[1], j-1, 1);model3.setValueAt(obj3[2], j-1, 2);model3.setValueAt(obj3[3], j-1, 3);cost1 = Float.parseFloat(obj3[4].toString())/Float.parseFloat(obj3[2].toString());model3.setValueAt(cost1, j-1, 4);model3.setValueAt(obj3[4], j-1, 5);}business = "stock_waste";reportMouseClick();break;case "當(dāng)前庫存": //根據(jù)日期抓取進(jìn)貨報(bào)廢和銷售記錄,計(jì)算當(dāng)前庫存String[] header4 = {"品名", "庫存數(shù)量", "單位"};// 設(shè)置表頭Object[][] item4 = new Object[0][3];// 設(shè)置單身DefaultTableModel model4 = new DefaultTableModel(item4, header4) {public boolean isCellEditable(int row, int column) {return false;}};// 創(chuàng)建一個(gè)model,并設(shè)置model數(shù)據(jù)不可編輯table = new JTable(model4);// 根據(jù)model創(chuàng)建一個(gè)Jtabletable.setFocusable(false);// 關(guān)閉鼠標(biāo)選中單個(gè)坐標(biāo) scrollPane_1.setViewportView(table);int r4 = model4.getRowCount();while(r4>0){model4.removeRow(r4-1);r4--;}Object[] obj4 = new Object[6];Object[] obj4_sales = new Object[4];ArrayList<Object[]> list4 = new OperationSqlData().getStockRecordByDate(df.format(spinner.getValue())+" "+"00:00:00", df.format(spinner_1.getValue())+" "+"23:59:59",0 , true);ArrayList<Object[]> list4_sales = new OperationSqlData().getSalesRecordByDate("0000-00-00 00:00:00", "9999-99-99 99:99:99", true);int row4 = list4.size();int row4_sales = list4_sales.size();float stock_last = 0;for(int i=0; i<row4; i++){model4.addRow(new Object[3]);int j = model4.getRowCount();obj4 = list4.get(i);for (int k=0; k<row4_sales; k++){obj4_sales = list4_sales.get(k);if (obj4_sales[0].toString().equals(obj4[0].toString())){stock_last = Float.parseFloat(obj4[1].toString()) - Float.parseFloat(obj4_sales[1].toString());model4.setValueAt(stock_last, j-1, 1);break;}else {model4.setValueAt(obj4[1], j-1, 1);} }if (row4_sales == 0)model4.setValueAt(obj4[1], j-1, 1);model4.setValueAt(obj4[0], j-1, 0);model4.setValueAt(obj4[2], j-1, 2);} break;case "利潤統(tǒng)計(jì)": //根據(jù)銷售利潤 報(bào)廢成本 統(tǒng)計(jì)最終凈利潤String[] header5 = { "品名", "銷售數(shù)量","報(bào)廢數(shù)量","單位", "平均銷售單價(jià)", "庫存成本單價(jià)", "銷售利潤", "報(bào)廢成本","凈利潤"};// 設(shè)置表頭Object[][] item5 = new Object[0][9];// 設(shè)置單身DefaultTableModel model5 = new DefaultTableModel(item5, header5) {public boolean isCellEditable(int row, int column) {return false;}};// 創(chuàng)建一個(gè)model,并設(shè)置model數(shù)據(jù)不可編輯table = new JTable(model5);// 根據(jù)model創(chuàng)建一個(gè)Jtabletable.setFocusable(false);// 關(guān)閉鼠標(biāo)選中單個(gè)坐標(biāo) scrollPane_1.setViewportView(table); int r5 = model5.getRowCount();while(r5>0){model5.removeRow(r5-1);r5--;}Object[] obj5_sales = new Object[4];Object[] obj5_waste = new Object[9];Object[] obj5 = new Object[9];ArrayList<Object[]> list5_sales = new OperationSqlData().getSalesRecordByDate(df.format(spinner.getValue())+" "+"00:00:00", df.format(spinner_1.getValue())+" "+"23:59:59", true);ArrayList<Object[]> list5_waste = new OperationSqlData().getStockRecordByDate(df.format(spinner.getValue())+" "+"00:00:00", df.format(spinner_1.getValue())+" "+"23:59:59", -1, true);int row5_sales = list5_sales.size();int row5_waste = list5_waste.size();for (int i=0; i<row5_sales; i++){obj5_sales = list5_sales.get(i);float price = new OperationSqlData().getStockCostByGoodsName(obj5_sales[0].toString());price = (float)(Math.round(price*100))/100;obj5[0] = obj5_sales[0];obj5[1] = obj5_sales[1];obj5[2] = 0.00;obj5[3] = obj5_sales[2];obj5[4] = (float)(Math.round((Float.parseFloat(obj5_sales[3].toString())/Float.parseFloat(obj5_sales[1].toString()))*100))/100; obj5[5] = price;obj5[6] = new BigDecimal(Float.parseFloat(obj5[1].toString())*(Float.parseFloat(obj5[4].toString())-price)).setScale(2, RoundingMode.HALF_UP);obj5[7] = 0.00;obj5[8] = obj5[6];for (int j=0; j<row5_waste; j++){obj5_waste = list5_waste.get(j);if (obj5_waste[0].equals(obj5_sales[0])){obj5[2] = obj5_waste[1];obj5[7] = obj5_waste[3];obj5[8] = new BigDecimal(Float.parseFloat(obj5[6].toString()) + Float.parseFloat(obj5[7].toString())).setScale(2, RoundingMode.HALF_UP);list5_waste.remove(j);row5_waste = row5_waste - 1;break;}} model5.addRow(new Object[9]);int m = model5.getRowCount();model5.setValueAt(obj5[0], m-1, 0);model5.setValueAt(obj5[1], m-1, 1);model5.setValueAt(obj5[2], m-1, 2); model5.setValueAt(obj5[3], m-1, 3);model5.setValueAt(obj5[4], m-1, 4);model5.setValueAt(obj5[5], m-1, 5); model5.setValueAt(obj5[6], m-1, 6); model5.setValueAt(obj5[7], m-1, 7);model5.setValueAt(obj5[8], m-1, 8); }if (row5_waste > 0){for(int k=0; k<row5_waste; k++){obj5_waste = list5_waste.get(k); obj5[0] = obj5_waste[0];obj5[1] = 0.00;obj5[2] = obj5_waste[1];obj5[3] = obj5_waste[2];obj5[4] = 0.00;float price = new OperationSqlData().getStockCostByGoodsName(obj5_waste[0].toString());price = (float)(Math.round(price*100))/100;obj5[5] = price;obj5[6] = 0.0;obj5[7] = obj5_waste[3];obj5[8] = Float.parseFloat(obj5_waste[3].toString());model5.addRow(new Object[9]);int m = model5.getRowCount();model5.setValueAt(obj5[0], m-1, 0);model5.setValueAt(obj5[1], m-1, 1);model5.setValueAt(obj5[2], m-1, 2); model5.setValueAt(obj5[3], m-1, 3);model5.setValueAt(obj5[4], m-1, 4);model5.setValueAt(obj5[5], m-1, 5); model5.setValueAt(obj5[6], m-1, 6); model5.setValueAt(obj5[7], m-1, 7);model5.setValueAt(obj5[8], m-1, 8); }}//最后一行插入利潤總計(jì)int row_last = model5.getRowCount();float sales_bft = 0;float stock_cost = 0;float last_bft = 0;for(int p=0; p<row_last; p++){if (!(model5.getValueAt(p, 6)==null)){sales_bft = sales_bft + Float.parseFloat(model5.getValueAt(p, 6).toString());}if (!(model5.getValueAt(p, 7)==null)){stock_cost = stock_cost + Float.parseFloat(model5.getValueAt(p, 7).toString());}if (!(model5.getValueAt(p, 8)==null)){last_bft = last_bft + Float.parseFloat(model5.getValueAt(p, 8).toString());}}sales_bft = (float)(Math.round(sales_bft*100))/100;stock_cost = (float)(Math.round(stock_cost*100))/100;last_bft = (float)(Math.round(last_bft*100))/100;model5.addRow(new Object[9]);model5.setValueAt("總計(jì)", row_last, 0);model5.setValueAt(sales_bft, row_last, 6);model5.setValueAt(stock_cost, row_last, 7);model5.setValueAt(last_bft, row_last, 8);break; default:break;}}});scrollPane = new JScrollPane();scrollPane.setViewportView(tree);scrollPane.setBounds(10, 10, 160, screenHeight-10);frame.getContentPane().add(scrollPane); scrollPane_1 = new JScrollPane();scrollPane_1.setBounds(180, 50, screenWidth-200, screenHeight-50);frame.getContentPane().add(scrollPane_1);}class PopupActionListener implements ActionListener {public void actionPerformed(ActionEvent event) {int answer = JOptionPane.showConfirmDialog(null, "刪除數(shù)據(jù)不加入報(bào)表計(jì)算,確定要?jiǎng)h除選中記錄?", "提交信息",JOptionPane.YES_NO_OPTION);if (answer == 0){int ret = 0;if (business.equals("stock")){ret = new OperationSqlData().DeleteSqlDataByGoodsNameDate(model1.getValueAt(table.getSelectedRow(), 1).toString(), model1.getValueAt(table.getSelectedRow(), 0).toString(), "stock");model1.removeRow(table.getSelectedRow());}else if(business.equals("sales")){ret = new OperationSqlData().DeleteSqlDataByGoodsNameDate(model2.getValueAt(table.getSelectedRow(), 1).toString(), model2.getValueAt(table.getSelectedRow(), 0).toString(), "sales");model2.removeRow(table.getSelectedRow()); }else if (business.equals("stock_waste")){ret = new OperationSqlData().DeleteSqlDataByGoodsNameDate(model3.getValueAt(table.getSelectedRow(), 1).toString(), model3.getValueAt(table.getSelectedRow(), 0).toString(), "stock"); model3.removeRow(table.getSelectedRow());}if (ret == 1)JOptionPane.showMessageDialog(null, "刪除成功");elseJOptionPane.showMessageDialog(null, "刪除失敗"); }}} ActionListener acitonListener = new PopupActionListener(); public void reportMouseClick() //右鍵提供指定行區(qū)域刪除選項(xiàng),處理選項(xiàng)事件{final JPopupMenu jp = new JPopupMenu();final JMenuItem item = jp.add("刪除該條記錄");item.addActionListener(acitonListener);table.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {if (e.getButton() == MouseEvent.BUTTON3) {// 彈出菜單if(table.getSelectedRow() > -1){int y = table.getSelectedRow() * table.getRowHeight();if(e.getY() > y && e.getY() <= y+16){jp.show(table, e.getX(), e.getY());}}}}}); } }

?


最后是數(shù)據(jù)庫操作部分:

?

?

?

package org.screen;import java.net.InetAddress; import java.net.UnknownHostException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date;import javax.swing.JComboBox; import javax.swing.JOptionPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel;public class OperationSqlData {//定義數(shù)據(jù)庫變量 private String url;private String passname;private String password;private String driver;private Statement st;private Connection con;private ResultSet rst; public OperationSqlData() //構(gòu)造數(shù)據(jù)庫鏈接默認(rèn)值{passname = "admin"; //SQL Server登陸賬號(hào)password = "init1234"; //SQL Server登陸密碼driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //驅(qū)動(dòng)加載try {Class.forName(driver);} catch (ClassNotFoundException e) {JOptionPane.showMessageDialog(null, "數(shù)據(jù)庫加載失敗!");e.printStackTrace();}try {url = "jdbc:sqlserver://"+InetAddress.getLocalHost().getHostAddress()+";"+"DatabaseName=goodsData";//URL鏈接} catch (UnknownHostException e) {JOptionPane.showMessageDialog(null, "數(shù)據(jù)庫加載失敗!");e.printStackTrace();} }public OperationSqlData(String p_name, String p_word) //構(gòu)造指定帳號(hào)密碼數(shù)據(jù)庫鏈接{passname = p_name; password = p_word;driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";try {Class.forName(driver);} catch (ClassNotFoundException e) {JOptionPane.showMessageDialog(null, "數(shù)據(jù)庫加載失敗!");e.printStackTrace();}try {url = "jdbc:sqlserver://"+InetAddress.getLocalHost().getHostAddress()+";"+"DatabaseName=goodsData";} catch (UnknownHostException e) {JOptionPane.showMessageDialog(null, "數(shù)據(jù)庫加載失敗!");e.printStackTrace();} }public void getGoodsNameToCombox(JComboBox<String> combox){String sql = "SELECT distinct goods FROM stock where qty > 0";// 數(shù)據(jù)庫取品名// 連接數(shù)據(jù)庫,執(zhí)行查詢語句try {con = DriverManager.getConnection(url, passname, password);con.setAutoCommit(true);st = con.createStatement();rst = st.executeQuery(sql);// 取出的結(jié)果加入combox的Itemwhile (rst.next()) {combox.addItem(rst.getString("goods"));}// 關(guān)閉數(shù)據(jù)庫連接rst.close();st.close();con.close();} catch (SQLException e) {JOptionPane.showMessageDialog(null, "商品名數(shù)據(jù)加載異常! ");e.printStackTrace();} }public String getUnitByGoodsName(String goods) {String unit = null;//從庫存表抓取商品單位String sql = "SELECT distinct unit FROM stock WHERE qty> 0 AND goods = ?";try {con = DriverManager.getConnection(url, passname, password);con.setAutoCommit(true);PreparedStatement ps = con.prepareStatement(sql); //SQL預(yù)處理ps.setString(1, goods); //SQL參數(shù)ResultSet rt = ps.executeQuery(); //執(zhí)行SQLwhile(rt.next()){unit = rt.getString("unit"); //從執(zhí)行結(jié)果中得到商品單位}// 關(guān)閉數(shù)據(jù)庫連接ps.close();rt.close();con.close(); } catch (SQLException e) {JOptionPane.showMessageDialog(null, "商品單位數(shù)據(jù)加載異常! ");e.printStackTrace();}return unit; //返回單位}public String getSalesPriceByGoodsName(String goods){String sales_price = null;//從定價(jià)表中抓取銷售單價(jià)String sql = "SELECT unitprice FROM Price WHERE goods = ?";try {con = DriverManager.getConnection(url, passname, password);con.setAutoCommit(true);PreparedStatement ps = con.prepareStatement(sql);// SQL預(yù)處理ps.setString(1, goods);ResultSet rt = ps.executeQuery();while(rt.next()){sales_price = rt.getString("unitprice");}// 關(guān)閉數(shù)據(jù)庫連接ps.close();rt.close();con.close(); } catch (SQLException e) {JOptionPane.showMessageDialog(null, "銷售單價(jià)數(shù)據(jù)加載異常! ");e.printStackTrace();} return sales_price;}public ArrayList<Object[]> getSalesRecordByDate(String date_begin, String date_end, Boolean sum_qty_totalprice){ArrayList<Object[]> list = new ArrayList<Object[]>(); String sql = null;if (sum_qty_totalprice){ sql = "SELECT goods, SUM(qty) AS s_qty, unit, SUM(totalprice) AS s_totalprice FROM sales where date between ? AND ? GROUP BY goods, unit";//抓取銷售記錄數(shù)據(jù)按商品名和單位匯總}else{sql = "SELECT date, goods, qty, unit, unitprice, totalprice FROM sales where date between ? AND ? ORDER BY date DESC";//抓取銷售記錄數(shù)據(jù)按時(shí)間排序}try {con = DriverManager.getConnection(url, passname, password);con.setAutoCommit(true);PreparedStatement ps = con.prepareStatement(sql);// SQL預(yù)處理ps.setString(1, date_begin); //指定抓取銷售記錄起始時(shí)間ps.setString(2, date_end); //指定抓取銷售記錄終止時(shí)間ResultSet rt = ps.executeQuery();Object[] obj = null;if (sum_qty_totalprice){while(rt.next()){obj = new Object[4];//Object數(shù)組加入arraylistobj[0] = rt.getString("goods");obj[1] = rt.getFloat("s_qty");obj[2] = rt.getString("unit");obj[3] = rt.getFloat("s_totalprice");list.add(obj);} }else{while(rt.next()){obj = new Object[6];obj[0] = rt.getString("date");obj[1] = rt.getString("goods");obj[2] = rt.getFloat("qty");obj[3] = rt.getString("unit");obj[4] = rt.getFloat("unitprice");obj[5] = rt.getFloat("totalprice");list.add(obj);}}// 關(guān)閉數(shù)據(jù)庫連接ps.close();rt.close();con.close();} catch (SQLException e) {JOptionPane.showMessageDialog(null, "銷售記錄數(shù)據(jù)加載異常! ");e.printStackTrace(); }return list; } public float getStockCostByGoodsName(String goods){float stock_cost = 0;// 這里計(jì)算的是商品進(jìn)貨成本(qty>0)String sql = "SELECT goods,SUM(qty) AS s_qty,SUM(amount) AS s_amount FROM stock WHERE goods = ? AND qty>0 GROUP by goods";try {con = DriverManager.getConnection(url, passname, password);con.setAutoCommit(true);PreparedStatement ps = con.prepareStatement(sql);// SQL預(yù)處理ps.setString(1, goods);ResultSet rt = ps.executeQuery();while(rt.next()){float s_amount = rt.getFloat("s_amount");float s_qty = rt.getFloat("s_qty");stock_cost = s_amount/s_qty;}// 關(guān)閉數(shù)據(jù)庫連接ps.close();rt.close();con.close();} catch (SQLException e) {JOptionPane.showMessageDialog(null, "成本數(shù)據(jù)加載異常! ");e.printStackTrace(); }return stock_cost;} public ArrayList<Object[]> getStockRecordByDate(String date_begin, String date_end, int status, Boolean sum_amount_qty){ArrayList<Object[]> list = new ArrayList<Object[]>();String sql = null;//sun_amount_qty是否根據(jù)商品名和單位加總庫存數(shù)量和成本,//status的值: -1計(jì)算報(bào)廢庫存;0計(jì)算總庫存; 1計(jì)算進(jìn)貨庫存if (sum_amount_qty){if (status == -1) sql = "SELECT goods, SUM(qty) AS s_qty, unit, SUM(amount) AS s_amount FROM stock where qty < 0 AND date between ? AND ? GROUP by goods,unit";else if (status == 1)sql = "SELECT goods, SUM(qty) AS s_qty, unit, SUM(amount) AS s_amount FROM stock where qty > 0 AND date between ? AND ? GROUP by goods,unit";else if (status == 0)sql = "SELECT goods, SUM(qty) AS s_qty, unit, SUM(amount) AS s_amount FROM stock where date between ? AND ? GROUP by goods,unit";}else {if(status == -1)sql = "SELECT date, goods, qty, unit, amount FROM stock where qty < 0 AND date between ? AND ? ORDER BY date DESC"; elsesql = "SELECT date, goods, qty, unit, amount FROM stock where qty > 0 AND date between ? AND ? ORDER BY date DESC";}try {con = DriverManager.getConnection(url, passname, password);con.setAutoCommit(true);PreparedStatement ps = con.prepareStatement(sql);// SQL預(yù)處理ps.setString(1, date_begin);ps.setString(2, date_end);ResultSet rt = ps.executeQuery();Object[] obj = null;if (sum_amount_qty) //依據(jù)是否匯總處理輸出結(jié)果{while(rt.next()){obj = new Object[4];obj[0] = rt.getString("goods");obj[1] = rt.getFloat("s_qty");obj[2] = rt.getString("unit");obj[3] = rt.getFloat("s_amount");list.add(obj);}}else {while(rt.next()){obj = new Object[5];obj[0] = rt.getString("date");obj[1] = rt.getString("goods");obj[2] = rt.getFloat("qty");obj[3] = rt.getString("unit");obj[4] = rt.getFloat("amount");list.add(obj);} }// 關(guān)閉數(shù)據(jù)庫連接ps.close();rt.close();con.close();} catch (SQLException e) {JOptionPane.showMessageDialog(null, "庫存數(shù)據(jù)加載異常! ");e.printStackTrace(); }return list;} public void CommitSalesStockPricingToSql(DefaultTableModel model, JTable table) //傳入用model定義的table{//這里先刪除原有定價(jià)記錄,再插入新紀(jì)錄String sql1 = "DELETE FROM price WHERE goods = ? ";String sql2 = "INSERT INTO price (goods, unit, cost, unitprice) VALUES(?,?,?,?)";try {// 設(shè)置數(shù)據(jù)庫鏈接,設(shè)置手動(dòng)提交數(shù)據(jù)con = DriverManager.getConnection(url, passname, password);con.setAutoCommit(false);PreparedStatement ps1 = con.prepareStatement(sql1);// SQL預(yù)處理PreparedStatement ps2 = con.prepareStatement(sql2);// SQL預(yù)處理int line = table.getRowCount();// 循環(huán)每一行,如果有值加入數(shù)據(jù)庫批處理for (int i = 0; i < line; i++) {if(!(model.getValueAt(i, 5)==null)) //首先判斷是否有輸入值,再判斷是否為空值,有值則刪除{if (!(model.getValueAt(i, 5).toString().isEmpty())){ps1.setString(1, (String) model.getValueAt(i, 0));ps1.addBatch();ps2.setString(1, (String) model.getValueAt(i, 0));ps2.setString(2, (String) model.getValueAt(i, 3));ps2.setString(3, model.getValueAt(i, 4).toString());ps2.setString(4, model.getValueAt(i, 5).toString());ps2.addBatch(); }}}ps1.executeBatch();// 執(zhí)行批處理,彈出對話消息,顯示成功失敗con.commit();//關(guān)閉數(shù)據(jù)庫相關(guān)鏈接ps1.close();int[] rt = ps2.executeBatch();// 執(zhí)行批處理,彈出對話消息,顯示成功失敗con.commit();if (rt.length > 0)JOptionPane.showMessageDialog(null, "提交成功!");ps2.close();con.close();} catch (SQLException e) {JOptionPane.showMessageDialog(null, "提交失敗! ");try {con.rollback();} catch (SQLException e1) {e1.printStackTrace();}e.printStackTrace();}} public Integer StockIn(String name, String qty, String unit, String amount){int ret = 0;String sql = "INSERT INTO stock (goods, date, qty, unit, amount) VALUES (?, ?, ?, ?, ?)"; // 定義SQL語句try {con = DriverManager.getConnection(url, passname, password);con.setAutoCommit(true); // 設(shè)置數(shù)據(jù)自動(dòng)提交數(shù)據(jù)庫PreparedStatement ps = con.prepareStatement(sql); // SQL預(yù)處理ps.setString(1, name);// 動(dòng)態(tài)參數(shù)執(zhí)行SQLSimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 創(chuàng)建指定格式的當(dāng)前時(shí)間ps.setString(2, df.format(new Date()));ps.setString(3, qty);ps.setString(4, unit);ps.setString(5, amount);ret = ps.executeUpdate(); // 執(zhí)行SQLps.close(); //關(guān)閉數(shù)據(jù)庫鏈接con.close();return ret;} catch (SQLException e) {e.printStackTrace();return ret;}} public void CommitSalesToSql(DefaultTableModel model, JTable table){// 插入銷售記錄到數(shù)據(jù)庫String sql = "INSERT INTO sales (goods, date, qty, unit, unitprice, totalprice) VALUES (?, ?, ?, ?, ?, ?)";// 批量插入Jtable中數(shù)據(jù)try {// 設(shè)置數(shù)據(jù)庫鏈接,設(shè)置手動(dòng)提交數(shù)據(jù)con = DriverManager.getConnection(url, passname, password);con.setAutoCommit(false);PreparedStatement ps = con.prepareStatement(sql);// SQL預(yù)處理SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 設(shè)置指定格式的當(dāng)前日期時(shí)間String date = df.format(new Date());float f4 = 0;int line = table.getRowCount();// 循環(huán)每一行,如果有值加入數(shù)據(jù)庫批處理for (int i = 0; i < line; i++) {if ((String) model.getValueAt(i, 0) != null) {ps.setString(1, (String) model.getValueAt(i, 0));ps.setString(2, date);float f1 = Float.parseFloat(model.getValueAt(i, 1).toString());ps.setFloat(3, f1);ps.setString(4, (String)model.getValueAt(i, 2));float f2 = Float.parseFloat(model.getValueAt(i, 3).toString());ps.setFloat(5, f2);float f3 = Float.parseFloat(model.getValueAt(i, 4).toString());ps.setFloat(6, f3);ps.addBatch();f4 = f3 + f4;}}f4 = (float)(Math.round(f4*100))/100;// 彈出選擇對話框,計(jì)算總金額,提示是否提交int answer = JOptionPane.showConfirmDialog(null, "總金額" + f4 + "元" + " " + "確認(rèn)提交?", "提交信息",JOptionPane.YES_NO_OPTION);if (answer == 0) {int rst[] = ps.executeBatch();// 執(zhí)行批處理,彈出對話消息,顯示成功失敗if (rst.length > 0) {JOptionPane.showMessageDialog(null, "交易成功");con.commit(); }else if(rst.length == 0){JOptionPane.showMessageDialog(null, "無數(shù)據(jù)");}}//關(guān)閉數(shù)據(jù)庫相關(guān)鏈接ps.close();con.close();} catch (SQLException e) {JOptionPane.showMessageDialog(null, "交易失敗");try {con.rollback();con.close();} catch (SQLException e1) {e1.printStackTrace();}e.printStackTrace();}} public Integer DeleteSqlDataByGoodsNameDate(String goods, String date, String business){String sql = null;int ret = 0;if(business.equals("stock")||business.equals("stock_waste"))sql = "DELETE FROM stock where goods = ? AND date = ?";else if (business.equals("sales"))sql = "DELETE FROM sales where goods = ? AND date = ?";// 連接數(shù)據(jù)庫,執(zhí)行查詢語句try {con = DriverManager.getConnection(url, passname,password);con.setAutoCommit(false); // 設(shè)置數(shù)據(jù)自動(dòng)提交數(shù)據(jù)庫PreparedStatement ps = con.prepareStatement(sql); // SQL預(yù)處理ps.setString(1, goods);ps.setString(2, date);ret = ps.executeUpdate(); // 執(zhí)行SQLcon.commit();ps.close(); //關(guān)閉數(shù)據(jù)庫鏈接con.close();return ret;} catch (SQLException e) {try {con.rollback();} catch (SQLException e1) {e1.printStackTrace();}e.printStackTrace();return ret;} } public void DeleteSqlDataByYear(String date_last)//清理數(shù)據(jù)庫舊記錄{String sql1 = "DELETE FROM stock where date < ?";String sql2 = "DELETE FROM sales where date < ?";int answer = JOptionPane.showConfirmDialog(null, "確認(rèn)要?jiǎng)h除"+date_last+"之前的數(shù)據(jù)? ", "提交信息",JOptionPane.YES_NO_OPTION);if (answer != 0) {return;} // 連接數(shù)據(jù)庫,執(zhí)行查詢語句try {con = DriverManager.getConnection(url, passname,password);con.setAutoCommit(false); // 設(shè)置數(shù)據(jù)不自動(dòng)提交數(shù)據(jù)庫PreparedStatement ps1 = con.prepareStatement(sql1); // SQL預(yù)處理ps1.setString(1, date_last);ps1.executeUpdate(); // 執(zhí)行SQLPreparedStatement ps2 = con.prepareStatement(sql2); // SQL預(yù)處理ps2.setString(1, date_last);ps2.executeUpdate(); // 執(zhí)行SQLcon.commit();ps1.close(); //關(guān)閉數(shù)據(jù)庫鏈接ps2.close();con.close();JOptionPane.showMessageDialog(null, "清除數(shù)據(jù)成功!");} catch (SQLException e) {try {con.rollback(); //失敗回滾} catch (SQLException e1) {e1.printStackTrace();}JOptionPane.showMessageDialog(null, "清除數(shù)據(jù)失敗");e.printStackTrace();} } }

?

?

?

?

?

總結(jié)

以上是生活随笔為你收集整理的JAVA学习作品之销售管理系统V1.0的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。