Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the problem when using updating data using existing table.

First I have the table that have id , now I want try to update the table, so the table have the id and link .

So I write the code to scrape the link and id .After this ,I using update query to update the table and using where clause to compare with the id that already have in the table.
So expected result is should be :

 Name                         | Link               
|-----------------------------|---------------------------
|CDE                          | https://xxxx./abcde  
|ABC                          | https://xxxx./ffcde            


but now is the result is:

 Name                         | Link               
|-----------------------------|---------------------------
|CDE                          | null 
|ABC                          | https://xxxx./ffcde            



It seems cannot update successfully through my code .So how to solve this problem?

*I run print out it is works and have all the id and link. But not idea cannot update into a table.

What I have tried:

The code I try to update the table:

package com;

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

import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;

public class yiutube {
    static String Url;
    static String githubid;
    final String url =
            "https://github.com/STIW3054-A202/class-activity-A202/issues/2";
    public static void main(String[] args) {



        new yiutube().list();
        new yiutube().lists();


    }
    public void list () {

        try {
            final Document document = Jsoup.connect(url) //it's in milliseconds, so this means 5 seconds.
                    .get();

            for (Element row : document.getElementsByClass("author Link--primary css-truncate-target width-fit").subList(1, 23)) {
                if (row.select("[href]").text().equals("")) {
                    continue;
                } else {
                  githubid = row.select("[href]").text();
                    
                    System.out.println(ulink);
                    System.out.println(githubid);


                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    private java.sql.Connection connect () {
        // SQLite connection string
        String url = "jdbc:sqlite:C://sqlite/db/test3.db";
        java.sql.Connection conn = null;
        try {
            conn = DriverManager.getConnection(url);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        return conn;
    }



    public void lists() {
        try {
            final Document document = Jsoup.connect(url).get();


            Elements links = document.select("div.edit-comment-hide tr");
            Elements linkss = links.select("a[href]");
            for (Element link : linkss.subList(0, 22)) {

                Url = link.attr("href");

                yiutube l = new yiutube();
                l.update();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public void update() {
        String sql = "UPDATE uList SET youtubelink = ? " + " WHERE githubId = ?";

        try (java.sql.Connection conn = this.connect();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            // set the corresponding param

            pstmt.setString(1, Url);
            pstmt.setString(2,githubid);
            pstmt.executeUpdate();

            // update
            pstmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }
}
Posted
Updated 8-May-21 22:47pm
v4

1 solution

You are creating the data in your (static) main method. You then create a new yiutube instance and call its add method, but without any parametersd. You should change it so main captures the information and passes the data to add thus:
Java
yiutube t = new yiutube(); // create instance here
for (Element row : document.getElementsByClass("author Link--primary css-truncate-target width-fit").subList(1, 23)) {
    if (row.select("[href]").text().equals("")) {
        continue;
    } else {
        String githubid = row.select("[href]").text();
       
         System.out.println(githubid);
         t.add(githubid, url); // add the data here
         
    }
}

Change the add method to:
Java
public void add(String githubid, String url) {
 
Share this answer
 
Comments
Cow cow Lee 7-May-21 3:54am    
ok, I have try it this code ,but still same result like :

Name | Link
|------|---------------------------
|CDE | https://xxxx./abcde
|ABC | https://xxxx./abcde

How I can insert the link that "different link with different name".

I have the different link but insert it that only one of the link to insert the table
Richard MacCutchan 7-May-21 5:39am    
You need to have a single loop:
For each item that you want to save:
  Capture both fields (githubid and URL)
  Pass them to the add method
Cow cow Lee 7-May-21 5:55am    
can share some code because I not have idea where to insert the single loop and where to pass them to add method
Richard MacCutchan 7-May-21 7:05am    
Are you saying you do not understand your own code? You already have the loop you just need to extract both fields inside it.
Cow cow Lee 7-May-21 7:12am    
just now try doing but still same result ,may you help me take a look ,see what are wrong with my code. Because not familiar with looping. Now I update the question.

It means need doing two loop?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900