【问题标题】:How to retrieve mathematical equation from MySQL using JDBC in java?java - 如何在Java中使用JDBC从MySQL中检索数学方程?
【发布时间】:2020-01-10 14:24:54
【问题描述】:

我想在 java 中从 MySQL 数据库中检索数学方程,并使用 iTextPDF 将其写入 PDF 文件。

我尝试在数据库连接中将字符编码设置为 UTF8,然后我从数据库中检索以字节为单位的数据,将其转换为字符串,然后将其添加到我的文档中。生成的 pdf 文件没有任何数学符号。罪魁祸首是 java 还是 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();
}

}

输出: 4 x 25 ÷ 5 = ?

预期输出为: √4 x √25 ÷ 5 = ?

【问题讨论】:

  • 请创建一个 www.sqlfiddle.com 示例。
  • 输出是“Storedas Question10Storedas Question10”的base64,甚至不像预期的输出。因此,要么您检索到错误的列,要么您插入了不正确的数据。表格行中的数据是什么?
  • @k5 我选择了错误的列。这是我的输出:4oiaNCB4IOKImjI1IMO3IDUgPSA/
  • 你为什么将方程编码为base64,然后立即从base64解码?你期望那次往返旅行能完成什么?如果 ResultSet.getString 没有返回有效字符,则需要修复 JDBC 连接的参数。在任何情况下,您都不应该尝试在其他任何地方修复它,而且 Base64 转换无论如何也帮不了您。
  • 另见dev.mysql.com/doc/connector-j/8.0/en/…,它明确警告:“不要使用Connector/J发出查询SET NAMES”

标签: java mysql jdbc itext


【解决方案1】:

输出是 base64 编码的。您需要先对其进行解码。

将此decodedEquation添加到pdf中

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

【讨论】:

  • 添加此代码后,输出为: √4 x √25 ÷ 5 = ?
  • 你是如何创建你的字体实例的?
  • @k5 static final String FONT = "D://Zaid/MathematicalEquations/FreeSans.ttf"; BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true);字体 f = new Font(bf);
  • 看起来您在其中混合了一些其他编码。字体定义很好。您可以将您当前的代码从选择添加到cell.add(para)
猜你喜欢
  • 1970-01-01
  • 2016-08-30
  • 1970-01-01
  • 1970-01-01
  • 2012-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-19
相关资源
最近更新 更多