0% found this document useful (0 votes)
878 views20 pages

1) Write A Java Program To Display IP Address and Name of Client Machine

The document contains 10 programming questions and their solutions in Java. It provides the name, roll number, and course of the student, followed by the question, code solution, and output for each question. The questions cover topics like displaying the IP address and hostname, finding vowels in a string using threads, retrieving data from a database, using threads to display text, inserting records into a database table, deleting database records, counting database records, using threads to blink text, and animating a temple flag using threads.

Uploaded by

Ajinkya Bhagat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
878 views20 pages

1) Write A Java Program To Display IP Address and Name of Client Machine

The document contains 10 programming questions and their solutions in Java. It provides the name, roll number, and course of the student, followed by the question, code solution, and output for each question. The questions cover topics like displaying the IP address and hostname, finding vowels in a string using threads, retrieving data from a database, using threads to display text, inserting records into a database table, deleting database records, counting database records, using threads to blink text, and animating a temple flag using threads.

Uploaded by

Ajinkya Bhagat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Name: Ajinkya Bhagat Roll no: 4308 TY.

BBA(CA)

1] Write a java program to display IP Address and Name of client machine.

import java.net.*;

classClientInfo
{
public static void main(String args[])throws UnknownHostException{
InetAddress i = InetAddress.getByName("localhost");
System.out.println(i);
System.out.println("IP Address:"+i.getHostAddress()+"\nName:"+i.getHostName());
}
}

Output:
Name: Ajinkya Bhagat Roll no: 4308 TY.BBA(CA)

2] Write a multithreading program in java to display all the vowels from a


given String.(Use Thread Class)

importjava.util.*;
class Demo extends Thread
{
String s1;
Demo(String s1)
{
this.s1=s1;
start();
}
public void run()
{
intcnt=0;
for(int i=0;i<s1.length();i++)
{

if(s1.charAt(i)=='A'||s1.charAt(i)=='a'||s1.charAt(i)=='E'||s1.charAt(i)=='e'||s1.charAt(i)=='I'||s1.c
harAt(i)=='i'||s1.charAt(i)=='O'||s1.charAt(i)=='o'||s1.charAt(i)=='U'||s1.charAt(i)=='u')
{
cnt++;
}
}
if(cnt>0)
{
System.out.println("Total Vowels="+cnt);
}
else
{
System.out.println("String not contain Vowels");
}
}
}
class MD
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String");
String s=sc.next();
Demo ob=new Demo(s);
}
}

Output:
Name: Ajinkya Bhagat Roll no: 4308 TY.BBA(CA)

3] Write a JDBC program to displays the details of employees (eno, ename,


department, sal) whose department is “Computer Science”.

importjava.util.*;
importjava.sql.*;
class Demo
{
public static void main(String[]args)throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("Jdbc:Odbc:Database1");
Statement st=con.createStatement();
ResultSetrs=st.executeQuery("select * from emp where dept='computer science'");
while(rs.next())
{
System.out.println("Emp no="+rs.getInt(1));
System.out.println("Emp name="+rs.getString(2));
System.out.println("Empsal="+rs.getInt(3));
System.out.println("EmpDept name="+rs.getString(4));
}
rs.close();
con.close();
}
}

Output:
Name: Ajinkya Bhagat Roll no: 4308 TY.BBA(CA)

4] Write a java program to display “Hello Java” message n times on the


screen. (Use Runnable Interface).

importjava.util.*;
class Time implements Runnable
{
Thread th;
int n;
Time()
{
th=new Thread(this);
Scanner sc=new Scanner(System.in);
System.out.println("Ente the limit");
n=sc.nextInt();
th.start();
}
public void run()
{
for(int i=1;i<=n;i++)
{
System.out.println("Hello java"+i);
}
}
}
class MD
{
public static void main(String[]args)
{
Time ob=new Time();
}
}
Output:
Name: Ajinkya Bhagat Roll no: 4308 TY.BBA(CA)

5] Write a java program to create Teacher table(TNo.TName, Sal, Desg) and


insert a record in it.

importjava.util.*;
importjava.sql.*;
class Demo
{
public static void main(String[]args)throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("Jdbc:ODBC:Database1");
Statement st=con.createStatement();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the teacher no");
int no=sc.nextInt();
System.out.println("Enter the teacher name");
String nm=sc.next();
System.out.println("Enter the Salary");
intsal=sc.nextInt();
System.out.println("Enter the Desigination");
String desig=sc.next();
int k=st.executeUpdate("insert into teacher values("+no+",'"+nm+"',"+sal+",'"+desig+"')");
if(k>0)
{
System.out.println("Record Inserted Successfully");
}
else
{
System.out.println("Error in Record Insert");
}
con.close();
st.close();
}
}
Output:
Name: Ajinkya Bhagat Roll no: 4308 TY.BBA(CA)

6] Write a JDBC program to accept the details of customer (CID, CName,


Address, Ph_No) and store it into the database (Use PreparedStatement
interface)

