首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >进行Jfile选择器查询

问进行Jfile选择器查询
EN

Stack Overflow用户
提问于 2015-05-20 19:13:26
回答 1查看 21关注 0票数 0

我使用了一个用于将CSV文件导入数据库的查询:

代码语言:javascript
复制
DATA LOCAL INFILE 'C:\\Users\\Desktop\\TT (4).csv' INTO TABLE incident FIELDS TERMINATED BY ';' ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\n'  (id_incident, status, vendor, priority, criticality_tt, title, description, affected_ci, affected_service, assignee, assignment_group, reassignment_count, open_time, close_time, outage_start, reassignment_time, outage_end, impact, solution, assignment_manager_name, assign_deptmgr, opened_by, closed_by, closure_code, update_action, last_updated_by);

我想用JFileChooser做这个操作,所以我试着用下面的代码:

代码语言:javascript
复制
JFileChooser fc=new JFileChooser(); 
fc.showOpenDialog(this);
File f=fc.getSelectedFile();
String path=f.getAbsolutePath();
try
{ 
    FileInputStream fin=new FileInputStream(f);
int len=(int)f.length(); 
        java.sql.PreparedStatement ps1=con.prepareStatement("LOAD DATA LOCAL INFILE '?' INTO TABLE incident FIELDS TERMINATED BY ';' ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n'  (id_incident, status, vendor, priority, criticality_tt, title, description, affected_ci, affected_service, assignee, assignment_group, reassignment_count, open_time, close_time, outage_start, reassignment_time, outage_end, impact, solution, assignment_manager_name, assign_deptmgr, opened_by, closed_by, closure_code, update_action, last_updated_by)");
ps1.setBinaryStream(1, fin, len); 
int status =ps1.executeUpdate();
if(status > 0) 
{ 
    j1.setText("Successfully inserted in DB"); 
}
else
{ 
        j2.setText(" not inserted!"); 
        } 
}
catch(Exception e)
{System.out.println(e);
}  

它不会给出错误,也不会产生结果。

EN

回答 1

Stack Overflow用户

发布于 2015-05-20 19:37:07

这将帮助您完成任务。

代码语言:javascript
复制
public YOUR-METHOD(){
    int returnVal = fileChooser.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fileChooser.getSelectedFile();
        try {
            // What to do with the file, e.g. display it in a TextArea
            CSVReader reader = new CSVReader(new FileReader( file.getAbsolutePath()));
            List myEntries = reader.readAll();
        } catch (IOException ex) {
            System.out.println("problem accessing file"+file.getAbsolutePath());
        }
    } else {
        System.out.println("File access cancelled by user.");
    }
}

和here一些视频教程,你可以学到。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30348015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档