JDBC Servlet从MySQL数据库返回空列表
我需要从 MySQL 获取所有数据。我已经尝试了几种方法来尝试让它发挥作用,但仍然得到空列表。我见过类似的问题通过添加得到解决
命令中的 com.mysql.cj.jdbc.Driver.zeroDateTimeBehavior=convertToNull ,但这只会给我一个类未找到异常
。有人知道如何克服这个问题吗?
我的 Servlet:
package org.datafetching;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
@WebServlet("/fetchdata")
public class FetchData extends HttpServlet {
private static final long serialVersionUID = 1L;
public FetchData() {
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//Class.forName("com.mysql.cj.jdbc.Driver.zeroDateTimeBehavior=convertToNull");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "name", "pw");
List<People> staffs = new ArrayList<People>();
PreparedStatement ps=conn.prepareStatement("select * from grey_goose.winter_internship");
ResultSet rs=ps.executeQuery();
while (rs.next()) {
People people = new People();
staffs.add(people);
}
rs.close();
ps.close();
conn.close();
Gson gson = new Gson();
String tablejson = gson.toJson(staffs);
PrintWriter printWriter = response.getWriter();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
printWriter.write(tablejson);
printWriter.flush();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
我的 People 类:
package org.datafetching;
public class People {
private Integer sl_no;
private String business_code;
private Integer cust_number;
private String clear_date;
private Integer buisness_year;
private Integer doc_id;
private String posting_date;
private String document_create_date;
private String due_in_date;
private String invoice_currency;
private String document_type;
private Integer posting_id;
private String total_open_amount;
private String baseline_create_date;
private String cust_payment_terms;
private Integer invoice_id;
public Integer getSlNo() {
return sl_no;
}
public void setSlNo(Integer sl_no) {
this.sl_no = sl_no;
}
public String getBusinessCode() {
return business_code;
}
public void setBusinessCode(String business_code) {
this.business_code = business_code;
}
public Integer getCustNumber() {
return cust_number;
}
public void setCustNumber(Integer cust_number) {
this.cust_number = cust_number;
}
public String getClearDate() {
return clear_date;
}
public void setClearDate(String clear_date) {
this.clear_date = clear_date;
}
public Integer getBusinessYear() {
return buisness_year;
}
public void setBusinessYear(Integer buisness_year) {
this.buisness_year = buisness_year;
}
public Integer getDocId() {
return doc_id;
}
public void setDocId(Integer doc_id) {
this.doc_id = doc_id;
}
public String getPostingDate() {
return posting_date;
}
public void setPostingDate(String posting_date) {
this.posting_date = posting_date;
}
public String getDocumentCreateDate() {
return document_create_date;
}
public void setDocumentCreateDate(String document_create_date) {
this.document_create_date = document_create_date;
}
public String getDueInDate() {
return due_in_date;
}
public void setDueInDate(String due_in_date) {
this.due_in_date = due_in_date;
}
public String getInvoiceCurrency() {
return invoice_currency;
}
public void setInvoiceCurrency(String invoice_currency) {
this.invoice_currency = invoice_currency;
}
public String getDocumentType() {
return document_type;
}
public void setDocumentType(String document_type) {
this.document_type = document_type;
}
public Integer getPostingId() {
return posting_id;
}
public void setDocumentType(Integer posting_id) {
this.posting_id = posting_id;
}
public String getTotalOpenAmount() {
return total_open_amount;
}
public void setTotalOpenAmount(String total_open_amount) {
this.total_open_amount = total_open_amount;
}
public String getBaselineCreateDate() {
return baseline_create_date;
}
public void setBaselineCreateDate(String baseline_create_date) {
this.baseline_create_date = baseline_create_date;
}
public String getCustPaymentTerms() {
return cust_payment_terms;
}
public void setCustPaymentTerms(String cust_payment_terms) {
this.cust_payment_terms = cust_payment_terms;
}
public Integer getInvoiceId() {
return invoice_id;
}
public void setInvoiceId(Integer invoice_id) {
this.invoice_id = invoice_id;
}
}
编辑:添加当前输出的示例
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在此处阅读:
Read here: https://docs.oracle.com/en/java/javase/11/docs/api/java.sql/java/sql/ResultSet.html
在里面,当您必须使用 ResultSet 中的实际数据填充 People 对象时:
另外,我建议您将 SQL 中的 * 替换为表 'winter_internship' 中的逗号分隔列列表,因为使用 * 时通常不能保证列顺序。
Inside while you have to fill your People object with actual data from ResultSet:
Also I suggest you to replace * in your SQL with comma separated list of columns from your table 'winter_internship', because with * column order is generally not guaranteed.