📄 silooutupdatedialog.java~157~
字号:
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;
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();
}
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.setColumns(1);
// gridLayout1.setRows(4);
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);
}
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();
boolean canOut = checkCanOut(name, quantity);
// System.out.println(canOut);
if (canOut) {
String insertSql = "insert into "+Type.SILOOUTTABLE+" values("+getNextId()+",'"+
name+"','"+getUserName+"','"+sendUserName+"',"+quantity+",";
insertSql = insertDate(insertSql);
// System.out.println(insertSql);
float newQuantity = totalQuantity - Float.parseFloat(quantity);
String updateSql = "update "+Type.SILOTABLE+" set 数量="+newQuantity+" where 原料名='"+name+"'";
// System.out.println(updateSql);
try{
con.setAutoCommit(false);
Statement sta = con.createStatement();
sta.executeUpdate(insertSql);
sta.executeUpdate(updateSql);
con.commit();
updateView();
sta.close();
}
catch(SQLException ex){
try {
con.rollback();
}
catch (SQLException ex1) {
System.err.println(ex1.toString());
}
System.err.println(ex.toString());
}
}
else{
JOptionPane.showMessageDialog(this, "请确定要出库的物品存在于原料库中!!", "错误提示", JOptionPane.ERROR_MESSAGE);
}
}
else if(this.action == this.DELETE){
try {
String sql = "delete from "+Type.SILOOUTTABLE+" where 序号="+params[0].getText();
Statement sta = con.createStatement();
sta.executeUpdate(sql);
updateView();
JOptionPane.showMessageDialog(this, "删除成功!!!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
catch (SQLException ex2) {
JOptionPane.showMessageDialog(this, "删除错误,请确定输入的记录序号存在!!", "错误提示", JOptionPane.ERROR_MESSAGE);
System.err.println(ex2.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();
// longId++;
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;
}
// longId++;
sta = con.createStatement();
int col = sta.executeUpdate("update " + Type.SILOOUTIDTABLE+ " set nextId=" +
longId); //10001);
// System.out.print(col);
}
catch (SQLException e) {
System.err.println(e.toString());
e.printStackTrace();
}
// return nextId;
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();
// String quantity = set.getString(1);
// float fQuantity = Float.parseFloat(quantity);
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 += ")";
// System.out.println(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();
// parentPanel.tablePanel.tbModel = new DefaultTableModel();
int rowCount = parentPanel.tablePanel.tbModel.getRowCount();
// System.out.print(rowCount);
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(" "));
}
// if(i == 3 || i == 4){//单价和数量取两位小数
// if (i == 4) {
// temp = temp.substring(0, temp.length() - 2);
// }
row.add(temp);
// System.out.print(set.getString(i)+" ");
}
// System.out.println();
parentPanel.tablePanel.tbModel.addRow(row);
}
}
catch (Exception ex) {
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -