0

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://www.ebay.in/rpp/deals/electronics/electronics/mobiles/?_ipg=192");
    }

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

        String url = "http://www.ebay.in/rpp/deals/electronics/electronics/mobiles/?_ipg=192";
        Document doc = Jsoup.connect(url).timeout(1000 * 100).get();

        Document doc1 = Jsoup.connect("http://www.ebay.in/rpp/deals/electronics/electronics/mobiles/?_ipg=192/")
                .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();



    }
}

1 Answers1

0

Your data types didn't match.

Calling getElementsByClass() can give you many results.

The setString() method expects a single string.

You will probably want to loop through the Elements objects (price and title) and process the results one by one.

Here are some examples of batch inserts using PreparedStatement: Java: Insert multiple rows into MySQL with PreparedStatement

Community
  • 1
  • 1
Peter M.
  • 713
  • 1
  • 5
  • 14