importjava.sql.*;
importjava.util.*;
class Demo
{
public static void main(String[]args)throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:Database1");
PreparedStatementpst=con.prepareStatement("insert into customer values(?,?,?,?)");
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Customer no");
int no=sc.nextInt();
System.out.println("Enter the Customer name");
String nm=sc.next();
System.out.println("Enter the Customer Address");
String addr=sc.next();
System.out.println("Enter the Customer PHno");
String phno=sc.next();
pst.setInt(1,no);
pst.setString(2,nm);
pst.setString(3,addr);
pst.setString(4,phno);
pst.executeUpdate();
System.out.println("Customer inserted Successfully");
pst.close();
con.close();
}
}
Output:
Name: Ajinkya Bhagat Roll no: 4308 TY.BBA(CA)

7] Write a JDBC program to delete the records of employees whose names are
starting with ‘A’ character.

importjava.sql.*;
class Demo
{
public static void main(String[]args)throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("Jdbc:Odbc:Database1");
Statement st=con.createStatement();
int k=st.executeUpdate("delete from emp where ename like 'a%' ");
if(k>0)
{
System.out.println("Employee Deleted Successfully");
}
else
{
System.out.println("Employee Not Found");
}
con.close();
st.close();
}
}
Output:
Name: Ajinkya Bhagat Roll no: 4308 TY.BBA(CA)

8] Write a JDBC program to count the number of records in table. (Without


using standard method)

importjava.sql.*;
class Demo
{
public static void main(String[]args)throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("Jdbc:Odbc:Database1");
Statement st=con.createStatement();
ResultSetrs=st.executeQuery("select * from emp");
intcnt=0;
while(rs.next())
{
cnt++;
}
System.out.println("Total Record="+cnt);
rs.close();
st.close();
con.close();
}
}
Output:
Name: Ajinkya Bhagat Roll no: 4308 TY.BBA(CA)

9] Write a Multithreading program using Runnable interface to blink Text on


the frame.

importjava.applet.*;
importjava.awt.*;
importjava.awt.event.*;
public class Blink extends Applet implements ActionListener,Runnable
{
int i;
Button b1,b2;
Thread th;
public void init()
{
b1=new Button("Start");
b2=new Button("Stop");
add(b1);add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEventae)
{
if(ae.getSource()==b1)
{
th.start();
}
if(ae.getSource()==b2)
{
th.stop();
}
}
public void run()
{
for(int i=0;i<=1000;i++)
{
try
{
repaint();
th.sleep(20);
}catch(Exception e)
{
System.out.println("Error="+e);
}
}
}
public void paint(Graphics g)
{
if(i%2==0)
{
g.drawString("Welcome",100,100);
}
}
}
/*
<applet code="Blink" width="500" height="500">
</applet> */

Output:
Name: Ajinkya Bhagat Roll no: 4308 TY.BBA(CA)

10] Write a MultiThreading program in java using Runnable interface to


draw temple flag on an applet container.

import java.io.*;
importjava.util.*;
importjava.applet.Applet;
importjava.awt.*;

public class flag extends Applet implements Runnable


{
Thread t;
int x1,x2,x3,y3,x4,y4,x5,ln;
public void init()
{
t=new Thread(this);
t.start();
ln=1;
}
public void run()
{
try{ if(ln==1) { for(x1=200;x1>100;)
{
t.sleep(200);
repaint();
}
}
ln=2;
if(ln==2) { for(x2=100;x2<150;)
{
t.sleep(200);
repaint();
}
}
ln=3;
if(ln==3) { for(x3=150,y3=100;x3>125&&y3<125;)
{
t.sleep(200);
repaint();
}
}
ln=4;
if(ln==4) { for(x4=125,y4=125;x4<150&&y4<150;)
{
t.sleep(200);
repaint();
}
}
ln=5;
if(ln==5) { for(x5=150;x5>100;)
{
t.sleep(200);
repaint();
}
}
ln=1;
}catch(Exception e){
System.out.println(e);
}
run();
}
public void paint(Graphics g)
{
if(ln==1&&x1>100)
{
g.drawLine(100,200,100,x1-=5);
}
if(ln==2&&x2<150)
{
g.drawLine(100,200,100,100);
g.drawLine(100,100,x2+=5,100);
}
if(ln==3&&x3>125&&y3<125)
{
g.drawLine(100,200,100,100);
g.drawLine(100,100,150,100);
g.drawLine(150,100,x3-=5,y3+=5);
}
if(ln==4&&x4<150&&y4<150)
{
g.drawLine(100,200,100,100);
g.drawLine(100,100,150,100);
g.drawLine(150,100,125,125);
g.drawLine(125,125,x4+=5,y4+=5);
}
if(ln==5&&x5>100)
{
g.drawLine(100,200,100,100);
g.drawLine(100,100,150,100);
g.drawLine(150,100,125,125);
g.drawLine(125,125,150,150);
g.drawLine(150,150,x5-=5,150);
}
}
}
/*
<applet code="flag" width="500" height="500">
</applet>
*/

Output:

You might also like