Java 如何在JDBC中使用列表

Java 如何在JDBC中使用列表,java,postgresql,jdbc,Java,Postgresql,Jdbc,我需要从3个不同的表中检索数据。最初,我在查询中使用“连接”来获得结果。三个表中的一个包含大量数据,这使得我的程序运行非常缓慢。因此,我将查询分为两部分。 1) 我的一个查询返回字符串列表,而另一个查询应该返回整数 现在我的疑问是如何在我的查询中使用这个列表 我已经尝试了以下代码,但它会引发异常。。请就此向我提出建议 1.Method1:返回一个列表 public ArrayList<String> custList() { ArrayList<String> cu

我需要从3个不同的表中检索数据。最初,我在查询中使用“连接”来获得结果。三个表中的一个包含大量数据,这使得我的程序运行非常缓慢。因此,我将查询分为两部分。 1) 我的一个查询返回字符串列表,而另一个查询应该返回整数

现在我的疑问是如何在我的查询中使用这个列表

我已经尝试了以下代码,但它会引发异常。。请就此向我提出建议

1.Method1:返回一个列表

public ArrayList<String> custList()
{
   ArrayList<String> customerList=new  ArrayList<String>();
 try {
        Class.forName("org.postgresql.Driver");
       conection=DriverManager.getConnection("","");
        Statment =coonection.createstatement;
       String CustomerInfo="select username, place from customer where customerID >=100 
              and customerID <=200";

       ResultSet result1=statement.executeQuery(CustomerInfo);
       while(result.next)
          {
           String cust=rs.getString(username);
           customerList.add(cust);
            }

        }
      //required catch exceptions
      //close resultset,connection,statement
     return customerList;

 }
public ArrayList custList()
{
ArrayList customerList=新的ArrayList();
试一试{
Class.forName(“org.postgresql.Driver”);
conction=DriverManager.getConnection(“,”);
station=coonnection.createstatement;
String CustomerInfo=“选择用户名,从customerID>=100的客户处放置
和客户ID解决方案
第一步
使用实例变量(比如EID和ENAME)创建一个类,并编写setter和getter(比如类名为Employee)

步骤2 现在编写一个类(比如RetriveRecords)

步骤3
完成…实际上是为了克服ResultSet对象的限制(即ResultSet对象无法传输)…Hibernate ORM在内部使用相同的代码。

可能创建一个
视图
,在数据库中执行查询并执行
select*from view\u name
?我猜您的查询运行缓慢,因为它在执行查询时连接了大表+过滤行。可能会有所帮助。
     public int useCount()
        {
        int count=0;

         //Using a method of same class, assuming my class name is customer
         ArrayList<String> custtDetails=customer.custList();
     try {
        Class.forName("org.postgresql.Driver");
       conection=DriverManager.getConnection("","");
        Statment =coonection.createstatement;

       String C=custtDetails.get(count);
       String CustomerInfo="select count(*) login  from customerdetails 
               where custUsername=C"; // <-- I need to use my list of strings here       

       ResultSet result1=statement.executeQuery(CustomerInfo);
       while(result.next)
          {
           String cust=rs.getString(username);
           customerList.add(cust);
            count++; // <--- I think my mistake goes here ! How to use my list now?? 
            }

        }
      //required catch exceptions
      //close resultset,connection,statement
     return useCount;

 }

         }
public class Employee implements java.io.Serializable {

private Integer eid;
private String ename;


public Integer getEid() {
    return eid;
}

public void setEid(Integer eid) {
    this.eid = eid;
}

public String getEname() {
    return ename;
}

public void setEname(String ename) {
    this.ename = ename;
}
}
public class RetriveRecords {

 public ArrayList listMethod(){
  ArrayList list = new ArrayList();
 try {
        //Standard JDBC Code
    Statement stmt = con.createStatement();
    String query = "select * from emp"; // Your own Query

    ResultSet rs = stmt.executeQuery(query);
    while(rs.next()) {

            Employee e = new Employee();
            e.setEno(rs.getInt(eno);
            e.setEName(rs.getString("ename");
            list.add(e);
}
   con.close();      
}
catch(Exception e){
    e.printStackTrace();
}
  return list;
}
}
public class DisplayRecords {
 public static void main(String[] args){

        RetriveRecords rr = new RetriveRecords();
        ArrayList a = rr.listMethod();
        Iterator i = a.iterator();

       while(i.hasNext()){

              Employee e =(Employee) i.next();
              System.out.println(e.getEno());
              System.out.println(e.getName());
}
}
}