简体   繁体   中英

How to retrieve mathematical equation from MySQL using JDBC in java?

I want to retrieve mathematical equations from MySQL database in java and write it to a PDF file using iTextPDF.

I tried setting character encoding to UTF8 in database connection, then I retrieved data in bytes from the database, converted it into String and then added it to my document. The resultant pdf file doesn't have any mathematical symbols. Is java the culprit or iTextPdf?

public class MathematicalEquation {
static final String DEST = "D://Zaid/MathemaicalEquations/test101.pdf";
static final String FONT = "D://Zaid/MathematicalEquations/FreeSans.ttf";
public static void main(String[] args) throws SQLException, DocumentException, IOException {
    File file = new File(DEST);
    file.getParentFile().mkdirs();
    FileOutputStream fos = new FileOutputStream(DEST);
    PdfWriter writer = new PdfWriter(fos);
    PdfDocument pdf = new PdfDocument(writer);
    Document document = new Document(pdf);
    Cell cell = new Cell();
    Table table = new Table(new float[] {50,5});

    BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true);
    Font f = new Font(bf);

    Connection myConn = DriverManager.getConnection("jdbc:mySql://127.0.0.1/MathematicalProblem?useUnicode=yes&characterEncoding=UTF-8", "root", "Mobilen0");
    Statement myStmt = myConn.createStatement();
    ResultSet myRs = null;
    myStmt.executeQuery("SET NAMES 'UTF8'");
    myStmt.executeQuery("SET CHARACTER SET UTF8");

    myRs = myStmt.executeQuery("SELECT * FROM equations");

    byte[] imageBytes=null;
    Blob image1=null;
    ImageData imgData;
    Image image = null;
    Paragraph para = new Paragraph();
    byte[] equation = null;



    while(myRs.next()) {
                equation = myRs.getString("equation").getBytes("UTF-8");
                image1 = myRs.getBlob("image"); 
    }

    if(image1!=null) {
        imageBytes = image1.getBytes(1, (int) image1.length());
        imgData = ImageDataFactory.create(imageBytes);
        image = new Image(imgData);
    }


    String s = Base64.getEncoder().encodeToString(equation);
    String decodedEquation = new String(Base64.getDecoder().decode(s), StandardCharsets.UTF_8);
    para = new Paragraph(String.format("%s%n " , decodedEquation, f));
    cell.add(para).setBorder(Border.NO_BORDER);
    if(image1!=null) {
        cell.add(image.scaleAbsolute(100, 100))
            .setBorder(Border.NO_BORDER);
    }
    table.addCell(cell);
    document.add(table);
    document.close();
    myRs.close();
    myStmt.close();
    myConn.close();
}

}

Output: 4 x 25 ÷ 5 = ?

Expected output is: √4 x √25 ÷ 5 = ?

The output is base64 encoded. You need to decode it first.

Add this decodedEquation to the pdf

String decodedEquation = new String(Base64.getDecoder().decode(equation), StandardCharsets.UTF_8);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM