java - Insert data into MySQL using JDBC -
i trying insert data mysql database using jdbc. problem in second row data starts end of first! .the picture explains better me. example of code. stored first column , down.
public static void main(string[] args) throws sqlexception { connection conn = null; statement stmt = null; try{ //step 2: register jdbc driver class.forname("com.mysql.jdbc.driver"); //step 3: open connection system.out.println("connecting selected database..."); conn = drivermanager.getconnection("jdbc:mysql://83.212.124.175:3306/zadmin_java?useunicode=yes&characterencoding=utf-8","username", "pass.." ); system.out.println("connected database successfully..."); //step 4: execute query system.out.println("inserting records table..."); stmt = (statement) conn.createstatement(); (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(); //get elements elements phoneurls = mobilephones.select("div[class=details] "); elements phonename = mobilephones.select("div[class=details]"); elements phoneprice = mobilephones.select("p[class=price]"); elements phonerating = mobilephones.select("div[class=rating-wrapper] span"); elements phonespecs = mobilephones.select("p[class=specs]"); //insert urls db for(int = 1; i<phoneurls.size();i++){ string urls = phoneurls.get(i).absurl("href"); string sql = "insert skrouzt(url) " + "values ( '"+urls+"' )"; stmt.executeupdate(sql); } //insert names db for(int =1; i<phonename.size();i++){ string names = phonename.get(i).text(); string insert = "insert skrouzt(name) " + "values ( '"+names+"' )"; stmt.executeupdate(insert); } } system.out.println("inserted records table..."); }catch(sqlexception se){ //handle errors jdbc se.printstacktrace(); }catch(exception e){ //handle errors class.forname e.printstacktrace(); }finally{ //finally block used close resources try{ if(stmt!=null) conn.close(); }catch(sqlexception se){ }// nothing try{ if(conn!=null) conn.close(); }catch(sqlexception se){ se.printstacktrace(); }//end try }//end try system.out.println("goodbye!"); }//end of main method
replace 2 loops 1 this.
int rowcount=math.max(phoneurls.size(),phonename.size()); for(int = 1; i<rowcount;i++){ string urls = (i<phoneurls.size()) ? "'"+phoneurls.get(i).absurl("href")+"'" : "null"; string names = (i<phonename.size()) ? "'"+phonename.get(i).text()+"'" : "null"; string sql = "insert skrouzt(url, name) " + "values ( "+urls+","+names+" )"; stmt.executeupdate(sql); }
Comments
Post a Comment