java.sql.SQLException: No value specified for parameter 2 ,Inserting arraylist to database using JDBC -
i extracting data website using jsoup , storing arraylist. want store data mysql database. code got java.sql.sqlexception: no value specified parameter 2
this class data website.
public class twrapper { arraylist<mobilephone> skrouztphones = new arraylist<>(); protected arraylist<mobilephone> getphonenames() throws ioexception{ (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 = 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] "); for(int =0; i<phoneurls.size(); i++){ mobilephone urls = new mobilephone(); urls.seturl(phoneurls.get(i).absurl("href")); skrouztphones.add(urls); } } return skrouztphones; } } this method store data database.
public static void main(string[] args) throws ioexception, sqlexception{ twrapper wrapper = new twrapper(); try { // create 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 selected database..."); connection conn = drivermanager.getconnection(myurl, "usr", "pass"); system.out.println("connected database successfully..."); // mysql insert statement string query = " insert skrouzt(url,name)" + " values (?, ?)"; preparedstatement updateskrouztps = conn.preparestatement(query); //use wrapper methos insert 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 connection conn.close(); system.out.println("done!!"); } catch(sqlexception se){ //handle errors jdbc se.printstacktrace(); } catch (exception e) { system.err.println("got exception!"); system.err.println(e.getmessage()); } }
your prepared statement needs both strings set before can call executeupdate. in each loop, you're trying set 1, run update, means jdbc ends sql statement like
insert skrouzt(url,name) values ('your url',) with no second value set, statement invalid. if these urls , names match each other, should go through single loop inserting data in pairs.
example requested. note works if lists same length, if they're different lengths database needs designed differently not have them 2 columns in same table.
for (int x = 0; x < skroutzphonesurls.size(); x++) { updateskrouztps.setstring(1, skroutzphonesurls.get(x).geturl()); updateskrouztps.setstring(2, skrouztphonesnames.get(x).getname()); updateskrouztps.executeupdate(); }
Comments
Post a Comment