📄 silooutupdatedialog.java~158~
字号:
package mysiloer;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.border.Border;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.util.Calendar;
import java.sql.Date;
import java.util.Vector;
import java.sql.ResultSetMetaData;
/**
* <p>Title: MySiloer</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: yskey</p>
*
* @author 杨涛
* @version 1.0
*/
public class SiloOutUpdateDialog
extends JDialog {
JPanel mainPanel = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
JPanel radioPanel = new JPanel();
JPanel controtPanel = new JPanel();
JPanel contentPanel = new JPanel();
JRadioButton deleteRadio = new JRadioButton();
JRadioButton createRadio = new JRadioButton();
JLabel jLabel1 = new JLabel();
JButton cancelButt = new JButton();
JButton okButt = new JButton();
JLabel jLabel2 = new JLabel();
static int CREATE = 0, DELETE = 1;
int action = 2;
JLabel[] labs = {
new JLabel(), new JLabel(), new JLabel(), new JLabel(),
new JLabel(), new JLabel(), new JLabel()};
JTextField[] params = {
new JTextField(), new JTextField(), new JTextField(),
new JTextField(), new JTextField(), new JTextField()};
GridLayout gridLayout1 = new GridLayout();
ButtonGroup buttonGroup1 = new ButtonGroup();
Border border1 = BorderFactory.createEmptyBorder(0, 10, 5, 10);
Border border2 = BorderFactory.createEmptyBorder(5, 10, 5, 10);
Border border3 = BorderFactory.createEmptyBorder(5, 10, 10, 10);
Connection con = null;
float totalQuantity = 0;
SalePanel parentPanel = null;
Toolkit kit = Toolkit.getDefaultToolkit();
public SiloOutUpdateDialog(Frame owner, SalePanel parent, boolean modal) {
super(owner, "材料出库更新对话框", modal);
try {
this.con = parent.dataBase.getConnection();
this.parentPanel = parent;
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 {
mainPanel.setLayout(borderLayout1);
deleteRadio.setText("删除记录");
deleteRadio.addActionListener(new
SiloOutUpdateDialog_deleteRadio_actionAdapter(this));
createRadio.setText("添加新记录");
createRadio.addActionListener(new
SiloOutUpdateDialog_createRadio_actionAdapter(this));
jLabel1.setText(" ");
cancelButt.setText("取 消");
cancelButt.addActionListener(new
SiloOutUpdateDialog_cancelButt_actionAdapter(this));
okButt.setText("确 定");
okButt.addActionListener(new SiloOutUpdateDialog_okButt_actionAdapter(this));
jLabel2.setText(" ");
contentPanel.setLayout(gridLayout1);
gridLayout1.setHgap(5);
gridLayout1.setVgap(5);
radioPanel.setBorder(border1);
contentPanel.setBorder(border2);
controtPanel.setBorder(border3);
getContentPane().add(mainPanel);
controtPanel.add(okButt);
controtPanel.add(jLabel2);
controtPanel.add(cancelButt);
mainPanel.add(radioPanel, java.awt.BorderLayout.NORTH);
radioPanel.add(createRadio);
radioPanel.add(jLabel1);
radioPanel.add(deleteRadio);
mainPanel.add(contentPanel, java.awt.BorderLayout.CENTER);
mainPanel.add(controtPanel, java.awt.BorderLayout.SOUTH);
buttonGroup1.add(createRadio);
buttonGroup1.add(deleteRadio);
for (int i = 0; i < params.length; i++) {
params[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
okButt_actionPerformed(e);
}
});
}
}
public void createRadio_actionPerformed(ActionEvent e) {
this.action = this.CREATE;
contentPanel.removeAll();
gridLayout1.setColumns(2);
gridLayout1.setRows(4);
contentPanel.setLayout(gridLayout1);
clearFields();
labs[0].setText("原材料名称:");
labs[1].setText("领货人名称:");
labs[2].setText("发货人名称:");
labs[3].setText("货物数量:");
contentPanel.add(labs[0]);
contentPanel.add(params[0]);
contentPanel.add(labs[1]);
contentPanel.add(params[1]);
contentPanel.add(labs[2]);
contentPanel.add(params[2]);
contentPanel.add(labs[3]);
contentPanel.add(params[3]);
mainPanel.add(contentPanel, BorderLayout.CENTER);
validate();
pack();
repaint();
}
public void deleteRadio_actionPerformed(ActionEvent e) {
this.action = this.DELETE;
contentPanel.removeAll();
gridLayout1.setColumns(2);
gridLayout1.setRows(1);
contentPanel.setLayout(gridLayout1);
clearFields();
labs[0].setText("请输入要删除的记录的序号:");
contentPanel.add(labs[0]);
contentPanel.add(params[0]);
mainPanel.add(contentPanel, BorderLayout.CENTER);
validate();
pack();
repaint();
}
private void clearFields() {
for (int i = 0; i < params.length; i++) {
params[i].setText("");
}
}
public void cancelButt_actionPerformed(ActionEvent e) {
this.setVisible(false);
this.dispose();
}
public void okButt_actionPerformed(ActionEvent e) {
if (this.action == this.CREATE) {
String name = params[0].getText();
String getUserName = params[1].getText();
String sendUserName = params[2].getText();
String quantity = params[3].getText();
if (name.length() <= 0 || getUserName.length() <= 0 ||
sendUserName.length() <= 0 || quantity.length() <= 0) {
JOptionPane.showMessageDialog(this, "请输入全部必要信息,文本域不允许为空!!", "错误提示!",
JOptionPane.ERROR_MESSAGE);
}
else {
boolean canOut = checkCanOut(name, quantity);
if (canOut) {
String insertSql = "insert into " + Type.SILOOUTTABLE + " values(" +
getNextId() + ",'" +
name + "','" + getUserName + "','" + sendUserName + "'," +
quantity +
",";
insertSql = insertDate(insertSql);
float newQuantity = totalQuantity - Float.parseFloat(quantity);
String updateSql = "update " + Type.SILOTABLE + " set 数量=" +
newQuantity + " where 原料名='" + name + "'";
try {
con.setAutoCommit(false);
Statement sta = con.createStatement();
sta.executeUpdate(insertSql);
sta.executeUpdate(updateSql);
con.commit();
sta.close();
con.setAutoCommit(true);
updateView();
JOptionPane.showMessageDialog(this, "出库货单添加成功!!");
kit.beep();
}
catch (SQLException ex) {
try {
con.rollback();
}
catch (SQLException ex1) {
System.err.println(ex1.toString());
}
System.err.println(ex.toString());
}
}
}
}
else if (this.action == this.DELETE) {
try {
if (params[0].getText() == null || params[0].getText().length() <= 0) {
JOptionPane.showMessageDialog(this, "请输入要删除的记录的序号!!", "错误提示!",
JOptionPane.ERROR_MESSAGE);
}
else {
String checkSql = "select * from " + Type.SILOOUTTABLE + " where 序号=" +
params[0].getText();
Statement sta1 = con.createStatement();
ResultSet set = sta1.executeQuery(checkSql);
if (set.next()) {
String sql = "delete from " + Type.SILOOUTTABLE + " where 序号=" +
params[0].getText();
sta1.close();
Statement sta = con.createStatement();
sta.executeUpdate(sql);
sta.close();
updateView();
JOptionPane.showMessageDialog(this, "记录删除成功!!!", "删除记录",
JOptionPane.INFORMATION_MESSAGE);
kit.beep();
}
else {
JOptionPane.showMessageDialog(this, "您输入的记录序号不存在,请确认后重新输入!!!",
"错误提示",
JOptionPane.ERROR_MESSAGE);
sta1.close();
}
}
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "删除错误!!!", "错误!!",
JOptionPane.ERROR_MESSAGE);
System.err.println(ex.toString());
}
}
}
class SiloOutUpdateDialog_okButt_actionAdapter
implements ActionListener {
private SiloOutUpdateDialog adaptee;
SiloOutUpdateDialog_okButt_actionAdapter(SiloOutUpdateDialog adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.okButt_actionPerformed(e);
}
}
class SiloOutUpdateDialog_cancelButt_actionAdapter
implements ActionListener {
private SiloOutUpdateDialog adaptee;
SiloOutUpdateDialog_cancelButt_actionAdapter(SiloOutUpdateDialog adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.cancelButt_actionPerformed(e);
}
}
class SiloOutUpdateDialog_deleteRadio_actionAdapter
implements ActionListener {
private SiloOutUpdateDialog adaptee;
SiloOutUpdateDialog_deleteRadio_actionAdapter(SiloOutUpdateDialog adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.deleteRadio_actionPerformed(e);
}
}
class SiloOutUpdateDialog_createRadio_actionAdapter
implements ActionListener {
private SiloOutUpdateDialog adaptee;
SiloOutUpdateDialog_createRadio_actionAdapter(SiloOutUpdateDialog adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.createRadio_actionPerformed(e);
}
}
private long getNextId() {
String nextId = "";
long longId = 0;
try {
Statement sta = con.createStatement();
ResultSet set = sta.executeQuery("select nextid from " +
Type.SILOOUTIDTABLE);
set.next();
nextId = set.getString(1);
set.close();
sta.close();
longId = Long.valueOf(nextId).longValue();
if (longId == Long.MAX_VALUE) {
longId = 10000;
}
sta = con.createStatement();
set = sta.executeQuery("select * from " + Type.SILOOUTTABLE +
" where 序号=" +
longId);
while (set.next()) {
longId++;
set.close();
sta.close();
sta = con.createStatement();
set = sta.executeQuery("select * from " + Type.SILOOUTTABLE +
" where 序号=" +
longId);
}
if (set != null) {
set.close();
set = null;
}
if (sta != null) {
sta.close();
sta = null;
}
sta = con.createStatement();
int col = sta.executeUpdate("update " + Type.SILOOUTIDTABLE +
" set nextId=" +
longId); //10001);
}
catch (SQLException e) {
System.err.println(e.toString());
e.printStackTrace();
}
return longId;
}
private boolean checkCanOut(String name, String outQuantity) {
Statement sta = null;
ResultSet set = null;
boolean result = false;
boolean canOut = false;
float floatQuantity = Float.parseFloat(outQuantity);
try {
sta = con.createStatement();
set = sta.executeQuery("select 数量 from " + Type.SILOTABLE +
" where 原料名='" + name + "'");
result = set.next();
float quantity = set.getFloat(1);
totalQuantity = quantity;
if (quantity >= floatQuantity) {
canOut = true;
}
set.close();
sta.close();
boolean returnValue = false;
returnValue = (result && canOut);
return returnValue;
}
catch (SQLException ex) {
System.err.println(ex.toString());
ex.printStackTrace();
return false;
}
}
private String insertDate(String sql) {
Calendar ca = Calendar.getInstance();
Date date = new Date(ca.getTime().getTime());
sql += "'" + date + "',";
int len = sql.length();
sql = sql.substring(0, len - 1);
sql += ")";
return sql;
}
private void updateView() {
try {
Statement sta = con.createStatement();
ResultSet set = sta.executeQuery("select * from " + Type.SILOOUTTABLE);
ResultSetMetaData meta = set.getMetaData();
int colCount = meta.getColumnCount();
int rowCount = parentPanel.tablePanel.tbModel.getRowCount();
for (int i = 0; i < rowCount; i++) {
parentPanel.tablePanel.tbModel.removeRow(0);
}
Vector colNames = new Vector();
Vector row;
for (int i = 1; i <= colCount; i++) {
colNames.add(meta.getColumnLabel(i));
}
parentPanel.tablePanel.tbModel.setColumnIdentifiers(colNames);
while (set.next()) {
row = new Vector();
for (int i = 1; i <= colCount; i++) {
String temp = set.getString(i);
if (temp.indexOf(" ") >= 0) {
temp = temp.substring(0, temp.indexOf(" "));
}
row.add(temp);
}
parentPanel.tablePanel.tbModel.addRow(row);
} //end while
if (parentPanel.tablePanel.tbModel.getRowCount() <= 0) {
kit.beep();
JOptionPane.showMessageDialog(this, "对不起,没有符合查找条件的记录,请修改数据重新查找!!");
}
}
catch (SQLException ex) {
System.err.println(ex.toString());
kit.beep();
JOptionPane.showMessageDialog(this, "查询错误,请检查输入是否正确!!");
}
catch (Exception ex2) {
System.err.println(ex2);
kit.beep();
JOptionPane.showMessageDialog(this, "查询错误!!!!");
}
validate();
repaint();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -