0% found this document useful (0 votes)
13 views

Java All 7 Programs

Uploaded by

madhan13092001
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)
13 views

Java All 7 Programs

Uploaded by

madhan13092001
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
You are on page 1/ 15

DOM

import java.io.File;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

public class DOM {

public static void main(String[] args) {

try {

File inputFile = new File("input.txt");

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

Document doc = dBuilder.parse(inputFile);

doc.getDocumentElement().normalize();

System.out.println("Root Element: " + doc.getDocumentElement().getNodeName());

NodeList nList = doc.getElementsByTagName("student");

System.out.println("------------------------");

for (int temp = 0; temp < nList.getLength(); temp++) {

Node nNode = nList.item(temp);

System.out.println("\nCurrent Element: " + nNode.getNodeName());

if (nNode.getNodeType() == Node.ELEMENT_NODE) {

Element eElement = (Element) nNode;

System.out.println("Student roll no: " + eElement.getAttribute("rollno"));


System.out.println("First name: " +
eElement.getElementsByTagName("firstname").item(0).getTextContent());

System.out.println("Last name: " +


eElement.getElementsByTagName("lastname").item(0).getTextContent());

System.out.println("Nick name: " +


eElement.getElementsByTagName("nickname").item(0).getTextContent());

System.out.println("Marks: " +
eElement.getElementsByTagName("marks").item(0).getTextContent());

} catch (Exception e) {

e.printStackTrace();

<?xml version="1.0" encoding="UTF-8"?>

<students>

<student rollno="1">

<firstname>John</firstname>

<lastname>Doe</lastname>

<nickname>JD</nickname>

<marks>85</marks>

</student>

<student rollno="2">

<firstname>Jane</firstname>

<lastname>Smith</lastname>

<nickname>Jay</nickname>

<marks>90</marks>

</student>

</students>
JDBC

import java.sql.*;

import java.lang.*;

public class JdbcEx

public static void main(String args[])

try{

Class.forName("com.mysql.cj.jdbc.Driver");

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/employee","root","sagar@123");

Statement stmt=con.createStatement();

ResultSet rs=stmt.executeQuery("select*from new_table");

while(rs.next())

System.out.println("Employee ID:"+rs.getInt(1));

System.out.println("Employee name:"+rs.getString(2));

System.out.println("Employee address:"+rs.getString(3));

System.out.println("Employee salary:"+rs.getInt(4));

System.out.println("****************************");

con.close();

}}

catch(Exception e)

System.out.println(e);

}
PRODUCER CONSUMER PROBLEM

class Shop

private static int materials;

private boolean available = false;

public synchronized void put(int value)

while(available == true)

try

wait();

catch(InterruptedException ie)

ie.printStackTrace();

materials=value;

available = true;

notifyAll();

System.out.println("produced value "+materials);

public synchronized void get()

while(available==false)

try
{

wait();

catch(InterruptedException ie)

{}

available = false;

notifyAll();

System.out.println("consumed value "+materials);

class Producer extends Thread

private Shop s;

public Producer(Shop c)

s=c;

public void run()

for(int i=0;i<6;i++)

s.put(i);

class Consumer extends Thread

{
private Shop s;

public Consumer(Shop c)

s=c;

public void run()

int value=0;

for(int i=0;i<6;i++)

s.get();

public class ProdCons

public static void main(String args[])

Shop c = new Shop();

Producer p1 = new Producer(c);

Consumer c1 = new Consumer(c);

p1.start();

c1.start();

}
THREAD

class One extends Thread

public void run()

try

for(int i=0;i<=5;i++)

sleep(1000);

System.out.println("Good Morning");

catch(Exception e)

System.out.println(e);

class Two extends Thread

public void run()

{try

for(int i=0;i<=5;i++)

sleep(2000);

System.out.println("Hello");

}
catch(Exception e)

System.out.println(e);

class Three extends Thread

public void run()

{try

for(int i=0;i<=5;i++)

sleep(3000);

System.out.println("Welcome");

catch(Exception e)

System.out.println(e);

class ThreadMain

public static void main(String args[])

One o=new One();

o.start();

Two t=new Two();


t.start();

Three th=new Three();

th.start();

}
TRAFFIC LIGHT

import java.awt.event.*;

import java.applet.*;

import java.awt.*;

public class TrafficLight extends Applet implements ItemListener

String msg=" ";

Checkbox Red,Green,Yellow;

CheckboxGroup cbg;

public void init()

cbg=new CheckboxGroup();

Red=new Checkbox("Red",cbg,false);

Green=new Checkbox("Green",cbg,false);

Yellow=new Checkbox("Yellow",cbg,false);

add(Red);

add(Yellow);

add(Green);

Red.addItemListener(this);

Green.addItemListener(this);

Yellow.addItemListener(this);

public void itemStateChanged(ItemEvent ie)

repaint();

public void paint(Graphics g)

Color color;

color=Color.black;
g.setColor(color);

g.drawOval(50,50,52,52);

g.drawOval(50,103,52,52);

g.drawOval(50,156,52,52);

String Col=cbg.getSelectedCheckbox().getLabel();

System.out.print(Col);

if(Col.equalsIgnoreCase("Green"))

color = Color.green;

g.setColor(color);

g.fillOval(50,156,52,52);

g.drawString("Go",110,190);

if(Col.equalsIgnoreCase("Red"))

color=Color.red;

g.setColor(color);

g.fillOval(50,50,52,52);

g.drawString("Stop",110,80);

if(Col.equalsIgnoreCase("Yellow"))

color=Color.yellow;

g.setColor(color);

g.fillOval(50,103,52,52);

g.drawString("Ready",110,135);

/* <applet code="TrafficLight.class" width=300 height=300></applet>*/


LIFE CYCLE OF APPLET

import java.applet.Applet;

import java.awt.Graphics;

public class prgm20a extends Applet

public void init()

System.out.println("Initilisation has done here!");

public void start()

System.out.println("Applet started!");

public void paint(Graphics g)

System.out.println("painting");

g.drawString("Bangalore university",100,100);

public void stop()

System.out.println("Applet stopped");

/*

<applet code="prgm20a.class" width="300" height="300">

</applet>

*/
CALCULATOR

import java.awt.*;

import java.awt.event.*;

import java.applet.Applet;

/*

<applet code="Calculator2.class" width=300 height=300>

</applet>

*/

public class Calculator2 extends Applet implements ActionListener {

TextField t;

Button b[] = new Button[10];

Button b1[] = new Button[6];

String op2[] = { "+", "-", "*", "/", "=", "C" };

String str1 = "";

int p = 0, q = 0;

String oper;

public void init() {

setLayout(new BorderLayout());

t = new TextField(30);

t.setEditable(false);

t.setBackground(Color.white);

t.setText("0");

add(t, BorderLayout.NORTH);

Panel buttonPanel = new Panel();

buttonPanel.setLayout(new GridLayout(4, 4));

for (int i = 0; i < 10; i++) {

b[i] = new Button("" + i);

b[i].setBackground(Color.gray);

b[i].addActionListener(this);
buttonPanel.add(b[i]);

for (int i = 0; i < 6; i++) {

b1[i] = new Button(op2[i]);

b1[i].setBackground(Color.gray);

b1[i].addActionListener(this);

buttonPanel.add(b1[i]);

add(buttonPanel, BorderLayout.CENTER);

public void actionPerformed(ActionEvent ae) {

String str = ae.getActionCommand();

if (str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/")) {

p = Integer.parseInt(t.getText());

oper = str;

t.setText("");

} else if (str.equals("=")) {

q = Integer.parseInt(t.getText());

if (oper.equals("+")) {

t.setText(String.valueOf(p + q));

} else if (oper.equals("-")) {

t.setText(String.valueOf(p - q));

} else if (oper.equals("*")) {

t.setText(String.valueOf(p * q));

} else if (oper.equals("/")) {

t.setText(String.valueOf(p / q));

} else if (str.equals("C")) {

t.setText("0");

p = 0;
q = 0;

oper = "";

} else {

if (t.getText().equals("0")) {

t.setText(str);

} else {

t.setText(t.getText() + str);

You might also like