我有一个 MySQL 表,其中包含我需要填充到 jsp 页面上的表中的文件列表。我已附上表格内容的屏幕截图。我指的是给出的例子 here 。但给出的示例仅适用于一个文件。如果专家能给我指出一个可以遵循的例子,那将非常有帮助...... 预先非常感谢您。
最佳答案
第一步: 数据库到 Controller : 通过创建 DAO(数据访问对象)从数据库中获取数据 例如:
public class Document {
String document_name;
String document_date;
String document_RevisedName;
public String getDocument_name() {
return document_name;
}
public void setDocument_name(String document_name) {
this.document_name = document_name;
}
public String getDocument_date() {
return document_date;
}
public void setDocument_date(String document_date) {
this.document_date = document_date;
}
public String getDocument_RevisedName() {
return document_RevisedName;
}
public void setDocument_RevisedName(String document_RevisedName) {
this.document_RevisedName = document_RevisedName;
}
}
创建接口(interface)Document_DAO:
public interface Document_DAO {
ArrayList<Document>getAllDocuments();
}
将上述接口(interface)实现为实现类Document_DAO_Imp
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.mysql.jdbc.Connection;
import Utils.DataSource;
import dao_class.Class;
public class Docuemnt_DAO_IMp implements Document_DAO{
Connection conn = null;
DataSource dataSource = new DataSource();
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String query= "";
ArrayList<Document> documentList= new ArrayList<>();
@Override
public ArrayList<Document> getAllDocuments() {
conn = (Connection) dataSource.createConnection();
try{
query = "SELECT * FROM youTableName";
preparedStatement = conn.clientPrepareStatement(query);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Document doc= new Document();
doc.setDocument_date(resultSet.getString("youRowName"));
doc.setDocument_RevisedName(resultSet.getString("youRowName"));
documentList.add(doc);
}
}
catch(SQLException sqlE){
System.out.println(sqlE.toString());
}
finally {
try {
resultSet.close();
preparedStatement.close();
conn.close();
} catch (SQLException e) {
System.out.println(e.toString());
}
}
return documentList;
}
}
最后: 从您的 Controller /Servlet 调用上面的 DAO。
ArrayList<Document> docList = new ArrayList<>();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Document_DAO dao = new Document_DAO_Imp();
docList = dao.getAllDocuments();
JsonObject jsonObject = new JsonObject();
JsonElement jsonElement = null;
if (classList.size()!=0) {
jsonElement = new Gson().toJsonTree(docList);
jsonObject.add("docList", jsonElement);
jsonObject.addProperty("success", "true");
}
else{
jsonObject.addProperty("success", "true");
}
response.getWriter().println(jsonObject);
}
}
到目前为止,您已成功从数据库中获取数据并将其发送给您的 JSP/HTML 页面。 确保添加 GSON 谷歌库以添加到您构建的路径和 WEB-INF/lib 文件夹中。
现在使用 jquery 我们可以将结果解析到页面,也可以以 XLS 格式下载它们。
要使用 Jquery Ajax 获取数据,您可以提供事件,也可以在页面加载时加载数据。 $(文档).load(函数(){ });
$.ajax({
type:"get",
url : "DocumentServlet",
dataType: 'json',
// if received a response from the server
success : function(data, textStatus, jqXHR) {
console.log("Getting Document Information!");
if (data.success == 'true') {
data = data.docList;
console.log(data);
$('.table').html("");
$.each(data, function(index, value) {
var eachrow = "<tr><td>"+value.documentName
+"</td><td>"+value.documentRevisedName
+"</td></tr>";
$('.table').append(eachrow);
});
}else{
console.log("failure");
}
},
error : function(jqXHR, textStatus, errorThrown) {
}
});
成功地将 JSON 数组填充到表中。
然后您可以使用任何外部库将数据转换为 XLS 并下载。 首先使用这个结构创建一个页面,,然后我将提供代码下载
关于mysql - 列出数据表中的多个文件进行下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37959426/