java如何将从数据库提取的对象添加到arraylist?
我编写了一个简单的程序,从数据库中提取一些数据,数据库表中的每一行都是用一个普通构造函数创建的对象来构造的,然后我使用。add()方法。 我的问题是,当我输出我的arraylist包含的内容时,我发现所有的案例都只包含数据库中的最后一行!!我尝试在添加到arraylist时打印,发现每次添加新对象时,它都会出现在arraylist的新旧大小写中,因此最后一个对象(代表数据库表的最后一行)会出现在所有arraylist中! 有什么帮助吗
以下是我的两门课: 客户端。java
public class Client {
public static String nom, prenom, adrs;
private static int id, maxcredit, totalpaye, totalnonpaye;
//i want data to be stored in this arraylist
public static ArrayList<ClientInfo> clients = new ArrayList();
public Client(){
connectDB();//connecting to the database
getClients();//storing the data from the database to arraylist!!
}
private static Connection conn = null;
private static Statement stmt = null;
private static ResultSet rs = null;
private static final String Conn_string = "jdbc:mysql://localhost/carnetcredit";
//Connect to DB:
public static void connectDB(){
try{
conn = DriverManager.getConnection(Conn_string, "root", "");
}catch(SQLException e){
System.err.println(e);
}
}
//Get all clients list:
public static void getClients(){
try{
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery("select * from client");
System.out.println("Table Client : ");
while (rs.next()){
//getting data from database to simpte variables
id = rs.getInt("idclient");
nom = rs.getString(2);
prenom = rs.getString(3);
adrs = rs.getString(4);
maxcredit = rs.getInt(5);
totalpaye = rs.getInt(6);
totalnonpaye = rs.getInt(7);
//creating an object using the data extracted from the database
ClientInfo client = new ClientInfo(id,nom,prenom,adrs,maxcredit,totalpaye,totalnonpaye);
//adding the object to the arraylist
clients.add(client);
}
}catch(SQLException e){
System.err.println(e);
}
}
//::::::::::::::::::::::::::::::::::::::::::::::::::: Main Method :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
public static void main(String[] args) {
conn = null;
stmt = null;
rs = null;
ClientInfo client = new ClientInfo();
new Client();
int i = 0;
while (i<clients.size()){
client = clients.get(i);
System.out.println("id : "+ client.id +" - nom : "+client.nom+" - prenom : "+client.prenom+" - adresse : "+client.adrs+
" - maxcredit : "+client.maxcredit+" - total payé : "+client.totalpaye+" - total non payé : "+client.totalnonpaye);
i++;
}
}
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
}
ClientInfo。java
public class ClientInfo {
public String nom, prenom, adrs;
public int id, maxcredit, totalpaye, totalnonpaye;
public ClientInfo(){
id = 0;
nom = prenom = adrs = "";
maxcredit = totalpaye = totalnonpaye = 0;
}
public ClientInfo(int id, String nom, String prenom, String adrs, int maxcredit, int totalpaye,int totalnonpaye){
this.id = id;
this.nom = nom;
this.prenom = prenom;
this.adrs = adrs ;
this.maxcredit = maxcredit ;
this.totalpaye = totalpaye ;
this.totalnonpaye = totalnonpaye;
}
}
谢谢大家
# 1 楼答案
每当我看到
只包含数据库中的最后一行
我自动知道这是由静态变量引起的
改变
非静态