sql语句“into outfile”不使用 jdbc
我正在尝试将“导出到 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);
}
}
任何帮助将不胜感激。
谢谢
I am attempting to add an "export to CSV" feature to a webapp that displays data from a MySQL database. I have a written a "queryExecuter" class to execute queries from my servlet. I have used this to successfully execute insert queries so I know the connection works etc however queries with the "into outfile" statement are simply not executing. Here is my code,
the java class...
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;
}
}
};
The 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);
}
}
Any help would be greatly appreciated.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您如何断定它没有执行?
让我猜猜,该文件没有写在您期望的位置?这可能是非常正确的,因为您的代码使用相对磁盘文件系统路径(如
'theoutfile.txt'
)而不是绝对文件系统路径(如'c:/theoutfile.txt'
)和类似地,也可能是new File('theoutfile.txt')
。相对磁盘文件系统路径相对于当前工作目录,这取决于您启动应用程序的方式(在命令行中、作为服务、在服务器中等)。编程时你不应该依赖它。始终使用绝对磁盘文件系统路径。
在 MySQL 中,文件的实际根目录可能
c:/mysql/bin
,而在 webapp(servlet)中,文件的实际根目录可能 em>c:/webserverinstallationfolder/webapps
。相对路径没有什么是确定的。不要使用它们。How did you conclude that it's not executing?
Let me guess, the file isn't written there where you expect it to be? This can be very true since your code uses relative disk file system paths like
'theoutfile.txt'
instead of absolute file system paths like'c:/theoutfile.txt'
and analogously probably alsonew File('theoutfile.txt')
.A relative disk file system path is relative to the current working directory which depends on the way how you started the application (in commandline, as a service, in a server, etc). You should never rely on that when programming. Always use absolute disk file system paths.
In MySQL the actual root of the file is probably
c:/mysql/bin
and in webapp (the servlet) the actual root of the file is probablyc:/webserverinstallationfolder/webapps
. Nothing is sure with relative paths. Don't use them.通常,您会执行查询并获取结果集。您可以读取 ResultSet 并将内容写入文件(使用 Java)。这很好,因为 Java 服务器通常与数据库位于另一台机器上,而 outfile 始终位于数据库机器上。
除了这种智慧:)你应该做
而不是
你甚至可能必须
看到 使用 java 和 MySQL 选择语法。
Normally you'd execute the query, and get a ResultSet. You can read the ResultSet and write the contents to file (with Java). This is Good(tm) because the Java server is normally on another machine as the database, and outfile is always on the database machine.
Apart from that wisdom :) you should do
and not
It may also be that you even have to do
see Export to CSV with java and MySQL Select syntax.