2

I have SQL query which results in multiple rows and columns.
I want to execute this query and get the results into List<String> instead of the ResultSet.

"select LectureDay, LectureStatus from lecturelist where LectureName like "Java%"; 
res = pstmt.executeQuery();

I want the results of the query in List<String>

public static List<String> ResultList;

while statements look like this

    while(res.next())
            {
                String LectureStatus = res.getString("LectureStatus");
                String LectureDay = res.getString("LectureDay");
            }
            res.close();

code:

public void GetData(){
String url = "jdbc:mysql://localhost/DB?serverTimezone=UTC";
String sql = "select LectureDay, LectureStatus from lecturelist where LectureName like 'Java%'";
pstmt = conn.prepareStatement(sql);
res = pstmt.executeQuery();
try 
        {           
            Class.forName(driver_name);
            conn = DriverManager.getConnection(url, user, password);
            pstmt = conn.prepareStatement(sql);
            while(res.next())
            {
                String LectureStatus = res.getString("LectureStatus");
                String LectureDay = res.getString("LectureDay");
            }
            res.close();
        catch (Exception e) 
        {
            System.out.println(e.getMessage());
        }       
        finally {
                try {
                    pstmt.close();
                    conn.close();
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
        }
    }
}

thank u for read this

13
  • Post the complete code Commented May 5, 2022 at 14:45
  • @OMiShah now i post that function code. does it enough to figure out the problem? if not tell me. Commented May 5, 2022 at 15:00
  • what you want like List<String> of LectureStatus or List<String> of LectureDay? Commented May 5, 2022 at 15:08
  • @yahitesh i want to add all of them in one list<string>. Or it's ok to store in separate list<string> Commented May 5, 2022 at 15:10
  • do you want it as one String list? Commented May 5, 2022 at 15:10

1 Answer 1

1
    public void GetData(){
    String url = "jdbc:mysql://localhost/DB?serverTimezone=UTC";
    String sql = "select LectureDay, LectureStatus from lecturelist where LectureName like "Java%";
    pstmt = conn.prepareStatement(sql);
    res = pstmt.executeQuery();
    List<String> list = new ArrayList<>();
    try
    {
        Class.forName(driver_name);
        conn = DriverManager.getConnection(url, user, password);
        pstmt = conn.prepareStatement(sql);
        while(res.next())
        {
            String LectureStatus = res.getString("LectureStatus");
            String LectureDay = res.getString("LectureDay");
            list.add(LectureStatus);
            list.add(LectureDay);
        }
        res.close();
    catch (Exception e)
        {
            System.out.println(e.getMessage());
        }       
    finally {
        try {
            pstmt.close();
            conn.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Just a wondering. If i want to use static List<string> list. Does it work same way?
yes! but then you need to declare List<String> list = new ArrayList<>(); outside the method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.