package datasourcetest;


import java.util.ArrayList;
import javax.naming.Context;
import javax.sql.DataSource;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.naming.InitialContext;

public class UsersList {

  public void getUsers(int startid,int count)
  {
    DataSource ds=null;
    Context ctx=null;
    Connection conn=null;

    Statement stmt=null;
    ResultSet rs=null;
    ArrayList users = new ArrayList();

   try{

      ctx=getInitialContext();
      ds=(DataSource)ctx.lookup("MyDataSource");

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

  try{
      conn=ds.getConnection();
      stmt=conn.createStatement();
      rs=stmt.executeQuery("select * from user order by username");

      while(startid-->0&&rs.next());

    while(count-->0&&rs.next())
    {
    System.out.println("username:" + rs.getString("username"));
    System.out.println("password:" + rs.getString("password"));
    }

    }catch(SQLException e){e.printStackTrace();}
    finally{
      try{
        if(stmt!=null){stmt.close();}
        if(conn!=null){conn.close();}

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

      }
  }

  private static Context getInitialContext()
  {

    String url="t3://localhost:7001";
    String user="system";
    String password="11111111";
    Properties properties=null;
    Context context=null;

  try
  {
    properties = new Properties();
    properties.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
    properties.put(Context.PROVIDER_URL,url);
    properties.put(Context.SECURITY_PRINCIPAL,user);
    properties.put(Context.SECURITY_CREDENTIALS,password);
    context = new InitialContext(properties);

  }

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

 return context;
}


}
