Search code examples
javajasper-reportsdynamic-reports

Why there is no varchar datatype in DynamicReports


I am using DynamicReports to generate a report in PDF,XLS,DOC. But my database columns are in "varchar" datatype. So when I am using those fields in generating DynamicReports there is every other datatype other than varchar. I need varchar datatype because my column consists of commas, fullstops, numbers like these. So is there any option to convert or to get the datatype as varchar.

Here is my code:

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

Any help in this will be appreciated


Solution

  • Since Dynamic Reports is based on Java, you can use the String data type for VARCHAR db types, as @AlexK and @KarthikeyanVaithilingam suggested.

    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