📄 goodsoutfrompane.java
字号:
package file2;
import java.sql.*;
import java.util.StringTokenizer;
import java.util.Calendar;
import javax.swing.*;
import java.awt.*;
import java.text.*;
import java.io.*;
public class GoodsOutFromPane extends JPanel{
//声明数据库连接组件
private DBConnection con=null;
private File file=null;//声明所选择的文件
private String fileName;//声明所选择文件的文件名
private JFileChooser fileChooser;//声明目的文件
private boolean isStoreNotEnough=false;//判断是否有商品库存不足
private boolean isRecordIlleagal=false;//判断导入文件中是否有无无效记录
public GoodsOutFromPane(JFrame frame){
fileChooser=new JFileChooser();
int returnValue=fileChooser.showOpenDialog(frame);
if(returnValue==JFileChooser.APPROVE_OPTION){
con=new DBConnection();
file=fileChooser.getSelectedFile();
if(file==null){
JOptionPane.showMessageDialog(null, "导出文件不能为空,请您选择导出文件!", "提示", JOptionPane.ERROR_MESSAGE);
return;
}
fileName=file.getName();
JOptionPane.showMessageDialog(null, "正从文件"+fileName+"导入数据", "提示", JOptionPane.INFORMATION_MESSAGE);
loadFrom(file);
}
}
public void loadFrom(File file){
try{
BufferedReader bw=new BufferedReader(new FileReader(file));
String read=null;
int count=0;
int countIn=0;
String nameNotIn="编号:";//记录录入有误的进货信息的具体商品的名称
int nameExist=0;
int isStart=0;//判断是否开始读记录,0表示未开始读
while((read=bw.readLine())!=null){
if(read.equals("")&&isStart==1){
break;
}
if(read.equals("")&&isStart==0){
continue;
}
isStart=1;
count++;
StringTokenizer st=new StringTokenizer(read,"|");
String To_Third_name=st.nextToken().trim();
String queryThird="select* from Third_Catalog where Third_name='"+To_Third_name+"'";
ResultSet test=con.executeSelect(queryThird);
if(!test.next()){
isRecordIlleagal=true;
nameNotIn+=(count+"--");
continue;
}
int quantity=0;
String dateGet=null;
try{
quantity=(new Integer(st.nextToken().trim())).intValue();
if(quantity<0){
//JOptionPane.showMessageDialog(null, "出货数量必须为正整数!", "提示", JOptionPane.ERROR_MESSAGE);
isRecordIlleagal=true;
nameNotIn+=(count+"--");
continue;
}
String dateStr=st.nextToken().trim();
StringTokenizer operatedStr=new StringTokenizer(dateStr,"-");
int num=0;
while(operatedStr.hasMoreTokens()){
operatedStr.nextToken();
num++;
}
if(num!=3){//说明没有包含两个"-"
//JOptionPane.showMessageDialog(null, "日期格式有误!", "提示", JOptionPane.ERROR_MESSAGE);
isRecordIlleagal=true;
nameNotIn+=(count+"--");
continue;
}
String query="select store_in from Third_Catalog where Third_name='"+To_Third_name+"'";
ResultSet set=con.executeSelect(query);
int store_in=0;
if(set.next()){
store_in=set.getInt(1);
}
if(store_in<quantity){
//JOptionPane.showMessageDialog(null, "该产品库存不足,只有 "+store_in, "提示", JOptionPane.INFORMATION_MESSAGE);
nameNotIn+=(count+"--");
isStoreNotEnough=true;
continue;
}
operatedStr=null;
operatedStr=new StringTokenizer(dateStr,"-");
java.util.Date date=null;
while(operatedStr.hasMoreTokens()){
Calendar calendar=Calendar.getInstance();
calendar.clear();
int year=(new Integer(operatedStr.nextToken().trim())).intValue();
int month=(new Integer(operatedStr.nextToken().trim())).intValue();
int day=(new Integer(operatedStr.nextToken().trim())).intValue();
calendar.set(year, month-1, day);
date=calendar.getTime();
}
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");
dateGet=formatter.format(date);
}catch(Exception e){
e.printStackTrace();
nameNotIn+=(count+"--");
continue;
}
String queryOperationRecord="select* from operationRecord where To_Third_name='"+
To_Third_name+"' and quantity="+quantity+" and operateTime='"+dateGet+
"' and operationState=1";
ResultSet rSet=con.executeSelect(queryOperationRecord);
if(rSet.next()){
int confirm=JOptionPane.showConfirmDialog(null, "数据库中已存在一条与 "+To_Third_name+"--"+quantity+
"--"+dateGet+"一样的出货记录,您是否确认该出货记录?","请确认",JOptionPane.YES_NO_OPTION);
if(confirm==JOptionPane.NO_OPTION){
nameNotIn+=(count+"--");
nameExist++;
continue;
}else{
String insertStr="insert into operationRecord values('"+To_Third_name+"',"+quantity+",'"+
dateGet+"',1)";
con.executeDML(insertStr);
String updateStr="update Third_Catalog set store_in=store_in-"+quantity+
" where Third_name='"+To_Third_name+"'";
con.executeDML(updateStr);
countIn++;
}
}else{
String insertStr="insert into operationRecord values('"+To_Third_name+"',"+quantity+",'"+
dateGet+"',1)";
con.executeDML(insertStr);
String updateStr="update Third_Catalog set store_in=store_in-"+quantity+
" where Third_name='"+To_Third_name+"'";
con.executeDML(updateStr);
countIn++;
}
}
bw.close();
//con.resetCon();
if(!nameNotIn.equals("编号:")){
JOptionPane.showMessageDialog(null, "导入 "+countIn+" 条记录,没导入的记录有 "+nameNotIn+
","+nameExist+" 条记录已存在!是否有商品库存不足:"+isStoreNotEnough+" ,是否有无效记录: "+isRecordIlleagal,"提示",JOptionPane.INFORMATION_MESSAGE);
return;
}else{
JOptionPane.showMessageDialog(null, "导入 "+countIn+" 条记录,没导入的记录有 0条,"+nameExist+" 条记录已存在!","提示",JOptionPane.INFORMATION_MESSAGE);
return;
}
}catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, "导入文件失败,文件记录的格式有误!", "提示", JOptionPane.ERROR_MESSAGE);
return;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -