FrameWork Program Collection in Java

DemoAL.java


import java.util.ArrayList;
import java.util.Iterator;


public class DemoAL {


public static void main(String[] args)
{
ArrayList al=new ArrayList();
al.add("JAVA");
al.add("PHP");
al.add("ORACLE");
al.add("ASP.NET");


System.out.println(al);




System.out.println("-------------------------------------");
/*for(String s:al)
{
System.out.println(s);
}*/


Iterator itr=al.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());


}
}


}
-----------------------


DemoALDataBase




import java.util.ArrayList;
import java.sql.*;
class DemoDB
{
String user,pass;
int sno;
DemoDB(String user,String pass,int sno)
{
this.user=user;
this.pass=pass;
this.sno=sno;
}


}
public class DemoALDataBase {




public ArrayList showResultAL()
{
ArrayList al=new ArrayList();


try
{
Class.forName("com.mysql.jdbc.Driver"); //type 4 driver
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/college","root","rat");//connection
System.out.println("Connection successful:"+con);
Statement stmt=con.createStatement();//creating statement for the further
ResultSet res=stmt.executeQuery("select * from login");
while(res.next())
{
String u=res.getString(1);
String p=res.getString(2);
int sno=res.getInt(3);


al.add(new DemoDB(u,p,sno));
}
}
catch(Exception e )
{
}


System.out.println("size is :"+al.size());








return al;
}


public static void main(String args[])
{
DemoALDataBase ob=new DemoALDataBase();
ArrayList al=ob.showResultAL();


DemoDB obj=al.get(2);
System.out.println(obj.user+","+obj.pass+","+obj.sno);


/* for(DemoDB obj:al)
{
System.out.println(obj.user+","+obj.pass+","+obj.sno);
}*/
}


}
-----------------------------
DemoHashMap.java


import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
public class DemoHashMap {
public static void main(String[] args) {
//create HashMap object
HashMap hMap = new HashMap();
//add key value pairs to HashMap
hMap.put("1","ram");
hMap.put("2","shyam");
hMap.put("3","mohan");
/*
get Collection of values contained in HashMap using
Collection values() method of HashMap class
*/
Collection c = hMap.values();
//obtain an Iterator for Collection
Iterator itr = c.iterator();
//iterate through HashMap values iterator
while(itr.hasNext())
System.out.println(itr.next());
}
}
----------------------------
DemoHashMap1.java




import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;


public class DemoHashMap1 {


public static void main(String[] args) {


//create HashMap object
HashMap hMap = new HashMap();


//add key value pairs to HashMap
hMap.put("1","java");
hMap.put("2","php");
hMap.put("3","oracle");


Set s=hMap.keySet();
System.out.println("size is :"+s.size());


String arr[]=new String[s.size()];
Iterator itr=s.iterator();
for(int i=0;i
{
arr[i]=itr.next();
System.out.println(arr[i]+":"+hMap.get(arr[i]));
}


// System.out.println(hMap.get("3"));
}
}

2 comments: