📄 thirdcatalogfrompane.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 ThirdCatalogFromPane extends JPanel{
//声明数据库连接组件
private DBConnection con=null;
private File file=null;//声明所选择的文件
private String fileName;//声明所选择文件的文件名
private JFileChooser fileChooser;//声明目的文件
private boolean isRecordIlleagal=false;//判断是否有无效记录存在
public ThirdCatalogFromPane(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;
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_Second_name=st.nextToken().trim();
String third_name=st.nextToken().trim();
float cost=(new Float(st.nextToken().trim())).floatValue();
float price=(new Float(st.nextToken().trim())).floatValue();
if(cost<0||price<0){
//JOptionPane.showMessageDialog(null, "成本.价格都必须为正数!","提示",JOptionPane.INFORMATION_MESSAGE);
isRecordIlleagal=true;
nameNotIn+=(count+"--");
continue;
}
int store_in=(new Integer(st.nextToken().trim())).intValue();
int limit=(new Integer(st.nextToken().trim())).intValue();
if(store_in<0||limit<0){
//JOptionPane.showMessageDialog(null, "现有库存.最低库存量都必须为正整数!","提示",JOptionPane.INFORMATION_MESSAGE);
isRecordIlleagal=true;
nameNotIn+=(count+"--");
continue;
}
String querySql="select Second_ID from Second_Catalog where Second_name='"+To_Second_name+"'";
ResultSet rs=con.executeSelect(querySql);
int Second_ID=0;
if(rs.next()){
Second_ID=rs.getInt(1);
}
if(Second_ID==0){
isRecordIlleagal=true;
nameNotIn+=(count+"--");
//JOptionPane.showMessageDialog(null,"该商品明细类不存在!","提示",JOptionPane.INFORMATION_MESSAGE);
continue;
}
String queryThirdCatalog="select* from Third_Catalog where To_Second_ID="+
Second_ID+" and Third_name='"+third_name+"'";
ResultSet rSet=con.executeSelect(queryThirdCatalog);
if(rSet.next()){
nameNotIn+=(count+"--");
nameExist++;
continue;
}else{
String insertStr="insert into Third_Catalog values("+Second_ID+",'"+
third_name+"',"+cost+","+price+","+store_in+","+limit+")";
con.executeDML(insertStr);
countIn++;
}
}
bw.close();
con.resetCon();
if(!nameNotIn.equals("编号:")){
JOptionPane.showMessageDialog(null, "导入 "+countIn+" 条记录,没导入的记录有 "+nameNotIn+
","+nameExist+" 条记录已存在!是否有无效记录: "+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 + -