2010-07-20 39 views
0

我已经能够使用Java和HTMLUnit来刮取网页,但是我不确定如何得到它来编写结果数据传输到远程托管服务器上的MySQL数据库。如果可能的话,我还需要定期(可能每天一次)发生这种情况,而无需手动运行程序。使用Java(HTMLUnit)刮网页,然后将结果写入mySQL数据库

这是可能的,我可以有任何指导,彻底与否,如何做到这一点?

谢谢!

回答

1

让你的网页的HTML到一些字符串变量

String scrapedHtml = ""; 

text场在MySQL中建立一个表

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

使用JDBC代码来连接到数据库,并插入数据。正确设置connectionURL和其他参数

Connection conn = DriverManager.getConnection(connectionURL, user, password); 
PreparedStatement pstmt = 
    conn.prepareStatement("insert into html_store (content) values (?)"); 
pstmt.setString(1, scrapedHtml); 
pstmt.executeUpdate(); 
pstmt.close(); 
conn.close();