/****************************************************************************/NAME ::
ROLL NO. ::
Practical Name :: Write a java program to create Teacher
table(TNo.TName, Sal, Desg)
and
insert a record in it.
Practical no. ::01
Date ::
Remark ::
/****************************************************************************/
import
java.io.*;
import
java.sql.*;
public
class CreateTeacherTable
{
static Connection cn;
static Statement st;
public static void main(String args[])
{
try
{
int tno,sal;
String tname,desg;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn=DriverManager.getConnection("jdbc:odbc:dsn","","");
st=cn.createStatement();
String str="create table Teacher(TNo
number,TName varchar(20),Sal number,Desg varchar(20))";
st.executeUpdate(str);
System.out.println("Table
Created");
System.out.println("Enter Tno");
tno=Integer.parseInt(br.readLine());
System.out.println("Enter Tname");
tname=br.readLine();
System.out.println("Enter Sal");
sal=Integer.parseInt(br.readLine());
System.out.println("Enter Desg");
desg=br.readLine();
st.executeUpdate("insert into Teacher
values("+tno+",'"+tname+"',"+sal+",'"+desg+"')");
System.out.println("Record added
successfully");
cn.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
OutPut:-
Table Created
Enter Tno
12
Enter Tname
Kurde
Enter Sal
20000
Enter Desg
MCS
Record added
successfully
/****************************************************************************/NAME ::
ROLL NO. ::
Practical Name :: Write
a JDBC program to create a Mobile (Model_No,
Company_Name,
Price, Color) table and insert a record
in it.
Practical
no. ::02
Date ::
Remark ::
/****************************************************************************/
import java.io.*;
import java.sql.*;
public class
CreateMobileTable
{
static Connection cn;
static Statement st;
public static void main(String args[])
{
try
{
int mno,price;
String company,color;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn=DriverManager.getConnection("jdbc:odbc:dsn","","");
st=cn.createStatement();
String str="create table
Mobile(Model_No number,Company varchar(20),Price number,Color
varchar(20))";
st.executeUpdate(str);
System.out.println("Table
Created");
System.out.println("Enter Model
no");
mno=Integer.parseInt(br.readLine());
System.out.println("Enter
Company");
company=br.readLine();
System.out.println("Enter Price");
price=Integer.parseInt(br.readLine());
System.out.println("Enter Color");
color=br.readLine();
st.executeUpdate("insert into Mobile
values("+mno+",'"+company+"',"+price+",'"+color+"')");
System.out.println("Record added
successfully");
cn.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
OutPut:-
Table Created
Enter Model no
101
Enter Company
Samsung
Enter Price
5000
Enter Color
red
Record added
successfully
Press any key to
continue . . .
/****************************************************************************/NAME ::
ROLL NO. ::
Practical Name :: Write a java program to accept the
details of Employee ( no,
EName,Sal)
from the user and insert it into the Database.(use Awt).
Practical
no. ::03
Date ::
Remark ::
/****************************************************************************/
import java.awt.*;
import javax.swing.*;
import
java.awt.event.*;
import java.sql.*;
public class Employee
extends Frame implements ActionListener
{
Label l1,l2,l3;
TextField t1,t2,t3;
Button b;
Connection cn;
Statement st;
ResultSet rs;
public Employee()
{
setLayout(null);
l1=new Label("Eno");
l2=new Label("EName");
l3=new Label("Salary");
t1=new TextField();
t2=new TextField();
t3=new TextField();
b=new Button("Save");
l1.setBounds(50,50,100,30);
t1.setBounds(160,50,100,30);
l2.setBounds(50,90,100,30);
t2.setBounds(160,90,100,30);
l3.setBounds(50,130,100,30);
t3.setBounds(160,130,100,30);
b.setBounds(50,170,100,30);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b);
b.addActionListener(this);
setSize(500,500);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent oe)
{
String str=oe.getActionCommand();
if(str.equals("Save"))
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn=DriverManager.getConnection("jdbc:odbc:dsn","","");
st
=cn.createStatement();
int en=Integer.parseInt(t1.getText());
String
enn=t2.getText();
int
sal=Integer.parseInt(t3.getText());
String
strr="insert into emp values(" + en + " ,'" + enn +
"'," + sal + ")";
int
k=st.executeUpdate(strr);
if(k>0)
{
JOptionPane.showMessageDialog(null,"Record Is Added");
}
}
catch(Exception er)
{
System.out.println("Error");
}
}
}
public static void main(String args[])
{
new Employee().show();
}
}
Output
/****************************************************************************/NAME ::
ROLL NO. ::
Practical Name :: Write
a java program to display IP Address and Name of client
Practical
no. ::04
Date ::
Remark ::
/****************************************************************************/
import java.net.*;
import java.io.*;
public class
ip_localmachine
{
public static void main(String args[]) throws
Exception
{
InetAddress ipadd
=InetAddress.getLocalHost();
System.out.println("Host and
Address :"+ipadd);
System.out.println("Host name
:"+ipadd.getHostName());
String n=ipadd.toString();
System.out.println("IP address
:"+n.substring(n.indexOf("/")+1));
}
}
OutPut:-
Host and Address :vikasaut-57b9af/192.168.1.100
Host name :vikasaut-57b9af
IP address :192.168.1.100
Press any key to continue . . .
/****************************************************************************/NAME ::
ROLL NO. ::
Practical Name :: Write
a SOCKET program in java to check whether given file is
present
on server or not, If it is present then display its content on the
server’s
machine otherwise display error message.
Practical
no. ::05
Date ::
Remark ::
/****************************************************************************/
// Server.java
import java.io.*;
import java.net.*;
public class Server
{
public static void main(String args[])
throws Exception
{
ServerSocket ss = new ServerSocket(4444);
BufferedReader fromClient = null;
PrintStream toClient = null;
BufferedReader brFile = null;
File file = null;
while(true)
{
Socket s = ss.accept();
fromClient = new BufferedReader(
new InputStreamReader(
s.getInputStream()));
toClient = new PrintStream(
s.getOutputStream());
String fileName = fromClient.readLine();
file = new File(fileName);
if(file.exists())
{
brFile = new BufferedReader(
new FileReader(fileName));
String line = null;
while((line=brFile.readLine())!=null)
toClient.println(line); }
else
{
toClient.println("File "+fileName+" Not
Found.");
}
toClient.println("----");
s.close();
}
}
}
// Client.java
import java.io.*;
import java.net.*;
public class Client
{
public static void main(String args[])
throws Exception
{
Socket s = new Socket("localhost",4444);
BufferedReader fromServer = new BufferedReader(
new InputStreamReader(
s.getInputStream()));
PrintStream toServer = new PrintStream(
s.getOutputStream());
BufferedReader br = new BufferedReader(
new InputStreamReader(
System.in));
System.out.print("Enter file name:");
String fileName = br.readLine();
toServer.println(fileName);
String line = null;
while(true)
{
line = fromServer.readLine();
if(line.equals("----")) break;
System.out.println(line);
}
s.close();
}
}
OutPut:-
/****************************************************************************/NAME ::
ROLL NO. ::
Practical Name :: Write
a Socket program in java which displays the server machine’s date and time
on the client
machine.
Practical
no. ::06
Date ::
Remark ::
/****************************************************************************/
import java.net.*;
import java.io.*;
import java.util.*;
public class Server
{
public static void main(String args[])
throws Exception
{
ServerSocket ss = new ServerSocket(4444);
while(true)
{
Socket s = ss.accept();
PrintStream ps = new PrintStream(s.getOutputStream());
ps.println(new Date());
s.close();
}
}
}
import java.net.*;
import java.io.*;
import java.util.*;
public class Client
{
public static void main(String args[])
throws Exception
{
Socket s = new Socket("localhost",4444);
BufferedReader br = new BufferedReader( new InputStreamReader(
s.getInputStream()));
System.out.println("From Server:"+br.readLine());
}
}
/****************************************************************************/NAME ::
ROLL NO. ::
Practical Name :: Write
a SERVLET program which counts how many times a user has visited a web page. If
the user is revisiting the page, display the number of times visited.
Practical
no. ::07
Date ::
Remark ::
/****************************************************************************/
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SimpleCounterServlet extends HttpServlet
{
private int HitCounter;
public void init() throws ServletException
{
HitCounter = 0;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out = response.getWriter();
out.println("<h2>Welcome to
SimpleCounterServlet.java</h2>");
out.println("Hits on this servlet so far: "+ (++HitCounter));
}
public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{
doGet(request, response);
}
}
OutPut:-
/****************************************************************************/NAME ::
ROLL NO. ::
Practical Name :: Write
a JSP script to accept username, store it into the session, compare it with
password in another jsp file, if username matches with password then display
appropriate message in html file.
Practical
no. ::08
Date ::
Remark ::
/****************************************************************************/
Login.html
<%@page
contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Login Page</h1>
<center>
<h2>Signup Details</h2>
<form
action="LoginCheck.jsp" method="post">
<br/>Username:<input
type="text" name="username">
<br/>Password:<input
type="password" name="password">
<br/><input
type="submit" value="Submit">
</form>
</center>
</body>
</html>
Home.jsp
<%@page
contentType="text/html" pageEncoding="UTF-8"
errorPage="Error.jsp"%>
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<br/><br/><br/><br/><br/>
<center>
<h2>
<%
String
a=session.getAttribute("username").toString();
out.println("Hello "+a);
%>
</h2>
<br/>
<br/>
<br/><br/><br/><br/><br/>
<a
href="logout.jsp">Logout</a>
</center>
</body>
</html>
LoginCheck.jsp
<%@page
contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String
username=request.getParameter("username");
String
password=request.getParameter("password");
if((username.equals("anurag")
&& password.equals("jain")))
{
session.setAttribute("username",username);
response.sendRedirect("Home.jsp");
}
else
response.sendRedirect("Error.jsp");
%>
</body>
</html>
Logout.jsp
<%@page
contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
session.removeAttribute("username");
session.removeAttribute("password");
session.invalidate();
%>
<h1>Logout was done
successfully.</h1>
</body>
</html>
OutPut:-
/****************************************************************************/NAME ::
ROLL NO. ::
Practical Name :: Write
a JSP program which accept UserName in a TextBox and greets
the
user according to the time on server machine.
Practical
no. ::09
Date ::
Remark ::
/****************************************************************************/
// user.html
<form
method='post' action='user.jsp'>
User
Name:<input type='text' name='uname'><br>
<input
type='submit'><input type='reset'>
</form>
// user.jsp
<%@page
import="java.util.*"%>
<%
String name
= request.getParameter("uname");
Date d = new
Date();
if(d.getHours()<10)
{
%>
Good Morning
<%=name%>
<%
}
else
if(d.getHours()<16)
{
%>
Good
Afternoon <%=name%>
<%
}
else
{
%>
Good Evening
<%=name%>
<%
}
%>
OutPut:-
/****************************************************************************/NAME ::
ROLL NO. ::
Practical Name :: 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.
.
Practical
no. ::10
Date ::
Remark ::
/****************************************************************************/
//
number.html
<form
method='post' action='words.jsp'>
Enter
Number:<input type='text' name='no'><br>
<input
type='submit'><input type='reset'>
</form>
// words.jsp
<font
color='red'>
<%
String no =
request.getParameter("no");
String
words[]={"Zero","One","Two","Three","Four",
"Five","Six","Seven","Eight","Nine"};
for(int
i=0;i<no.length();i++)
{
out.print(words[no.charAt(i)-'0']+"
");
}
%>
</font>
OutPut:-
/****************************************************************************/NAME ::
ROLL NO. ::
Practical Name :: 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)).
Practical
no. ::11
Date ::
Remark ::
/****************************************************************************/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript form validation - checking
email</title>
<link rel='stylesheet' href='form-style.css'
type='text/css' />
</head>
<body
onload='document.form1.text1.focus()'>
<div class="mail">
<h2>Input an email and Submit</h2>
<form name="form1"
action="#">
<ul>
<li><input type='text'
name='text1'/></li>
<li> </li>
<li class="submit"><input
type="submit" name="submit" value="Submit"
onclick="ValidateEmail(document.form1.text1)"/></li>
<li> </li>
</ul>
</form>
</div>
<script
src="valid.jsp"></script>
</body>
</html>
//Valid.jsp
function ValidateEmail(inputText)
{
var mailformat =
/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if(inputText.value.match(mailformat))
{
document.form1.text1.focus();
return true;
}
else
{
alert("You have entered an invalid email
address!");
document.form1.text1.focus();
return false;
}
}
OutPut:-
/****************************************************************************/NAME ::
ROLL NO. ::
Practical Name :: Write a java program to simulate traffic
signal using multithreading.
Practical
no. ::12
Date ::
Remark ::
/****************************************************************************/
import java.awt.*;
import java.applet.*;
/*
<applet code= "tsignal.java"
height="600" width="600">
</applet>
*/
public class tsignal
extends java.applet.Applet implements Runnable {
int r,g1,y,i;
Thread t;
public void init() {
r=0;g1=0;y=0;
t=new Thread(this);
t.start();
}
public void run()
{
try
{ for (i=24;i>=1;i--)
{
t.sleep(100);
if (i>=16 && i<24)
{
g1=1;
repaint();
}
if (i>=8 && i<16)
{
y=1;
repaint();
}
if (i>=1 && i<8)
{
r=1;
repaint();
}
}
if (i==0)
{
run();
}
}
catch(Exception e)
{
}
}
public void paint(Graphics g) {
g.drawOval(100,100,100,100);
g.drawOval(100,225,100,100);
g.drawOval(100,350,100,100);
g.drawString("start",200,200);
if (r==1)
{
g.setColor(Color.red);
g.fillOval(100,100,100,100);
g.drawOval(100,100,100,100);
g.drawString("stop",200,200);
r=0;
}
if (g1==1)
{
g.setColor(Color.green);
g.fillOval(100,225,100,100);
g1=0;
g.drawOval(100,225,100,100);
g.drawString("go",200,200);
}
if (y==1)
{
g.setColor(Color.yellow);
g.fillOval(100,350,100,100);
y=0;
g.drawOval(100,350,100,100);
g.drawString("slow",200,200);
}
}
}
OutPut:-
/****************************************************************************/NAME ::
ROLL NO. ::
Practical Name :: Write
a Multithreading program in java using Runnable interface to move text on
the
frame
as follow:
![]()
![]()
![]()
![]()
Starting Position of Text [25]
Practical
no. ::13
Date ::
Remark ::
/****************************************************************************/
import
java.awt.*;
import
java.awt.event.*;
class
MoveText extends Frame implements Runnable
{
Label l1;
Thread t;
int x,y,side;
public MoveText()
{
setLayout(null);
l1=new Label(" Hello Java");
l1.setFont(new
Font("",Font.BOLD,14));
l1.setForeground(Color.red);
setSize(400,400);
setVisible(true);
t=new Thread(this);
t.start();
x=5;
y=200;side=1;
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
public void run()
{
try
{
if(side==1)
{
t.sleep(50);
l1.setBounds(x+=5,y-=5,70,15);
add(l1);
if(y==20)
side=2;
}
if(side==2)
{
t.sleep(50);
l1.setBounds(x+=5,y+=5,70,15);
add(l1);
if(y==200)
side=3;
}
if(side==3)
{
t.sleep(50);
l1.setBounds(x-=5,y+=5,70,15);
add(l1);
if(y==390)
side=4;
}
if(side==4)
{
t.sleep(50);
l1.setBounds(x-=5,y-=5,70,15);
add(l1);
if(x==0)
{
side=1;
x=0;y=200;
}
}
}catch(Exception e)
{
System.out.println(e);
}
run();
}
public static void main(String args[])
{
new MoveText();
}
}
OutPut:-
/****************************************************************************/NAME ::
ROLL NO. ::
Practical Name :: Write
a Multithreading program in java for bouncing ball. For
each
bounce, Change the color of ball randomly.
Practical
no. ::14
Date ::
Remark ::
/****************************************************************************/
import
java.awt.*;
/*<applet
code="BounsingBall.class" height=400 width=350>
</applet>*/
public
class BounsingBall extends java.applet.Applet implements Runnable
{
Thread t;
int f,y,f1,f2,f3;
public void init()
{
t=new Thread(this);
t.start();
f=0;y=0;
f1=0;
}
public void run()
{
try{
if (f==0)
{
t.sleep(10);
y=y+5;
repaint();
if(f1==6)
f1=0;
}
if(f==1)
{
t.sleep(10);
y=y-5;
repaint();
if(f1==6)
f1=0;
}
}catch(Exception e){
}
run();
}
public void paint(Graphics g)
{
if(f==0)
{
if(f1==1)
g.setColor(Color.green);
if(f1==2)
g.setColor(Color.blue);
if(f1==3)
g.setColor(Color.red);
if(f1==4)
g.setColor(Color.yellow);
if(f1==5)
g.setColor(Color.orange);
g.fillOval(150,y+10,20,20);
if(y==400)
{
f1++;
f=1;
}
}
if(f==1)
{
if(f1==1)
g.setColor(Color.green);
if(f1==2)
g.setColor(Color.blue);
if(f1==3)
g.setColor(Color.red);
if(f1==4)
g.setColor(Color.yellow);
if(f1==5)
g.setColor(Color.orange);
g.fillOval(150,y-10,20,20);
if(y==0)
{
f1++;
f=0;
}
}
}
}
OutPut:-
/****************************************************************************/NAME ::
ROLL NO. ::
Practical Name :: Write
a java program to display “Hello Java” message n times on the screen.
Practical no.
::15
Date ::
Remark ::
/****************************************************************************/
import java.io.*;
import java.util.*;
public class slip4 extends Thread
{
slip4()
{
start();
}
public void
run()
{
try
{
int i;
for(i=0;i<=25;i++)
{
Thread.sleep(1000);
System.out.print("\nHello Java"+i);
}
}
catch(Exception e)
{
System.out.print(e);
}
}
public static
void main(String args[])
{
slip4 th=new
slip4();
slip4
th1=new slip4();
}
}
OutPut:-
/****************************************************************************/NAME ::
ROLL NO. ::
Practical Name :: Write
a Multithreading program in java for Racing Car. (Use AWT)
.
Practical
no. ::16
Date ::
Remark ::
/****************************************************************************/
import java.awt.*;
import java.applet.*;
/*
<applet
code= "CAR.java" height="600" width="600">
</applet>
*/
public class CAR extends java.applet.Applet implements
Runnable {
int
aflag,x;
Thread t;
public void
init() {
t=new
Thread(this); aflag=0;x=0;
t.start();
}
public void
run()
{
try
{
x+=5;
t.sleep(200);
repaint();
run();
}
catch(Exception e)
{
}
}
public void
paint(Graphics g) {
g.drawLine(0,150,600,150);
if
(aflag==0)
{
g.fillRect(x,100,20,20);
g.fillRect(x+10,125,20,20);
g.drawRect(x,100,20,20);
g.drawRect(x+10,125,20,20);
aflag=1;
}
else
{
g.fillRect(x,100,20,20);
g.fillRect(x,125,20,20);
g.drawRect(x,100,20,20);
g.drawRect(x,125,20,20);
aflag=0;
}
}
}
OutPut:-
/****************************************************************************/NAME ::
ROLL NO. ::
Practical Name :: Write
a Multithreading program in java to display the number’s
between
1 to 100 continuously in a TextField by
clicking on button.
Practical
no. ::17
Date ::
Remark ::
/****************************************************************************/
import java.io.*;
import javax.swing.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
class slip20 extends JFrame implements ActionListener
{
Container cc;
JButton s,b;
JTextField t1,t2;
slip20()
{
super("Thread Program");
cc=getContentPane();
cc.setLayout(null);
t1=new JTextField("");
t2=new JTextField("");
s=new JButton("Start");
b=new JButton("Exit");
cc.add(t1);
cc.add(t2);
cc.add(s);
cc.add(b);
t1.setBounds(50,50,924,30);
t2.setBounds(50,100,924,30);
s.setBounds(200,200,80,30);
b.setBounds(330,200,80,30);
s.addActionListener(this);
b.addActionListener(this);
setSize(1024,768);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equalsIgnoreCase("Start"))
{
FirstThread f=new FirstThread();
SecondThread s=new SecondThread();
}
if(e.getActionCommand().equalsIgnoreCase("Exit"))
{
System.exit(0);
}
}
class FirstThread extends Thread
{
FirstThread()
{
super("FirstThread");
start();
}
public void run()
{
int i;
for(i=1;i<=100;i++)
{
t1.setText(t1.getText()+""+i);
try
{
Thread.sleep(500);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
class SecondThread extends Thread
{
SecondThread()
{
super("SecondThread");
start();
}
public void run()
{
int i;
for(i=1;i<=100;i++)
{
t2.setText(t2.getText()+""+i);
try
{
Thread.sleep(500);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
public static void main(String args[])
{
slip20 s1=new slip20();}}
OutPut:-
/****************************************************************************/NAME ::
ROLL NO. ::
Practical Name :: Write
a Multithreading program in java to convert smile face into the
crying
face after 5 seconds and vice versa(Use Applet).
Practical
no. ::18
Date ::
Remark ::
/****************************************************************************/
import java.awt.*;
import java.applet.*;
/*
<applet
code= "smileface.java" height="600"
width="600">
</applet>
*/
public class smileface extends java.applet.Applet
implements Runnable {
int aflag;
Thread t;
public void
init() {
t=new
Thread(this); aflag=0;
t.start();
}
public void
run()
{
try
{
if
(aflag==0)
{
t.sleep(1000);
aflag=1;
} else
{
t.sleep(1000);
aflag=0;
}
repaint();
run();
}
catch(Exception e)
{
}
}
public void
paint(Graphics g) {
g.drawOval(100,100,100,100);
g.fillOval(120,125,20,20);
g.fillOval(160,125,20,20);
g.drawLine(150,135,150,165);
if
(aflag==0)
{
g.drawArc(140,160,20,20,0,-180);
aflag=1;
}
else
{
g.drawArc(140,160,20,20,0,180);
aflag=0;
}
}
}
OutPut:-
No comments:
Post a Comment