Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8776137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:58:45+00:00 2026-06-13T18:58:45+00:00

I have a homework to retrieve a myqsl query and save it to a

  • 0

I have a homework to retrieve a myqsl query and save it to a ArrayList , and then to link it to another class and then serialize it and send it through http,
In a scheme it would be

class Server{static class a   {try{try{  try{arraylist1}  }}}}

class b {var1,var2,link_to(arraylist1)}

then serialize class b and send it

i managed to take the sql query and save the objects in the ArrayList (objects created from class “Personat”) through

     if (rs != null) {
         List<Personat> perList = new ArrayList<Personat>();
         while (rs.next()) {
            Personat per = new Personat();      
            per.setID(rs.getInt("var1"));
            per.setName(rs.getString("var2"));  
            per.setAmount(rs.getInt("var3"));
            perList.add(per);
         }
     }
     Where rs=ResultSet object

but i cant access the ArrayList from class b so i can serialize it. I have tried to make it static (nothing ,it cant be linked).I have tried to make a getter (yet nothing eclipse wont let me as i automatically generate them).

So i don’t know what i should do ! Can someone help me ? Or does anyone have any idea?
i have tried to search google for this but as you can see is a little too specific so no results until now ….

here is my Server.java

package server2;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.InetSocketAddress;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

 public class Server {
private static List<Personat> perList = new ArrayList<Personat>();
 //need to access this in the SendRes class 
public List<Personat> getPerList() {
    return perList;
}

public static void main(String[] args) throws Exception {

    HttpServer server = HttpServer.create(new InetSocketAddress(3333), 0);

    server.createContext("/", new MyHandler());
    server.setExecutor(null); 
    server.start();
}

static public class MyHandler implements HttpHandler {

    public void handle(HttpExchange t) throws IOException {

        ObjectInputStream ios = new ObjectInputStream(t.getRequestBody());
        //
        final String url = "jdbc:mysql://localhost/httpServer";

        final String user = "root";

        final String password = "";

        try {
            Send oin = (Send) ios.readObject();
            int id = oin.getId();
            String emri = oin.getName();
            int amount = oin.getAmount();
            int paid = oin.getPaid();
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection(url, user,
                        password);

                try {
                    PreparedStatement s = con
                            .prepareStatement("INSERT INTO person(ID,Name,Amount,Paid) VALUES (?,?,?,?)");
                    s.setInt(1, id);
                    s.setString(2, emri);
                    s.setInt(3, amount);
                    s.setInt(4, paid);
                    s.executeUpdate();

                    ResultSet rs = s.executeQuery("SELECT * "
                            + "from personat ORDER BY EmpId");

                    if (rs != null) {

                        while (rs.next()) {
                            Personat per = new Personat();
                            per.setID(rs.getInt("ID"));
                            per.setName(rs.getString("Name"));
                              per.setAmount(rs.getInt("Amount"));

                            perList.add(per);
                        }
                    }

                    //here i need to send an SendRes object     with the ArrayList inside it 

                } catch (Exception e) {

                    e.printStackTrace();
                } finally {
                    if (con != null) {
                        con.close();
                    }
                }

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

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

    }
}
    }

    class SendResponse implements Serializable {
String gabim;
String gabimNr;
    //link the arraylist from class server here
    }

    class Personat {
int ID;

public int getID() {
    return ID;
}

public void setID(int iD) {
    ID = iD;
}

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public int getAmount() {
    return Amount;
}
public void setAmount(int amount) {
    Amount = amount;
}
String Name;
int Amount;
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:58:46+00:00Added an answer on June 13, 2026 at 6:58 pm

    Objects of type B can only access the public members of type A. To get access to your list you need to make it a public member of A. The typical way to do this is to use a private field and a public getter.

    class A
    {
        private List<Personat> personList;
    
        public List<Personat> getPersonList() { return personList; }
    
        public void handle(HttpExchange t) throws IOException
        {
            // ...
            personList = ...;
            // ...
        }
    }
    

    Note that by giving public access to your list you are also allowing clients to modify the contents of the list. You may prefer to give them a copy of the list if this is not desirable.


    On a slightly unrelated note, if you three nested try blocks in a single method then that method is probably too complex and should be refactored into smaller methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this homework that I gotta finish, but then something about it is
I have a homework assigment to iterate through an object array and print out
I have a homework assignment that I am supposed to write a http server
I have homework and I'm supposed to draw a class diagram AND data model.
I have homework that I need to make a custom string class using C
I have a homework problem for my C++ class and the problem wants us
I have a homework problem that asks: Desgin a derived class GraduateStudent from base
I have a homework problem for my algorithms class asking me to calculate the
I have a homework to build an application which will send and receive simple
I have some homework that I was flying through until I got to this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.