Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Java

Exp 5
import java.util.*;
import java.util.Iterator;
public class ex5 {
public static void main(String args[])
{
Hashtable<Integer, String> htbl = new Hashtable<Integer, String>();
htbl.put(6445, "Aditya");
htbl.put(6450, "Sujal");
htbl.put(6464, "Himanshu");
htbl.put(6414, "Rishab");
System.out.println("Display in the Key - Value");
Iterator it1 = htbl.keySet().iterator();
Iterator it2 = htbl.values().iterator();
while(it1.hasNext()){
System.out.println(it1.next()+ " - " + it2.next());
}
}
}

Exp 5.2

import java.util.*;
class ex6{
public static void main(String args[])
{
HashSet<String> set=new HashSet<String>();
set.add("Aditya");
set.add("Sujal");
set.add("Himanshu");
set.add("Dikshant");
System.out.println("An initial list of elements:\n"+set);
}
}
Ex6
import java.util.*;
public class ex6
{
public static void main(String[] args) {
List<String> lst=new ArrayList<String>();
Scanner adi=new Scanner(System.in);
while(true)
{
System.out.println(" 1 To Insert Element \n 2 To Delete Element\n 3 To Display Element\n
4 To Search Element\n 5 To Exit");
int ch=adi.nextInt();
switch(ch)
{
case 1: System.out.println("Enter The Element");
String a=adi.next();
lst.add(a);
break;
case 2: System.out.println("Enter The Element To Remove");
String r=adi.next();
lst.remove(r);
break;
case 3: System.out.println("\n"+lst);
break;
case 4: System.out.println("Enter The Element To Search");
String st=adi.next();
int i=lst.indexOf(st);
if(i==-1)
System.out.println("Element Is Not In List");
else
System.out.println("Element Is At Index "+i);
break;
case 5: System.exit(0);
}

}
}
}
Ex7

class Hare extends Thread {


public void run() {
for (int a=1; a<5; a++) {

System.out.println("Distance covered by HARE: "+a);


}
System.out.println("\n HARE WENT TO SLEEP \n");
try {
Thread.sleep(4000);
} catch (Exception e) {
System.out.println(e);
}
System.out.println("\n HARE BACK TO THE RACE \n");
for (int b = 6; b < 11; b++)
System.out.println("Distance covered by HARE: "+b);
System.out.println("\n HARE LOST THE RACE \n");
}
}
class Tortoise extends Thread {
public void run() {
for (int c = 1; c < 11; c++) {
System.out.println("Distance covered by TORTOISE: "+c);
}
System.out.println("\n TORTOISE HAS WON THE RACE \n");

}
public class ex7 {
public static void main(String[] args) {
Hare h=new Hare();
Tortoise t = new Tortoise();
h.start();
t.start();
}
}
Exp8

import java.sql.*;
import java.util.*;
class test {
public static void main(String args[]) {
Scanner obj = new Scanner(System.in);
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/adi",
"root", "aditya");
while (true) {
System.out.println("Enter 1 To Input Data");
System.out.println("Enter 2 To Display Data");
System.out.println("Enter 3 Exit");
System.out.println("Input Choice: ");
int ch = obj.nextInt();
if (ch == 1) {
System.out.println("Enter Customer Name: ");
String cn = obj.next();
System.out.println("Enter Product Name: ");
String pn = obj.next();
System.out.println("Enter Product Quantity: ");
int qty = obj.nextInt();
String query = "insert into inventorylist values(?,?,?)";
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1, cn);
ps.setString(2, pn);
ps.setInt(3, qty);
ps.execute();
} else if (ch == 2) {
Statement st = con.createStatement();
String sql = "select * from inventorylist";
ResultSet result = st.executeQuery(sql);
System.out.println("-----------------------------------------------------");
while (result.next()) {
System.out.println("Name: " + result.getString("Customer_Name"));
System.out.println("Product:" + result.getString("Product_Name"));
System.out.println("Quantity:" + result.getString("Quantity"));
System.out.println("-----------------------------------------------------");
}
} else if (ch == 3)
break;
}
}
catch (Exception e) {
System.out.print(e);
}

}
}

You might also like