Java gets the network file and inserts the database code _java

Source: Internet
Author: User
Tags readline throwable stringbuffer
Get Baidu's song name, singers and links!!
Copy Code code as follows:

Package webtools;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import java.io.UnsupportedEncodingException;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
Import Dbtools.dbtools;
public class Iotoweb {
public string Gethtmlcontent (string htmlurl) {
URL url = null;
String rowcontent = "";
StringBuffer htmlcontent = new StringBuffer ();
try {
url = new URL (htmlurl);
BufferedReader in = new BufferedReader (new InputStreamReader (URL
. OpenStream (), "gb2312"));
while ((rowcontent = In.readline ())!= null) {
Htmlcontent.append (rowcontent);
}
In.close ();
catch (Malformedurlexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (Unsupportedencodingexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return htmlcontent.tostring ();
}
Public List GetLink (String htmlcontent) {
ArrayList Listlink = new ArrayList ();
String regex = "<td[^>]*>[\\ (]*<a[^>]*href=) ([^\"]*) \ |\ ' ([^\ ']*) \ ' | [^\\s>]*)) [^>]*> (. *?) [\\)] *[\\s]*</td> ";
Pattern pattern = pattern.compile (regex, Pattern.dotall);
Matcher Matcher = Pattern.matcher (htmlcontent);
while (Matcher.find ()) {
Listlink.add (Matcher.group ());
}
return listlink;
}
Public list<string> gethref (String htmlcontent) {
String regex;
List listthref = new ArrayList ();
Regex = "href= ([^\"]*) \ |\ ' ([^\ ']*) \ ' | [^\\s>]*)] \ "";
Pattern PA = pattern.compile (regex, Pattern.dotall);
Matcher ma = pa.matcher (htmlcontent);
while (Ma.find ()) {
Listthref.add (Ma.group (). Replacefirst ("href=\", ""). Replace ("\",
""));
}
return listthref;
}
Public list<string> Getperson (String htmlcontent) {
String regex;
List List = new ArrayList ();
Regex = "\" (<a[^>]*href= (\) ([^\ "]*) \ |\ ' ([^\ ']*) \ ' | ( [^\\s>]*)) [^>]*> (. *?) \\)";
Pattern PA = pattern.compile (regex, Pattern.dotall);
Matcher ma = pa.matcher (htmlcontent);
while (Ma.find ()) {
List.add (Ma.group (). Replacefirst ("href=\", ""). Replace ("\", ""));
}
return list;
}
Public list<string> getsongname (String htmlcontent) {
String regex;
List Listperson = new ArrayList ();
Regex = "<a[^>]*href= ([^\"]*) \ |\ ' ([^\ ']*) \ ' | [^\\s>]*)) [^>]*> (. *?) </a>\\s ";
Pattern PA = pattern.compile (regex, Pattern.dotall);
Matcher ma = pa.matcher (htmlcontent);
while (Ma.find ()) {
Listperson.add (Ma.group ());
}
return Listperson;
}
public string Getmaincontent (string htmlcontent) {
String regex = "<table width=\" 100%\ "align=\ center\" cellpadding=\ "0\" cellspacing=\ "0\" class=\ "list\" > (. *?) </table> ";
StringBuffer maincontent = new StringBuffer ();
Pattern pattern = pattern.compile (regex, Pattern.dotall);
Matcher Matcher = Pattern.matcher (htmlcontent);
while (Matcher.find ()) {
Maincontent.append (Matcher.group ());
}
return maincontent.tostring ();
}
public string Outtag (final String s) {
Return S.replaceall ("<.*?>", "");
}
DBTools DBTools = new DBTools ();
public void GetFromBaiduMap3 (String htmlurl) throws Throwable {
HashMap Htmlcontentmap = new HashMap ();
String htmlcontent = gethtmlcontent (Htmlurl);
String maincontent = getmaincontent (htmlcontent);
List Listlink = GetLink (maincontent);
for (int j = 0; J < Listlink.size (); j + +) {
String Tdtag = Listlink.get (j). ToString ();
List songnamelist = Getsongname (Tdtag);
String songname = Outtag (songnamelist.get (0). toString ());
List personlist = Getperson (Tdtag);
String Songperson = "";
if (personlist.size ()!= 0) {
for (int n = 0; n < personlist.size (); n++) {
System.out.println (Personlist.get (n). toString ());
Songperson = Outtag (Personlist.get (n). toString ());
}
} else {
Songperson = "None";
}
System.out.print (Songnamelist.get (0). toString ());
List hreflist = gethref (songnamelist.get (0). toString ());
String songhref = hreflist.get (0). toString ();
System.out.println ();
String sql = INSERT into song (SONGNAME,SONGPERSON,SONGHREF) values (?,?,?);
ArrayList list_values = new ArrayList ();
List_values.add (Songname);
List_values.add (Songperson);
List_values.add (SONGHREF);
Dbtools.update (SQL, list_values);
}
}
}

Dbtools Database Link class:
Copy Code code as follows:

Package dbTools;
Import java.util.ArrayList;
Import java.sql.*;
public class DBTools {
Private PreparedStatement PreparedStatement;
Private ResultSet ResultSet;
Private Connection Connection;
Public DBTools () {
try {
Class.forName ("Com.mysql.jdbc.Driver");
catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
try {
Connection = Drivermanager.getconnection (
"Jdbc:mysql://localhost:3306/testurl", "root", "Zhuyi");
catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Public ArrayList query (String sql, ArrayList list_values) throws Throwable {
ArrayList listRows = new ArrayList ();
PreparedStatement = connection.preparestatement (sql);
for (int i = 0; i < list_values.size (); i++) {
Preparedstatement.setobject (i + 1, list_values.get (i));
}
ResultSet = Preparedstatement.executequery ();
while (Resultset.next ()) {
string[] Rowinfo = new String[resultset.getmetadata ()
. getColumnCount ()];
for (int i = 0; i < rowinfo.length; i++) {
Rowinfo[i] = resultset.getstring (i + 1);
}
Listrows.add (Rowinfo);
}
return listRows;
}
public void Update (String sql, ArrayList list_values) throws Throwable {
PreparedStatement = connection.preparestatement (sql);
for (int i = 0; i < list_values.size (); i++) {
Preparedstatement.setobject (i + 1, list_values.get (i));
}
Preparedstatement.executeupdate ();
Preparedstatement.close ();
}
}

Servlet Call:
Copy Code code as follows:

Package controller;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import java.util.List;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Webtools.iotoweb;
public class Testurl extends HttpServlet {
/**
* constructor of the object.
*/
Public Testurl () {
Super ();
}
/**
* Destruction of the servlet. <br>
*/
public void Destroy () {
Super.destroy (); Just puts "destroy" string in log
Put your code here
}
/**
* The Doget method of the servlet. <br>
*
* This is called when a form has it tag value method equals to get.
*
* @param request
* The request send by the client to the server
* @param response
* The response send by the server to the client
* @throws servletexception
* If an error occurred
* @throws IOException
* If an error occurred
*/
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
try {
Iotoweb iotoweb = new Iotoweb ();
IOTOWEB.GETFROMBAIDUMAP3 ("Http://list.mp3.baidu.com/topso/mp3topsong.html?id=1?top2");
catch (Throwable e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
/**
* The DoPost method of the servlet. <br>
*
* This is called when a form has it tag value method equals to
* Post.
*
* @param request
* The request send by the client to the server
* @param response
* The response send by the server to the client
* @throws servletexception
* If an error occurred
* @throws IOException
* If an error occurred
*/
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Response.setcontenttype ("text/html");
PrintWriter out = Response.getwriter ();
Out
. println ("<! DOCTYPE HTML public \-//w3c//dtd HTML 4.01 transitional//en\ ">");
Out.println ("<HTML>");
Out.println ("Out.println ("<BODY>");
Out.print ("This is");
Out.print (This.getclass ());
Out.println (", using the" POST method);
Out.println ("</BODY>");
Out.println ("</HTML>");
Out.flush ();
Out.close ();
}
/**
* Initialization of the servlet. <br>
*
* @throws servletexception
* If an error occurs
*/
public void Init () throws Servletexception {
Put your code here
}
}

Get the book name of the Golden Book Network:
Copy Code code as follows:

Package webtools;
Import Java.io.BufferedReader;
Import Java.io.InputStreamReader;
Import Java.net.URL;
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
Import Dbtools.dbtools;
public class GetBook {
public string Gethtmlcontent (String htmlurl) throws Throwable {
URL url = null;
String rowcontent = "";
StringBuffer htmlcontent = new StringBuffer ();
url = new URL (htmlurl);
BufferedReader in = new BufferedReader (new InputStreamReader (URL
. OpenStream (), "gb2312"));
while ((rowcontent = In.readline ())!= null) {
Htmlcontent.append (rowcontent);
}
In.close ();
return htmlcontent.tostring ();
}
public string Getbookname (string htmlcontent) {
String bookname = "";
String regex = "<span class=\" style15\ ">[^>]*</span>";
Pattern pattern = pattern.compile (regex, Pattern.dotall);
Matcher Matcher = Pattern.matcher (htmlcontent);
if (Matcher.find ()) {
BookName = Matcher.group ();
}
return bookname;
}
public string Outtag (final String s) {
Return S.replaceall ("<.*?>", "");
}
DBTools DBTools = new DBTools ();
public void Getfromjinshu (String htmlurl) throws Throwable {
String htmlcontent = gethtmlcontent (Htmlurl);
String bookname = Outtag (Getbookname (htmlcontent));
if (bookname!= null &&! "". Equals (BookName)) {
System.out.println (BookName);
String sql = "INSERT into BookInfo (bookname) VALUES (?)";
ArrayList list_values = new ArrayList ();
List_values.add (BookName);
Dbtools.update (SQL, list_values);
}
}
}

Invoke servlet:
Copy Code code as follows:

Package controller;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Webtools.getbook;
public class Testbook extends HttpServlet {
/**
* constructor of the object.
*/
Public Testbook () {
Super ();
}
/**
* Destruction of the servlet. <br>
*/
public void Destroy () {
Super.destroy (); Just puts "destroy" string in log
Put your code here
}
/**
* The Doget method of the servlet. <br>
*
* This is called when a form has it tag value method equals to get.
*
* @param request
* The request send by the client to the server
* @param response
* The response send by the server to the client
* @throws servletexception
* If an error occurred
* @throws IOException
* If an error occurred
*/
int i = 1;
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
GetBook bookinfo = new GetBook ();
for (; i < 10000; i++) {
String Bookurl = "http://www.golden-book.com/booksinfo/12/" + I
+ ". html";
try {
Bookinfo.getfromjinshu (Bookurl);
catch (Throwable e) {
i++;
DoPost (request, response);
}
}
}
/**
* The DoPost method of the servlet. <br>
*
* This is called when a form has it tag value method equals to
* Post.
*
* @param request
* The request send by the client to the server
* @param response
* The response send by the server to the client
* @throws servletexception
* If an error occurred
* @throws IOException
* If an error occurred
*/
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
GetBook bookinfo = new GetBook ();
for (; i < 10000; i++) {
String Bookurl = "http://www.golden-book.com/booksinfo/12/" + I
+ ". html";
try {
Bookinfo.getfromjinshu (Bookurl);
catch (Throwable e) {
i++;
Doget (request, response);
}
}
}
/**
* Initialization of the servlet. <br>
*
* @throws servletexception
* If an error occurs
*/
public void Init () throws Servletexception {
Put your code here
}
}

There are many ways to implement each function, and I hope you can communicate different ideas and methods. can add QQ412546724. Oh

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.