[英]How to access and ArrayList inside another class inside multiple try's?
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 我有一个作业来检索myqsl查询并将其保存到
ArrayList
,然后将其链接到另一个类,然后对其进行序列化并通过http发送,在一种方案中
class Server{static class a {try{try{ try{arraylist1} }}}}
class b {var1,var2,link_to(arraylist1)}
then serialize class b and send it 然后序列化类b并将其发送
i managed to take the sql query and save the objects in the ArrayList (objects created from class "Personat") through 我设法通过sql查询并将对象保存在ArrayList中(通过“ Personat”类创建的对象)通过
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. 但是我无法从类b访问
ArrayList
,所以我可以对其进行序列化。 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 ....
我曾尝试在Google上搜索此内容,但是如您所见,它有点太具体了,所以到目前为止没有结果。
here is my Server.java 这是我的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;
}
Objects of type B
can only access the public members of type A
. 类型
B
对象只能访问类型A
的公共成员。 To get access to your list you need to make it a public member of A
. 要访问您的列表,您需要使其成为
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. 需要注意的一点是,如果在一个方法中嵌套三个try块,则该方法可能太复杂了,应将其重构为较小的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.