我有一个像这样的 mysql 表
create table students(id varchar(20),
name varchar(20),
address varchar(255)
primary key(id));
我想在jsp中使用每个学生的唯一ID显示数据。我已经尝试过这样的
ArrayList<Object> student = new ArrayList<Object>();
try {
DB_Conn con = new DB_Conn();
Connection c = con.getConnection();
Statement statement = c.createStatement() ;
ResultSet resultset = statement.executeQuery("select * from students");
while(resultset.next()){
student.add(resultset.getString(1));
student.add(resultset.getString(2));
student.add(resultset.getString(3));
}
} catch (SQLException e){
System.out.println(e.getMessage());
}
return student;
现在我想使用每个学生的唯一 ID 显示他们的姓名和地址。怎么这样显示。我如何从数组列表中检索特定的变量数据。
最佳答案
有多种方法可以做到这一点 - 最常见的是
使用两个不同的Arraylist来存储学生的姓名和地址
ArrayList<Object> nameList= new ArrayList<Object>();
ArrayList<Object> addressList= new ArrayList<Object>();
并使用-
nameList.add(resultset.getString(2));
addressList.add(resultset.getString(3));
在 i nameList.get(i)
和 addressList.get(i)
的特定值 int 处,将为您提供特定学生的结果。
关于java - 通过 arraylist JAVA 使用主键添加和检索 MySql 数据库数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29466794/