【问题标题】:sql statement "into outfile" not working with jdbcsql语句“进入outfile”不适用于jdbc
【发布时间】:2010-01-04 14:28:39
【问题描述】:

我正在尝试将“导出到 CSV”功能添加到显示来自 MySQL 数据库的数据的 web 应用程序。我编写了一个“queryExecuter”类来执行来自我的 servlet 的查询。我已经用它成功地执行了插入查询,所以我知道连接工作等等,但是使用“into outfile”语句的查询根本没有执行。这是我的代码,

java 类...

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;

public class queryExecuter {
    public void exportToCSV(String query) {
        DBase db = new DBase();
        Connection conn = db.connect(
               mydatabaseurl ,"myusername","mypassword");

        db.exportData(conn,query);
    }

}

class DBase {
    public DBase() {
    }

    public Connection connect(String db_connect_str,
            String db_userid, String db_password) {
        Connection conn;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(db_connect_str,
                    db_userid, db_password);

        } catch(Exception e) {
            e.printStackTrace();
            conn = null;
        }
        return conn;
    }

    public void exportData(Connection conn,String query) {
        Statement stmt;

        try {
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);

            stmt.execute(query);

        } catch(Exception e) {
            e.printStackTrace();
            stmt = null;
        }
    }
};

servlet...

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class MyExportServlet extends HttpServlet {

  public void doPost(HttpServletRequest request, HttpServletResponse response)

     throws IOException, ServletException {

            String query = "select into outfile 'theoutfile.txt' * from mytable;";

            request.setAttribute("query", query);

            queryExecuter mydata = new queryExecuter();

            mydata.exportToCSV(query);

            RequestDispatcher view = request.getRequestDispatcher("ConfirmationPage.jsp");

            view.forward(request, response);
    }
}

任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: java mysql sql jdbc into-outfile


    【解决方案1】:

    你是如何断定它没有执行的?

    让我猜猜,文件没有写在你期望的地方?这是非常正确的,因为您的代码使用像'theoutfile.txt' 这样的相对磁盘文件系统路径,而不是像'c:/theoutfile.txt' 这样的绝对文件系统路径,类似地也可能使用new File('theoutfile.txt')

    相对磁盘文件系统路径是相对于当前工作目录的,它取决于您启动应用程序的方式(在命令行中、作为服务、在服务器中等)。编程时永远不要依赖它。始终使用绝对磁盘文件系统路径。

    在 MySQL 中,文件的实际根目录可能c:/mysql/bin,而在 webapp(servlet)中,文件的实际根目录可能c:/webserverinstallationfolder/webapps。相对路径没有什么是确定的。不要使用它们。

    【讨论】:

    • 感谢您的帖子我同意相对路径是不可取的,但是我知道我可以在我的 MySQL 数据文件中查看这个简单的测试用例,因为我直接从 MySQL 执行查询作为比较。看来真正的问题是我的语法在我尝试修复代码中的其他内容时变得混乱。
    • 也就是说,你的问题已经用另一种方式解决了?
    【解决方案2】:

    通常您会执行查询,并获得一个 ResultSet。您可以读取 ResultSet 并将内容写入文件(使用 Java)。这很好(tm),因为 Java 服务器通常在另一台机器上作为数据库,而 outfile 总是在数据库机器上。

    除了那种智慧:)你应该这样做

    select * into outfile
    

    而不是

    select into outfile *
    

    这也可能是你甚至不得不做的事情

    select <entire_query> into outfile
    

    Export to CSV with javaMySQL Select syntax

    【讨论】:

    • 这只能在记录数以万计的情况下占用内存。
    • 谢谢!在某个地方,我的 sql 语法变得混乱了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    • 2017-11-05
    相关资源
    最近更新 更多