I am extracting some data from website using jsoup and storing to arraylist. Now i want to store the data to mysql database. This is my code but i got this java.sql.SQLException: No value specified for parameter 2
This is my class to get data from the website.
public class Twrapper {
ArrayList<MobilePhone> skrouztPhones = new ArrayList<>();
protected ArrayList<MobilePhone> getPhoneNames() throws IOException{
for (int j=1; j<=1;j++){
Document mobilePhones = Jsoup.connect("http://www.skroutz.gr/c/40/kinhta-thlefwna.html?order_dir=asc&page=" + j).userAgent("Mozilla").get();
Elements phoneName = mobilePhones.select("div[class=details]");
for(int i = 1; i<phoneName.size();i++){
MobilePhone names = new MobilePhone();
names.setName(phoneName.get(i).text());
skrouztPhones.add(names);
}
}
return skrouztPhones;
}
protected ArrayList<MobilePhone> getPhonesUrls() throws IOException{
for(int j =1; j<=1;j++){
Document mobilePhone = Jsoup.connect("http://www.skroutz.gr/c/40/kinhta-thlefwna.html?order_dir=asc&page=" + j).userAgent("Mozilla").get();
Elements phoneUrls = mobilePhone.select("div[class=details] a ");
for(int i =0; i<phoneUrls.size(); i++){
MobilePhone urls = new MobilePhone();
urls.setUrl(phoneUrls.get(i).absUrl("href"));
skrouztPhones.add(urls);
}
}
return skrouztPhones;
}
}
This is my method to store the data to database.
public static void main(String[] args) throws IOException, SQLException{
Twrapper wrapper = new Twrapper();
try
{
// create a mysql database connection
String myDriver = "org.gjt.mm.mysql.Driver";
String myUrl = "jdbc:mysql://83.212.124.175:3306/zadmin_java?useUnicode=yes&characterEncoding=UTF-8";
Class.forName(myDriver);
System.out.println("Connecting to a selected database...");
Connection conn = DriverManager.getConnection(myUrl, "usr", "pass");
System.out.println("Connected database successfully...");
// the mysql insert statement
String query = " INSERT INTO skrouzt(url,name)"
+ " values (?, ?)";
PreparedStatement updateSkrouztPs = conn.prepareStatement(query);
//use the wrapper methos to insert to db
ArrayList<MobilePhone> skroutzPhonesUrls = wrapper.getPhonesUrls();
for(MobilePhone phone: skroutzPhonesUrls){
String urls = phone.getUrl();
updateSkrouztPs.setString(1, urls);
updateSkrouztPs.executeUpdate();
}
ArrayList<MobilePhone> skrouztPhonesNames = wrapper.getPhoneNames();
for(MobilePhone phone: skrouztPhonesNames){
String names = phone.getName();
updateSkrouztPs.setString(2, names);
updateSkrouztPs.executeUpdate();
}
//close the connection
conn.close();
System.out.println("Done!!");
}
catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}
catch (Exception e)
{
System.err.println("Got an exception!");
System.err.println(e.getMessage());
}
}
updateSkrouztPs.setString(1, urls);and you do executeUpdate(), you get an error because u dont specify parameter 2