lundi 30 mai 2016

i writing a code for webcrawling here i m able to fetch data but the prepared statement is not allowning to insert data in mysql Db

the code displays all products and prices just need to insert in db error showing at stmt.setstring(1, title) can any one explain this error The method setString(int, String) in the type PreparedStatement is not applicable for the arguments (int, Elements)

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Set;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

@SuppressWarnings("unused")
public class Main {

    public static DB db = new DB();

    public static void main(String[] args) throws SQLException, IOException {
        db.runSql2("TRUNCATE Record;");
        processPage("http://ift.tt/1UdNndi");
    }

    public static void processPage(String URL) throws SQLException, IOException {

        String url = "http://ift.tt/1UdNndi";
        Document doc = Jsoup.connect(url).timeout(1000 * 100).get();

        Document doc1 = Jsoup.connect("http://ift.tt/1XZr4hu")
                .followRedirects(true).timeout(1000 * 100).get();
        Elements title = doc.getElementsByClass("rttl");
        System.out.println("title is: " + title);

        Elements price = doc1.getElementsByClass("gl-cpr2");
        System.out.println("Price is: " + price);


        String sql = "INSERT INTO  data " + "(Product Name) VALUES " + "(?);";
        ResultSet rs = db.runSql(sql);

        PreparedStatement stmt = db.conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
        stmt.setString(1, title);
        stmt.execute();

        String sql1 = "INSERT INTO  data " + "(Product Name) VALUES " + "(?);";
        ResultSet rs1 = db.runSql(sql);

        PreparedStatement stmt1 = db.conn.prepareStatement(sql1, Statement.RETURN_GENERATED_KEYS);
        stmt.setString(1, price);
        stmt.execute();



    }
}




Aucun commentaire:

Enregistrer un commentaire