简体   繁体   English

java.sql.SQLException:ResultSet来自UPDATE。 没有数据

[英]java.sql.SQLException: ResultSet is from UPDATE. No Data

I'm trying to import table using Java application. 我正在尝试使用Java应用程序导入表。 The query runs perfectly with MySQL Workbench: 该查询可与MySQL Workbench完美运行:

Here is the query: 这是查询:

LOAD DATA LOCAL INFILE 'C:\\Users\\Zero\\Desktop\\Book1.csv' INTO TABLE  tbl_students
FIELDS TERMINATED BY ',' 
enclosed by '"' 
lines terminated by '\n'

Now using that, I included a save path for the user to locate the file here is the code: 现在使用它,我为用户提供了一个保存路径来找到文件,这里是代码:

    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 ");}

for the ImportTBL() here is the code 对于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);
        }
}

I think you are using the wrong execute method. 我认为您使用的执行方法错误。 The exception you get states that ResultSet is from UPDATE. 您得到的异常表明ResultSet来自UPDATE。 No Data 没有数据

Try using: 尝试使用:

int affectedRows = ps.executeUpdate();

if ( affectedRows <= 0 ) System.out.println("failed!");
else System.out.println("Success :)");

see: http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#executeUpdate() 请参阅: http : //docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#executeUpdate()

The problem is that you are executing the load statement with executeQuery() method. 问题在于您正在使用executeQuery()方法执行load语句。 It is designed for executing select statements that return a resultset, not a data manupulation statement, that does not. 它设计用于执行返回结果集的select语句,而不是返回结果集的数据操作语句。 Use executeUpdate() method instead and check the number of rows affected to determine if the execution was successful. 请改用executeUpdate()方法,并检查受影响的行数,以确定执行是否成功。

Also, there is no need to prepare this statement because it's highly unlikely that the user would run this repeatedly several times right after each other. 另外,也无需准备该语句,因为用户极不可能彼此立即重复运行几次。 And you code does not even try to make use of the advantages offered by prepared statements. 而且您的代码甚至没有尝试利用准备好的语句提供的优势。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM