Skip to Main Content

Java and JavaScript in the Database

Announcement

Testing banner

Is it possible to export a MYSQL table directly from Java?

User_DDIDTDec 21 2022
public class automateExport {
    public static void main(String[] argv) throws Exception {
        String driverName = "com.mysql.jdbc.Driver";
        Class.forName(driverName);
        String serverName = "192.168.0.189";
        String mydatabase = "ArchiveIndexer";
        String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
        String username = "username";
        String password = "password";
        Connection connection = DriverManager.getConnection(URL, username, password);
        Statement stmt = connection.createStatement();
        String filename = "c:/outfiless.txt";
        String tablename = "D_Centre";
        System.err.println("SELECT * INTO OUTFILE \"" + filename + "\" FROM " + tablename);
        stmt.executeUpdate("SELECT * INTO OUTFILE \"" + filename + "\" FROM " + tablename);
        // stmt.executeQuery("SELECT * INTO OUTFILE \"" + filename + "\" FROM " + tablename);
        // stmt.execute("SELECT * INTO OUTFILE \"" + filename + "\" FROM " + tablename);
    }
}

But this is throwing error like

"Exception in thread "main" java.sql.SQLException: Can not issue SELECT via executeUpdate().
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1764)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725)
    at automateexceldatabase.automateExport.main(automateExport.java:38)
Java Result: 1"

Java

Comments
Post Details
Added on Dec 21 2022
0 comments
55 views