public static void getContent(String url, String leftlang, String rightlang) throws IOException{
String content = getString(url);
org.jsoup.nodes.Document doc = Jsoup.parse(content);
Connection conn = null;
Statement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
links = doc.select(".section-block .result-block");
int count = 0;
for (Element element : links) {
if(idList.size() > count)
{
int id = idList.get(count);
count++;
int rowcount = 0;
Elements rows = element.select(".result-row");
String prepstr = "Insert into word1 (devinition, word_id, language, type, row, description) "
+ "Values(?, ?, ?, ?, ?, ?) ";
PreparedStatement prepStmt = conn.prepareStatement(prepstr);
for (Element row : rows) {
rowcount++;
if(!row.select(".result-left strong").isEmpty()){
String description = row.select(".result-left span").toString();
if(description.indexOf("(") > 0 && description.indexOf("(") < description.indexOf(")"))
description = description.substring(description.indexOf("("), description.indexOf(")"));
else
description = "";
prepStmt.setString(1, row.select(".result-left strong").text());
prepStmt.setInt(2, id);
prepStmt.setString(3, leftlang);
prepStmt.setString(4, row.select(".result-left abbr").text());
prepStmt.setInt(5, rowcount);
prepStmt.setString(6, description);
prepStmt.addBatch();
}
if(!row.select(".result-right .result-link").isEmpty()){
String description = row.select(".result-right span").toString();
if(description.indexOf("(") > 0 && description.indexOf("(") < description.indexOf(")"))
description = description.substring(description.indexOf("("), description.indexOf(")"));
else
description = "";
prepStmt.setString(1, row.select(".result-right .result-link").text());
prepStmt.setInt(2, id);
prepStmt.setString(3, rightlang);
prepStmt.setString(4, row.select(".result-right abbr").text());
prepStmt.setInt(5, rowcount);
prepStmt.setString(6, description);
prepStmt.addBatch();
}
}
prepStmt.executeBatch();
try{
String sql = "update link set issave = true where link.link = ?";
PreparedStatement prepsql = conn.prepareStatement(sql);
prepsql.setString(1, url);
prepsql.executeUpdate();
}
catch(Exception ex)
{
System.err.println(url);
}
}
}
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
}