0% found this document useful (0 votes)
1 views6 pages

Java 2

The document contains multiple Java programs that illustrate various concepts such as threading, synchronization, collection classes, and JDBC for database operations. It includes examples of using both the Thread class and Runnable interface for threading, as well as demonstrating the use of ArrayList and LinkedList. Additionally, it covers database operations like selecting, inserting, updating, and deleting records using JDBC.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views6 pages

Java 2

The document contains multiple Java programs that illustrate various concepts such as threading, synchronization, collection classes, and JDBC for database operations. It includes examples of using both the Thread class and Runnable interface for threading, as well as demonstrating the use of ArrayList and LinkedList. Additionally, it covers database operations like selecting, inserting, updating, and deleting records using JDBC.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

​ .

Java program to illustrate the concept of​


1 ​[Link]();​
​threading using Thread Class​ ​}​
​class ThreadDemo extends Thread​ ​}​
​{​ ​}​
​Thread t = new Thread();​ ​}​

​ ublic void run()​


p ​ lass MyThread​
c
​{​ ​{​
​for(int i=1;i<=10;i++)​ ​public static void main(String x[])​
​{​ ​{​
​[Link](i+" "+[Link]());​ ​ThreadDemo2 t1 = new ThreadDemo2();​
​try​ ​ThreadDemo2 t2 = new ThreadDemo2();​
​{​
​[Link](2000);​ ​ hread a = new Thread(t1);​
T
​}​ ​Thread b = new Thread(t2);​
​catch(InterruptedException e)​
​{​ ​ .start();​
a
​[Link]();​ ​[Link]();​
​}​ ​}​
​}​ }​ ​
​}​ ​3. Java program to illustrate the concept of​
​}​ ​Thread Synchronization​
​class reserve implements Runnable​
​ lass ExtendThread​
c ​{​
​{​ ​int available = 1;​
​public static void main(String x[])​ ​int wanted;​
​{​ ​reserve(int i)​
​ThreadDemo t1 = new ThreadDemo();​ ​{​
​[Link]();​ ​wanted = i;​
​}​ ​}​
​}​
​2. Java program to illustrate the concept of​ ​ ublic void run()​
p
​threading using Runnable Interface​ ​{​
​class ThreadDemo2 implements Runnable​ ​synchronized(this)​
​{​ ​{​
​Thread t = new Thread();​ ​[Link]("available berths = "​
​ available);​
+
​ ublic void run()​
p
​{​ ​if(available >= wanted)​
​for(int i=1;i<=10;i++)​ ​{​
​{​ ​String name =​
​[Link](i+" "+[Link]());​ ​[Link]().getName();​
​try​
​{​ ​[Link](wanted +​
​[Link](2000);​ ​" berths reserved for " + name);​
​}​
​catch(InterruptedException e)​ ​try​
​{​ ​{​
​ [Link](2000);​
T ​{​
​available = available - wanted;​ ​Iterator<String> iterator = [Link]();​
}​ ​
​catch(InterruptedException ie)​ ​ hile([Link]())​
w
​{​ ​{​
​}​ ​[Link]([Link]());​
}​ ​ ​}​
​else​ ​}​
​[Link]("sorry no berths");​​}​
}​ ​ ​5. Java program to illustrate collection classes​
}​ ​ ​like LinkedList​
}​ ​ ​import [Link].*;​

​ lass safe​
c ​ ublic class LinkedListDemo​
p
​{​ ​{​
​public static void main(String[] args)​ ​public static void main(String args[])​
​{​ ​{​
​reserve obj = new reserve(1);​ ​LinkedList l1 = new LinkedList();​
​LinkedList l2 = new LinkedList();​
​ hread t1 = new Thread(obj);​
T
​Thread t2 = new Thread(obj);​ l​[Link](10);​
​[Link](20);​
t​[Link]("first person");​ ​[Link](5);​
​[Link]("second person");​ ​[Link](25);​
​[Link](2);​
t​[Link]();​ ​[Link](1);​
​[Link]();​ ​[Link](10);​
​}​ ​[Link](40);​
}​ ​
​4. Java program to illustrate collection classes​ ​[Link](l2);​
​like ArrayList​
​import [Link];​ ​ [Link]("l1 " + l1);​
S
​import [Link];​ ​[Link]("l2 " + l2);​

​ ublic class ArrayListSol3​


p l​[Link]();​
​{​ ​[Link]("remove " + l1);​
​public static void main(String[] args)​
​{​ l​[Link]();​
​ArrayList<String> arr = new ArrayList<>();​ ​[Link]("removelast " + l1);​

​ [Link]("One");​
a l​[Link]();​
​[Link]("Two");​ ​[Link]("removefirst " + l1);​
​[Link]("Three");​
l​[Link](4);​
​printAll(arr);​ ​[Link]("removefirstoccurance​
​}​ "​ + l1);​

​static void printAll(ArrayList<String> list)​ ​[Link](7);​


​ [Link]("removelastoccurance​ ​[Link] a Java program to implement iteration​
S
​" + l1);​ ​over Collection using ListIterator interface​
​import [Link].*;​
​[Link]("getposition " +​ ​public class TreeSetExample​
​[Link](3));​ ​{​
​[Link]("getfirst " +​ ​public static void main(String[] args)​
​[Link]());​ ​{​
​[Link]("getlast " +​ ​ArrayList list = new ArrayList();​
​[Link]());​ ​Random num = new Random();​

​ [Link]("element() " +​
S ​for(int i=0;i<10;i++)​
​[Link]());​ ​[Link]([Link](100));​
​[Link]("peek " + [Link]());​
​[Link]("peekfirst " +​ ​ListIterator collection = [Link]();​
​[Link]());​
​[Link]("peeklast " +​ ​[Link]("\nAll the elements of​
​[Link]());​ ​collection:");​

​[Link]("pop " + [Link]());​ ​while([Link]())​


​[Link]([Link]() + ", ");​
​[Link]("indexofelement " +​
​[Link](5));​ ​[Link]("\nReverse of​
​[Link]("lastindex " +​ ​collection:");​
​[Link](5));​
​}​ ​while([Link]())​
​}​ ​[Link]([Link]() + ",​
​[Link] a Java program to implement iteration​ ​");​
​over Collection using Iterator interface​ ​}​
​import [Link].*;​ ​}​
​public class TreeSetExample​ ​}​
​{​ ​[Link] a Java Program that reads a line of​
​public static void main(String[] args)​ ​integers and displays each integer and the​
​{​ ​sum of all integers using StringTokenizer.​
​TreeSet set = new TreeSet();​ ​import [Link].;​
​Random num = new Random();​ ​import [Link].;​
​class StringTokenizerDemo​
​for(int i=0;i<10;i++)​ ​{​
​[Link]([Link](100));​ ​public static void main(String[] a) throws​
​IOException​
​Iterator collection = [Link]();​ ​{​
​Scanner s = new Scanner([Link]);​
​[Link]("All the elements of​ ​String data,temp;​
​TreeSet collection:");​ ​int tot=0,num;​

​while([Link]())​ ​ [Link]("Enter input values");​


S
​[Link]([Link]() + ", ");​ ​data=[Link]();​
}​ ​
​}​
​ tringTokenizer st = new​
S ​MouseDemo() {​
​ tringTokenizer(data);​
S ​addMouseListener(new MouseAdapter()​
​{​
​ [Link]("Total no. of Tokens:" +​
S ​public void mouseClicked(MouseEvent​
​[Link]());​ ​e) {​
​ [Link]("Mouse​
S
​ hile([Link]())​
w ​Clicked");​
​{​ ​}​
​temp = [Link]();​ ​});​
​num = [Link](temp);​
​tot += num;​ ​ etSize(300, 200);​
s
​setVisible(true);​
​[Link](num);​ ​}​
​}​ ​public static void main(String[] args) {​
​new MouseDemo();​
​[Link]("Total of integers:" + tot);​ ​}​
​}​ ​}​
​}​ ​[Link] program to select entire information​
​[Link] a window application for handling​ ​from the database using JDBC​
​key events use adapter classes to design​ ​import [Link].*;​
​import [Link].*;​ ​class JdbcDemo1​
​import [Link].*;​ ​{​
​public static void main(String x[]) throws​
​public class KeyEventDemo extends Frame {​ ​Exception​
​{​
​KeyEventDemo() {​ ​[Link]("[Link]");​
​addKeyListener(new KeyAdapter() {​ ​Connection conn =​
​public void keyPressed(KeyEvent e) {​ ​[Link](​
​[Link]("Key Pressed: "​ ​"jdbc:mysql://localhost:3306/mydb2",​
​+ [Link]());​ ​"root","");​
​}​
​});​ ​Statement stmt = [Link]();​

​ etSize(300, 200);​
s ​ esultSet rs =​
R
​setVisible(true);​ ​[Link]("select * from student");​
​}​
​ esultSetMetaData rsmd =​
R
​public static void main(String[] args) {​ ​[Link]();​
​new KeyEventDemo();​
​}​ ​int cc = [Link]();​
}​ ​
​[Link] a window application for handling​ ​for(int i=1;i<=cc;i++)​
​mouse events use adapter classes to design​
​import [Link].*;​ ​ [Link]([Link](i)+"\t"​
S
​import [Link].*;​ ​);​

​public class MouseDemo extends Frame {​ ​[Link]();​


​ hile([Link]())​
w ​{​
​{​
​for(int i=1;i<=cc;i++)​ ​[Link]([Link](i)+”\t”);​
​[Link]([Link](i)+"\t");​
​}​
​[Link]();​
​[Link]();​
​}​
}​ ​ ​}​
​}​
​[Link] program to insert values into the​ ​}​
​database using JDBC​
​}​
​import [Link].*;​
​ [Link] program to select a particular​
1
​ lass JdbcDemo2​
c ​record from the database using JDBC​
​{​
​public static void main(String x[]) throws​ ​import [Link].*;​
​Exception​
​{​ ​ lass JdbcDemo3​
c
​[Link]("[Link]");​ ​{​
​public static void main(String x[])​
​ onnection conn =​
C ​throws Exception​
​[Link](​ ​{​
​"jdbc:mysql://localhost:3306/mydb2",​ ​[Link]("[Link]​
​"root","");​ ​er");​

​ reparedStatement pstmt =​
P ​Connection conn =​
​[Link](​
​[Link](​
​"insert into student values(?,?,?)");​

​[Link](1,x[0]);​
​"jdbc:mysql://localhost:3306/mydb2",​
​[Link](2,x[1]);​
​"root","");​
​[Link](3,x[2]);​

​[Link]();​
​ reparedStatement pstmt =​
P
​[Link](​
​ tatement​
S
​"select * from student where rno=?");​
​stmt=[Link]();​

​ esultset​
R ​[Link](1,x[0]);​
​rs=[Link](“select*From​
​student”);​ ​ResultSet rs = [Link]();​

​while([Link]())​ ​ hile([Link]())​
w
​{​
​{​ ​for(int i=1;i<=3;i++)​
​[Link]([Link](i)+"\t");​
​for(int i=1;i<=3;i++)​
​ 5. JAVA PROGRAM TO UPDATE A​
1
​[Link]();​ ​PARTICULAR RECORD IN THE DATABASE​
​}​ ​USING JDBC​
​}​ ​import [Link].*;​

}​ ​ ​ lass JdbcDemo5​
c
​[Link] PROGRAM TO DELETE A​ ​{​
​PARTICULAR RECORD FROM THE​ ​public static void main(String x[]) throws​
​DATABASE USING JDBC​ ​Exception​
​import [Link].*;​ ​{​
​class JdbcDemo4​
​{​ ​[Link]("[Link]");​
​public static void main(String x[]) throws​
​Exception​ ​Connection conn =​
​{​ ​[Link](​
​[Link]("[Link]");​ ​"jdbc:mysql://localhost:3306/mydb2",​
​Connection conn =​ ​"root",​
​[Link](​ ​""​
​"jdbc:mysql://localhost:3306/mydb2",​ ​);​
​"root",​ ​PreparedStatement pstmt =​
​"");​ ​[Link](​
​"update student set name=? where​
​PreparedStatement pstmt =​ ​rno=?"​
​[Link](​ ​);​
​"delete from student where rno=?");​
​ [Link](1, x[0]);​
p
​[Link](1, x[0]);​ ​[Link](2, x[1]);​

​[Link]();​ ​[Link]();​

​Statement stmt = [Link]();​ ​ tatement stmt =​


S
​[Link]();​
​ResultSet rs =​
​[Link]("select * from​ ​ResultSet rs =​
​student");​ ​[Link]("select * from​
​student");​
​ hile([Link]())​
w
​{​ ​while([Link]())​
​for(int i=1; i<=3; i++)​ ​{​
​{​ ​for(int i = 1; i <= 3; i++)​
​[Link]([Link](i) + "\t");​ ​{​
​}​ ​[Link]([Link](i) +​
​[Link]();​ ​"\t");​
​}​ ​}​
​}​ ​[Link]();​
​}​ ​}​
​}​
​}​

You might also like