📄 silostatisticdialog.java~177~
字号:
package mysiloer;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.Vector;
import java.sql.ResultSetMetaData;
import java.text.NumberFormat;
/**
* <p>Title: MySiloer</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: yskey</p>
*
* @author 杨涛
* @version 1.0
*/
public class SiloStatisticDialog
extends JDialog {
JPanel panel1 = new JPanel();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField startField = new JTextField();
JLabel jLabel3 = new JLabel();
JTextField endField = new JTextField();
JButton statisticButt = new JButton();
JButton cancelButt = new JButton();
GridBagLayout gridBagLayout1 = new GridBagLayout();
MainFrame frmae = null;
Connection con = null;
StatisticPanel sPanel = null;
public SiloStatisticDialog(JFrame frame, StatisticPanel panel, boolean modal) {
super(frame, "统计查询对话框", modal);
try {
this.sPanel = panel;
con = panel.dataBase.connection;
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jbInit();
pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2 - 150);
}
catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
panel1.setLayout(gridBagLayout1);
jLabel1.setText("请输入要统计的时间段:");
jLabel2.setText("起始时间:");
startField.setText("");
jLabel3.setText("终止时间:");
endField.setText("");
statisticButt.setText("统计查询");
statisticButt.addActionListener(new
StatisticDialog_statisticButt_actionAdapter(this));
cancelButt.setText("取 消");
cancelButt.addActionListener(new StatisticDialog_cancelButt_actionAdapter(this));
this.setResizable(false);
getContentPane().add(panel1);
panel1.add(jLabel1, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0
, GridBagConstraints.WEST,
GridBagConstraints.NONE,
new Insets(16, 17, 0, 36), 3, 9));
panel1.add(jLabel2, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
, GridBagConstraints.WEST,
GridBagConstraints.NONE,
new Insets(17, 17, 0, 0), 0, 0));
panel1.add(jLabel3, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
, GridBagConstraints.WEST,
GridBagConstraints.NONE,
new Insets(17, 0, 0, 132), 0, 0));
panel1.add(endField, new GridBagConstraints(2, 1, 1, 1, 1.0, 0.0
, GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL,
new Insets(14, 62, 0, 16), 112,
0));
panel1.add(startField, new GridBagConstraints(0, 1, 2, 1, 1.0, 0.0
, GridBagConstraints.WEST,
GridBagConstraints.HORIZONTAL,
new Insets(14, 76, 0, 0), 112,
0));
panel1.add(statisticButt, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
, GridBagConstraints.CENTER, GridBagConstraints.NONE,
new Insets(45, 12, 42, 14), 8, 6));
panel1.add(cancelButt, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0
, GridBagConstraints.CENTER,
GridBagConstraints.NONE,
new Insets(45, 37, 42, 75), 8,
6));
}
public void cancelButt_actionPerformed(ActionEvent e) {
this.setVisible(false);
this.dispose();
}
public void statisticButt_actionPerformed(ActionEvent e) {
try{
Vector showTable = new Vector();
String startTime = startField.getText();
String endTime = endField.getText();
String sql = "select * from "+Type.SILOOUTTABLE+" where 出库时间>'"+startTime+"' and 出库时间<'"+endTime+"'";
Statement sta = con.createStatement();
ResultSet set = sta.executeQuery(sql);
Vector row = new Vector();
Vector colNames = new Vector();
ResultSetMetaData meta = set.getMetaData();
int rowCount = sPanel.tablePanel.table.getRowCount();
int colNum = meta.getColumnCount();
for(int i = 0; i < colNum; i++){
colNames.add(meta.getColumnLabel(i + 1));
}
colNames.insertElementAt("单价", 4);
colNames.add("合计金额");
sPanel.tablePanel.tbModel.setColumnIdentifiers(colNames);
for(int i = 0; i < rowCount; i++){
sPanel.tablePanel.tbModel.removeRow(0);
}
while(set.next()){
row = new Vector();
for(int i = 0; i < colNum; i++){
String temp = set.getString(i + 1);
if(temp.indexOf(" ") > 0){
temp = temp.substring(0, temp.indexOf(" "));
}
row.addElement(temp);
}
showTable.addElement(row);
}
if(showTable.size() <= 0){
startField.setText("");
end
JOptionPane.showMessageDialog(this, "无对应的记录,请确认后重新输入!!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
set.close();
sta.close();
showTable(showTable);
}
catch(SQLException ex){
JOptionPane.showMessageDialog(this, "查询错误!!!", "错误提示", JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
}
}
private void showTable(Vector table){
try {
Vector names = new Vector();
Vector prices = new Vector();
for(int i = 0; i < table.size(); i++){
Vector tempVec = (Vector) table.elementAt(i);
String name = (String)tempVec.elementAt(1);
names.add(name.trim());
}
Statement sta1 = con.createStatement();
for(int i = 0; i < names.size(); i++){
String sql = "select 单价 from "+Type.SILOTABLE+" where 原料名='"+(String)names.elementAt(i)+"'";
ResultSet set1 = sta1.executeQuery(sql);
set1.next();
prices.add(set1.getString(1));
}
NumberFormat format = NumberFormat.getCurrencyInstance();
for(int i = 0; i < prices.size(); i++){
Vector tempRow = (Vector)table.elementAt(i);
long num = Long.parseLong((String)tempRow.elementAt(4));
double price = Double.parseDouble((String)prices.elementAt(i));
String strPrice = format.format(price);
tempRow.insertElementAt(strPrice, 4);
double totalPrice = price * num;
String total = format.format(totalPrice);
tempRow.add(total);
sPanel.tablePanel.tbModel.addRow(tempRow);
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
}
class StatisticDialog_statisticButt_actionAdapter
implements ActionListener {
private SiloStatisticDialog adaptee;
StatisticDialog_statisticButt_actionAdapter(SiloStatisticDialog adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.statisticButt_actionPerformed(e);
}
}
class StatisticDialog_cancelButt_actionAdapter
implements ActionListener {
private SiloStatisticDialog adaptee;
StatisticDialog_cancelButt_actionAdapter(SiloStatisticDialog adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.cancelButt_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -