简体   繁体   English

如何使用Java在MySQL DB中插入HTML?

[英]How to insert HTML in MySQL DB using Java?

我们如何使用Java将网页的完整HTML插入表中?

Do what follows: 请执行以下操作:

Get your page's HTML somehow into a String variable: 以某种方式将页面的HTML放入String变量中:

String fullHtml = "";

Create a table with a text field 创建带有文本字段的表

create table your_html_container (
     id int primary key autoincrement,
     content text not null
);

Insert html content using JDBC: 使用JDBC插入html内容:

Connection conn = DriverManager.getConnection(connectionURL, user, password);
PreparedStatement pstmt = 
    conn.prepareStatement("insert into your_html_container (content) values (?)");
pstmt.setString(1, fullHtml); // this is your html string from step #1
pstmt.executeUpdate();
pstmt.close();
conn.close();

You are done. 大功告成

Use the text type in MySQL. 在MySQL中使用text类型。 Then use a simple Statement / PreparedStatement to insert the text. 然后使用简单的Statement / PreparedStatement插入文本。

What I'd suggest though is to have an html template outside the DB, and fill it with specific data obtained from the DB. 不过我建议的是在数据库外部有一个html模板,并用从数据库获得的特定数据填充它。

If you are putting full HTML pages in the DB, you may want to look into zipping the field. 如果要将完整的HTML页面放入数据库中,则可能需要考虑对字段进行压缩。 Should get pretty substantial space savings... 应该可以节省大量空间...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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