Web crawler Java Basic article Queryrunner (Ⅲ)

Source: Internet
Author: User

Directory

1Java Operation Database Java operation database The first method Queryrunner method of Java Operation database

1. Java Operation Database

How to store the data crawled by the crawler is also a problem before our attention, the common storage methods are. Txt,mysql,hbase and so on. I have already introduced the Java input stream output stream operation text Way, now mainly introduces several Java operation MySQL way. The first method of Java operational Database

This method is the simplest and most primitive way to manipulate the database.

Package crawlertest;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;

Import java.sql.Statement;  public class Mysqlconnectiontest {public static void main (string[] args) throws ClassNotFoundException, SQLException           {Connection connection=null;                    Acquisition of database connections Statement stmt=null;                 Create statement object ResultSet Rst=null;   Create a database execution object String driver= "Com.mysql.jdbc.Driver";  Database-driven String dburl= "Jdbc:mysql://127.0.0.1:3306/test"; The database address of the operation, port and library name String user = "root"; Database user name String password = "112233";            Database Password Class.forName (driver);
        Driver registration try {connection = drivermanager.getconnection (Dburl,user,password);
        catch (SQLException e) {e.printstacktrace ();
        }//Connection database stmt = Connection.createstatement (); String sql= "SELECT *From auto_forum_comments limit 1,100 ";
        The statement rst=stmt.executequery (SQL) that needs to be queried;              while (Rst.next ()) {System.out.println (rst.getstring (6));
        Take the 6th column data} rst.close ();
        Stmt.close ();
    Connection.close (); }

}
Queryrunner method of Java operation database

Queryrunner is an open source Operation database tool below Apache, it is a very practical Operation database tool, is also I most commonly used a tool. including batch (), update (), insert () and other common data manipulation methods, I use the specific procedures to illustrate these methods.
First, we're going to create an object class (model) to encapsulate the object. As shown in the following figure, we built two classes under model. The address class is the class used in this case, which encapsulates the data in the database (the datasheet that we need to manipulate), as shown in the following illustration.

Package model;
    public class Address {private String addr_id;
    Private String Addr_car;
    Private String Addr_url;
    Private String Addr_forum;
    Private String Craw_time;
    Using shortcut keys in Ecliplse, Shift+alt+s shortcut generation set, get method public String getaddr_id () {return addr_id;
    } public void setaddr_id (String addr_id) {this.addr_id = addr_id;
    Public String Getaddr_car () {return addr_car;
    } public void Setaddr_car (String addr_car) {this.addr_car = Addr_car;
    Public String Getaddr_url () {return addr_url;
    } public void Setaddr_url (String addr_url) {this.addr_url = Addr_url;
    Public String Getaddr_forum () {return addr_forum;
    } public void Setaddr_forum (String addr_forum) {this.addr_forum = Addr_forum;
    Public String Getcraw_time () {return craw_time;
} public void Setcraw_time (String craw_time) {this.craw_time = Craw_time;    }

} 

Next we create a class that encapsulates the database, user name, and password, as shown in the following figure.

Package db;

Import Javax.sql.DataSource;

Import Org.apache.commons.dbcp2.BasicDataSource;

public class myDataSource {public

    static DataSource Getdatasource (String connecturi) {

        Basicdatasource ds = new B Asicdatasource ();
        Ds.setdriverclassname ("Com.mysql.jdbc.Driver");
        Ds.setusername ("root");             
        Ds.setpassword ("112233");                
        Ds.seturl (Connecturi);
        return ds;

    }

The following represents the write operation database statement.

Package crawlertest;
Import java.sql.SQLException;
Import java.util.List;
Import Javax.sql.DataSource; Import model.
address;
Import Org.apache.commons.dbutils.QueryRunner;
Import Org.apache.commons.dbutils.ResultSetHandler;
Import Org.apache.commons.dbutils.handlers.BeanListHandler;
Import Org.apache.commons.logging.Log;

Import Org.apache.commons.logging.LogFactory; Import db.

myDataSource;
    public class Queryrunnertest {static final Log logger = Logfactory.getlog (Queryrunnertest.class);
        public static void Main (string[] args) throws ClassNotFoundException, SQLException {/****** The database to be manipulated ******/
        DataSource ds = Mydatasource.getdatasource ("Jdbc:mysql://127.0.0.1:3306/test");
        Queryrunner qr = new Queryrunner (DS);
        Resultsethandler<list<address>> h = new beanlisthandler<address> (address.class); /****** Execute SQL statement fetch data ******/list<address> cardata = Qr.query ("Select Addr_id,addr_car from Auto_forum_comment
  S ", h);      /****** Traverse output Data ******/for (address Car:cardata) {logger.info ("Carid:" +car.getaddr_id () + "\tcarna"
        Me: "+car.getaddr_car ()); }
    }
}

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.