JDBC Servlet从MySQL数据库返回空列表

发布于 2025-01-17 15:30:47 字数 5976 浏览 0 评论 0 原文

我需要从 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;
    }
}

编辑:添加当前输出的示例

在此处输入图像描述

I need to fetch all my data from MySQL. I have tried several methods to try to get this to work but still, I'm getting empty list. I had seen a similar problem getting solved by adding
com.mysql.cj.jdbc.Driver.zeroDateTimeBehavior=convertToNull in the command, but that just gives me a class not found exception. Does anybody know how to get over this?

My 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();
        }
    }

}

My People class:

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;
    }
}

Edit: Adding an example of my current output

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒冷纷飞旳雪 2025-01-24 15:30:47
while (rs.next()) {
    People people = new People();
    people.setBusinessYear(rs.getInt(5));
    // ... all the other fields
    staffs.add(people);
}

在此处阅读:

while (rs.next()) {
    People people = new People();
    people.setBusinessYear(rs.getInt(5));
    // ... all the other fields
    staffs.add(people);
}

Read here: https://docs.oracle.com/en/java/javase/11/docs/api/java.sql/java/sql/ResultSet.html

何以畏孤独 2025-01-24 15:30:47

在里面,当您必须使用 ResultSet 中的实际数据填充 People 对象时:

while (rs.next()) {
    People people = new People();
    people.setSlNo(rs.getInt(1));

    // ... and so on

    staffs.add(people);
}

另外,我建议您将 SQL 中的 * 替换为表 'winter_internship' 中的逗号分隔列列表,因为使用 * 时通常不能保证列顺序。

Inside while you have to fill your People object with actual data from ResultSet:

while (rs.next()) {
    People people = new People();
    people.setSlNo(rs.getInt(1));

    // ... and so on

    staffs.add(people);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文