为什么 DynamicReports 中没有 varchar 数据类型

Why there is no varchar datatype in DynamicReports

我正在使用 DynamicReports 生成 PDF、XLS、DOC 格式的报告。但是我的数据库列是 "varchar" 数据类型。因此,当我在生成 DynamicReports 时使用这些字段时,除了 varchar 之外,还有其他所有数据类型。我需要 varchar 数据类型,因为我的列由逗号、句号和类似的数字组成。那么是否有任何选项可以将数据类型转换或获取为 varchar。

这是我的代码:

public class DynamicReport {
    public static void main(String[] args) {
        Connection connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/marketing_database","root","root");
        } catch (SQLException e) {
            e.printStackTrace();
            return;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }

        JasperReportBuilder report = DynamicReports.report();//a new report
        StyleBuilder plainStyle = stl.style().setFontName("FreeUniversal");

        StyleBuilder boldStyle = stl.style(plainStyle).bold().setBorder(stl.pen1Point());

        report
          .columns(
              Columns.column("Contact Person", "Contact_Person", DataTypes.stringType()))
          .title(//title of the report
                  Templates.createTitleComponent("Fonts"),
              Components.text("FreeUniversal").setStyle(boldStyle)
              .setHorizontalAlignment(HorizontalAlignment.CENTER))
              .pageFooter(Components.pageXofY())//show page number on the page footer
              .setDataSource("SELECT Contact_Person FROM marketing_database.lead", 
                                      connection);

        try {
            report.show();
            report.toPdf(new FileOutputStream("c:/report.pdf"));
        } catch (DRException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
      }
}

如有任何帮助,我们将不胜感激

由于动态报告基于 Java,您可以将 String 数据类型用于 VARCHAR 数据库类型,正如@AlexK 和@KarthikeyanVaithilingam 所建议的那样。

CHAR, VARCHAR, and LONGVARCHAR could have been mapped to either String or char[], but String is more appropriate for normal use. Also, the String class makes conversions between String and char[] easy: There is a method for converting a String object to a char[] and also a constructor for turning a char[] into a String object.

Source