开发者

How to insert HTML in MySQL DB using Java?

How can we insert full 开发者_StackOverflow社区HTML of a webpage into my table using Java?


Do what follows:

Get your page's HTML somehow into a String variable:

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:

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. Then use a simple Statement / PreparedStatement to insert the text.

What I'd suggest though is to have an html template outside the DB, and fill it with specific data obtained from the DB.


If you are putting full HTML pages in the DB, you may want to look into zipping the field. Should get pretty substantial space savings...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