我想使用Java應用程序導入表。查詢與MySQL工作臺完美運行:java.sql.SQLException:ResultSet來自UPDATE。沒有數據
下面是該查詢:
LOAD DATA LOCAL INFILE 'C:\\Users\\Zero\\Desktop\\Book1.csv' INTO TABLE tbl_students
FIELDS TERMINATED BY ','
enclosed by '"'
lines terminated by '\n'
現在使用,我包括一個保存路徑,爲用戶在這裏找到該文件是代碼:
chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle(choosertitle);
FileNameExtensionFilter filter = new FileNameExtensionFilter("CSV Files", "csv");
chooser.setFileFilter(filter);
if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
path = file.getAbsolutePath();
System.out.println(path.replace("\\", "\\\\"));
ImportTBL();
}
else {
System.out.println("No Selection ");}
的ImportTBL()這裏是代碼
private void ImportTBL(){
String sql = "LOAD DATA LOCAL INFILE '"+path.replace("\\", "\\\\")+"' INTO TABLE tbl_students "
+ "FIELDS TERMINATED BY ',' enclosed by '\"' lines terminated by '\\n'";
System.out.println(sql);
try{PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if(rs.next()){
JOptionPane.showMessageDialog(null, "Import Successful!");
}
else{
JOptionPane.showMessageDialog(null, "Import Fail!");}
}
catch(Exception e){
JOptionPane.showMessageDialog(null, "Error: "+ e);
}
}
嘗試刪除本地,如'LOAD DATA INFILE'。 –
@RahulYadav我想導入一張表,爲什麼我應該刪除LOAD DATA INFILE –
你會得到什麼錯誤? –