我使用了一个用于将CSV文件导入数据库的查询:
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
进行此操作,所以我尝试使用此代码:
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);
}
它没有错误,没有结果。
答案 0 :(得分:-1)
这将帮助您完成任务。
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您可以学习一些视频教程。