Exception: Can not issue SELECT via executeUpdate!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • asenthil
    New Member
    • Dec 2006
    • 61

    Exception: Can not issue SELECT via executeUpdate!

    Hai to all,

    Now i'm trying to export Mysql table to a file By using the

    following program...

    itz sucessfully compiled... but it throws the above Exception..

    [HTML]
    Code:
    import java.io.*;
    import java.sql.*;
    public class ExportData {
    public static void main(String args[]) {
            String Driver;
            Statement stmt; 
             ResultSet rs; 
     
             Driver = "com.mysql.jdbc.Driver";
             Connection con = null;
     
    try {
    Class.forName(Driver);
    con = DriverManager.getConnection("jdbc:mysql://192.168.0.01/senthil","root","");
     if(!con.isClosed()){
    System.out.println("Successfully connected to MySQL DataBase \n");
          stmt = con.createStatement(); 
          String filename = "C:/2.txt";
          String tablename = "employees";
          String sql;
    stmt.executeUpdate("SELECT * INTO OUTFILE \"" + filename + "\" FROM " + tablename);
      }
    } catch(Exception e) {
     System.err.println("Exception: " + e.getMessage());
        } 
    finally {
          try {
            if(con != null)
              con.close();
          } catch(SQLException e) {}
     }
    }
    }
    [/HTML]

    Can anyone plzz help me....

    thanks and regards..
    senthil
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by asenthil
    Hai to all,

    Now i'm trying to export Mysql table to a file By using the

    following program...

    itz sucessfully compiled... but it throws the above Exception..

    [HTML]
    Code:
    import java.io.*;
    import java.sql.*;
    public class ExportData {
    public static void main(String args[]) {
    String Driver;
    Statement stmt; 
    ResultSet rs; 
     
    Driver = "com.mysql.jdbc.Driver";
    Connection con = null;
     
    try {
    Class.forName(Driver);
    con = DriverManager.getConnection("jdbc:mysql://192.168.0.01/senthil","root","");
    if(!con.isClosed()){
    System.out.println("Successfully connected to MySQL DataBase \n");
    stmt = con.createStatement(); 
    String filename = "C:/2.txt";
    String tablename = "employees";
    String sql;
    stmt.executeUpdate("SELECT * INTO OUTFILE \"" + filename + "\" FROM " + tablename);
    }
    } catch(Exception e) {
    System.err.println("Exception: " + e.getMessage());
    } 
    finally {
    try {
    if(con != null)
    con.close();
    } catch(SQLException e) {}
    }
    }
    }
    [/HTML]

    Can anyone plzz help me....

    thanks and regards..
    senthil
    You use executeQuery for select. You re doing it wrong any way. Read the data from the db record by record and write to the file using BufferedWriter and FileReader.

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      hello r035198x
      why r u telling to do using file operation...
      i wondering .....
      plz tell me u suggestion in detail ............

      Comment

      • hirak1984
        Contributor
        • Jan 2007
        • 316

        #4
        well executeUpdate is for operations such as insert,update etc.

        for select we use executeQuery.
        and regarding file operation,It is up to me whether I write the output in file or console.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by dmjpro
          hello r035198x
          why r u telling to do using file operation...
          i wondering .....
          plz tell me u suggestion in detail ............
          The reading of data is done using the jdbc and the sql is done using executeQuery in this case which returns a resultset object. That part is fine. In this case the OP used the wrong command, executeUpdate, which you use to modify data in the table and you have to provide the new data that you want to mofify with. That's why the compiler complained. With executeQuery, you query the database and ask for data which you are given through a resultset object. To write that data to a file, you do not use sql like the OP was trying to there but instead you use a FileWriter and wrap it with a BufferedWriter like this

          Code:
          BufferedWriter writer = new BufferedWriter(new FileWriter("filename.txt"));
          and then do something like
          Code:
          writer.write(resultset.getString("columnName"));

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            sorry my mistake .......


            i didn't see that command ......

            u r right ....
            what i am fool

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by dmjpro
              sorry my mistake .......


              i didn't see that command ......

              u r right ....
              what i am fool
              Don't be too hard on yourself. We all miss important parts of code at one point or another. That's the reason we keep company.

              Comment

              Working...