Sie sind auf Seite 1von 13

NAME : PRIYA MITTAR GURUNG

SEAT NO :

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


machine.
SERVER:

import java.io.*;
import java.net.*;

class S_Slip1_1
{
public static void main(String a[]) throws Exception
{
ServerSocket ss = new ServerSocket(1000);
System.out.println("Server is waiting for client : ");
Socket s =ss.accept();
System.out.println("Client is connected");
}
}

CLIENT:

import java.io.*;
import java.net.*;

class C_Slip1_1
{
public static void main(String a[]) throws Exception
{
Socket ss = new Socket("localhost",1000);
System.out.println("IP Address = "+ss.getInetAddress());
System.out.println("Client port = "+ss.getPort());
}
}
2. Write a multithreading program in java to display all the vowels from a given
String.(Use Thread Class)
import java.util.*;
import java.io.*;

class Slip2_1 extends Thread


{
String s1;
Slip2_1(String s)
{ s1=s;
start();

}
public void run()
{
System.out.println("Vowels are ");
for(int i=0;i<s1.length();i++)
{
char ch=s1.charAt(i);
if(ch=='a'||ch=='e'||
ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
System.out.print(" "+ch);
}
}

public static void main(String a[]) throws Exception


{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter a string");
String str=br.readLine();
Slip2_1 v=new Slip2_1(str);
}
}
3.Write a JDBC program to display the details of employees (eno, ename, department,
sal) whose department is �Computer Science�.
import java.sql.*;
public class Slip3_1
{
public static void main(String args[]){
Connection con;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection("jdbc:odbc:dsn");
if(con==null)
{
System.out.println("Connection
Failed....");
System.exit(1);
}

Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * From
employee Where dept='computer science'");

System.out.println("eno\t"+"ename\t"+"department\t"+"sal");
while(rs.next())
{

System.out.println("\n"+rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+""+r
s.getInt(4));
}
con.close();
rs.close();
stmt.close();
}

catch(Exception e)
{
System.out.println(e);
}
}
}
4.Write a client server programs which displays the server machines date and time
on the client machine.
/* Server_Slip14_1*/

import java.net.*;
import java.io.*;
import java.util.Date;

public class Server_Slip14_1


{
public static void main(String g[])throws UnknownHostException,
IOException
{
ServerSocket ss=new ServerSocket(4444);
System.out.println("server started");
Socket s=ss.accept();
System.out.println("Client connected");
Date d=new Date();
int m=d.getMonth();
m=m+1;
int y=d.getYear();
y=y+1900;
String time=""+d.getHours()+":"+d.getMinutes()
+":"+d.getSeconds()+" Date is "+d.getDate()+"/"+m+"/"+y;
OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
dos.writeUTF(time);
}
}

/* client_Slip14_1*/

import java.net.*;
import java.io.*;
import java.util.*;
public class Client_Slip14_1
{
public static void main(String args[]) throws Exception
{
Socket s = new Socket("localhost",4444);
DataInputStream dos = new DataInputStream(s.getInputStream());
String time = dos.readUTF();
System.out.println("Current date and time is "+time);
}
}
5. Write a JDBC program to accept the details of customer (CID, CName, Address,
Ph_No) and store it into the database (Use PreparedStatement interface)

import java.sql.*;
import java.io.*;
class Slip6_1
{
public static void main(String a[])
{
PreparedStatement ps;
Connection con;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection("jdbc:odbc:dsn");
if(con==null)
{
System.out.println("Connection
Failed......");
System.exit(1);
}
System.out.println("Connection
Esatablished......");
Statement stmt=con.createStatement();

BufferedReader br = new BufferedReader(new


InputStreamReader(System.in));
String query="insert into Customer
values(?,?,?,?)";
ps=con.prepareStatement(query);

System.out.println("Customer Details....");
System.out.println("Enter CID");
int cid=Integer.parseInt(br.readLine());

ps.setInt(1,cid);
System.out.println("Enter name");
String name=br.readLine();
ps.setString(2,name);
System.out.println("Enter Address");
String add=br.readLine();
ps.setString(3,add);
System.out.println("Enter Ph_No");
String phno=br.readLine();
ps.setString(4,phno);

int no=ps.executeUpdate();
if(no!=0)
System.out.println("Data inserted
succesfully.....");
else
System.out.println("Data NOT
inserted");
con.close();

}
catch(Exception e)
{
e.printStackTrace();
}
}
}

6. Write a Multithreading program using Runnable interface to blink Text on the


frame.
import java.awt.*;
import java.awt.event.*;

class Slip8_1 extends Frame implements Runnable


{
Thread t;
Label l1;
int f;
Slip8_1()
{
t=new Thread(this);
t.start();
setLayout(null);
l1=new Label("Hello JAVA");
l1.setBounds(100,100,100,40);
add(l1);
setSize(300,300);
setVisible(true);
f=0;
}
public void run()
{
try
{
if(f==0)
{
t.sleep(200);
l1.setText("");
f=1;
}
if(f==1)
{
t.sleep(200);
l1.setText("Hello Java");
f=0;
}
}
catch(Exception e)
{
System.out.println(e);
}
run();
}
public static void main(String a[])
{
new Slip8_1();
}
}
7. Write a Multithreading program in java to display the number�s between 1 to 100
continuously in a TextField by clicking on button. (use Runnable Interface).
import java.awt.event.*;
import javax.swing.*;

class Message implements Runnable


{
JTextField t;
public void run()
{
for(int i =1; i<=100;i++)
{
t.setText(""+i);
try
{
Thread.sleep(50);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
class Slip12_1 implements ActionListener
{
JFrame f;
JPanel p;
JTextField t;
JButton b;
Thread t1;

Slip12_1()
{
f = new JFrame();
p = new JPanel();

t = new JTextField(60);
b = new JButton("Start");

t1 = new Thread(this);

b.addActionListener(this);

p.add(t);
p.add(b);

f.add(p);
f.setSize(400, 400);
f.setVisible(true);
}

public void actionPerformed(ActionEvent e)


{
t1.start();
}
}

8. Write a JDBC program to create a Mobile (Model_No, Company_Name, Price, Color)


table and insert a record in it.
import java.sql.*;
class Slip13_1
{
public static void main(String a[])
{
Connection con;
Statement stmt;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection("jdbc:odbc:dsn");
if(con==null)
{
System.out.println("Connection
Failed");
System.exit(1);
}
System.out.println("Connection
Established....");
stmt=con.createStatement();

String sql="create table mobile"+"(Model_No


int,"+"Company_Name varchar(20),"+"price float(6,2),"+"color varchar(20))";
stmt.executeUpdate(sql);
System.out.println("Table Created
sucessfully...");

con.close();

}
catch(Exception e)
{
System.out.println(e);
}
}
}
9.Write a JDBC program to count the number of records in table.(Without using
standard method)

import java.sql.*;

class Slip10_1
{
public static void main(String args[])
{
Connection con;
Statement stmt;

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection("jdbc:odbc:dsn");
if(con==null)
{
System.out.println("Connection
Failed.......");
System.exit(1);
}
System.out.println("Connection
Established......");
stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from
employee");

int cnt=0;
while(rs.next())
{
cnt++;
}
System.out.println("Number of records in Table
are:"+cnt);
}

catch(Exception e)
{
System.out.println(e);
}
}
}
15. Create a JSP page to accept a number from an user and display it in words:
Example: 123 � One Two Three. The output should be in red color�

HTML FILE :

<!DOCTYPE html>
<html>
<body>
<form method=get action="Slip19.jsp">
Enter Any Number : <input type=text name=num><br><br>
<input type=submit value="Display">
</form>
</body>
</html>

JSP FILE:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<font color=red>
<%! int i,n;
String s1;
%>
<% s1=request.getParameter("num");
n=s1.length();
i=0;
do
{
char ch=s1.charAt(i);
switch(ch)
{
case '0': out.println("Zero ");break;
case '1': out.println("One ");break;
case '2': out.println("Two ");break;
case '3': out.println("Three ");break;
case '4': out.println("Four ");break;
case '5': out.println("Five ");break;
case '6': out.println("Six ");break;
case '7': out.println("Seven ");break;
case '8': out.println("Eight ");break;
case '9': out.println("Nine ");break;
}
i++;
}while(i<n);
%>
</font>
</body>
</html>

16. Write a java program to create a student table with field�s rno, name and per.
Insert values in the table. Display all the details of the student on screen. (Use
PreparedStatement Interface)

import java.awt.event.*;
import javax.swing.*;
class Message implements Runnable
{
JTextField t;
public void run()
{
for(int i =1; i<=100;i++)
{
t.setText(""+i);
try
{
Thread.sleep(50);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
class Slip12_1 implements ActionListener
{
JFrame f;
JPanel p;
JTextField t;
JButton b;
Thread t1;

Slip12_1()
{
f = new JFrame();
p = new JPanel();

t = new JTextField(60);
b = new JButton("Start");

t1 = new Thread(this);

b.addActionListener(this);

p.add(t);
p.add(b);

f.add(p);
f.setSize(400, 400);
f.setVisible(true);
}

public void actionPerformed(ActionEvent e)


{
t1.start();
}
}

17. Write a JSP script to check whether given mail ID is valid or not. (Mail ID
should contain one @ symbol and atleast one Dot(.) symbol)
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<form action="Slip16.jsp" method="post">
Enter Your Name : <input type="text" name="name"><br>

<input type="submit" value="Submit">


</form>
</body>
</html>

SLIP.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>

<%

String name = request.getParameter("name");

Calendar rightnow = Calendar.getInstance();


int time = rightnow.get(Calendar.HOUR_OF_DAY);

if(time > 0 && time <= 12)


{
out.println("Good Morning"+name);
}
else if(time < 12 && time >=16)
{
out.println("Good Afternoon"+name);
}
else
{
out.println("Good Night"+name);
}
%>

18.Write a Multithreading program in java to display all the alphabets from A to Z


after 3 seconds.

class PrintDemo
{
public void printCount()
{
try
{
for(int i = 5; i > 0; i--)
{
System.out.println("Counter ---
" + i );
}
}
catch (Exception e)
{
System.out.println("Thread interrupted.");
}
}

class ThreadDemo extends Thread


{
private Thread t;
private String threadName;
PrintDemo PD;

ThreadDemo( String name, PrintDemo pd)


{
threadName = name;
PD = pd;
}
public void run()
{
synchronized(PD)
{
PD.printCount();
}
System.out.println("Thread " + threadName + " exiting.");
}

public void start ()


{
System.out.println("Starting " + threadName );
if (t == null)
{
t = new Thread (this, threadName);
t.start ();
}
}

public class Sl_1


{
public static void main(String a[])
{

PrintDemo PD = new PrintDemo();

ThreadDemo T1 = new ThreadDemo( "Thread - 1 ", PD );


ThreadDemo T2 = new ThreadDemo( "Thread - 2 ", PD );

T1.start();
T2.start();

// wait for threads to end


try
{
T1.join();
T2.join();
}
catch( Exception e)
{
System.out.println(e);
}
}
}
19. Write a java program which will display name and priority of current thread.
Change name of Thread to MyThread and set the priority to 2 and display it on
screen.
class S1
{
public static void main(String a[])
{
String S;
int p;

Thread t = Thread.currentThread();

S = t.getName();
System.out.println("\n Current Thread name : "+S);

p = t.getPriority();
System.out.println("\n Current thread priority : "+p);

t.setName("My Thread");
S = t.getName();
System.out.println("\nChanged Name : "+S);

t.setPriority(2);
p = t.getPriority();
System.out.println("\nChanged Priority : "+p);
}
}

20.Write a JDBC Program in java to display the names of Employees starting with �S�

character
import java.sql.*;
class Slip21_1
{
public static void main(String args[])
{ Connection con;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection("jdbc:odbc:dsn");
if(con==null)
{
System.out.println("Connection
Failed....");
System.exit(1);
}
System.out.println("Connection
Established...");

Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from
employee where name like 'S%'");

System.out.println("eno\t"+"ename\t"+"department\t"+"sal");
while(rs.next())
{
System.out.println("\n"+rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"
+rs.getInt(4));
}
}
catch(Exception e)
{
System.out.println(e);

}
}
}

Das könnte Ihnen auch gefallen