0% found this document useful (0 votes)
6 views33 pages

Java Database and Networking Programs

The document contains multiple Java programming practicals covering various topics such as creating and managing databases, socket programming, servlets, JSP, and multithreading. Each practical includes code snippets, descriptions of functionality, and expected outputs. The practicals are designed for students to learn and implement different aspects of Java programming.

Uploaded by

zinat shaikh
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)
6 views33 pages

Java Database and Networking Programs

The document contains multiple Java programming practicals covering various topics such as creating and managing databases, socket programming, servlets, JSP, and multithreading. Each practical includes code snippets, descriptions of functionality, and expected outputs. The practicals are designed for students to learn and implement different aspects of Java programming.

Uploaded by

zinat shaikh
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

/****************************************************************************/

NAME :: Shinde Pradnya Sanjay ROLL NO. ::


Practical Name :: Write a java program to create Teacher table([Link], Sal, Desg)
and insert a record in it.
Practical no. ::01
Date :: Remark ::
/****************************************************************************/
import [Link].*;
import [Link].*;
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([Link]));
[Link]("[Link]");
cn=[Link]("jdbc:odbc:dsn","","");
st=[Link]();
String str="create table Teacher(TNo number,TName varchar(20),Sal number,Desg
varchar(20))";
[Link](str);
[Link]("Table Created");
[Link]("Enter Tno");
tno=[Link]([Link]());
[Link]("Enter Tname");
tname=[Link]();
[Link]("Enter Sal");
sal=[Link]([Link]());
[Link]("Enter Desg");
desg=[Link]();
[Link]("insert into Teacher values("+tno+",'"+tname+"',"+sal+",'"+desg+"')");
[Link]("Record added successfully");
[Link]();
}catch(Exception e)
{
[Link](e);
}
}
}
OutPut:-

Table Created
Enter Tno
12
Enter Tname
Kurde
Enter Sal
20000
Enter Desg
MCS
Record added successfully
/****************************************************************************/
NAME :: Shinde Pradnya Sanjay 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. ::02
Date :: Remark ::
/****************************************************************************/
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
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");
[Link](50,50,100,30);
[Link](160,50,100,30);
[Link](50,90,100,30);
[Link](160,90,100,30);
[Link](50,130,100,30);
[Link](160,130,100,30);
[Link](50,170,100,30);
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b);
[Link](this);
setSize(500,500);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
[Link](0);
}
});
}
public void actionPerformed(ActionEvent oe)
{
String str=[Link]();
if([Link]("Save"))
{
try
{
[Link]("[Link]");
cn=[Link]("jdbc:odbc:dsn","","");
st =[Link]();
int en=[Link]([Link]());
String enn=[Link]();
int sal=[Link]([Link]());
String strr="insert into emp values(" + en + " ,'" + enn + "'," + sal + ")";
int k=[Link](strr);
if(k>0)
{

[Link](null,"Record Is Added");
}
}
catch(Exception er)
{
[Link]("Error");
}
}
}
public static void main(String args[])
{
new Employee().show();
}

Output
]
/****************************************************************************/
NAME :: Bhojane Kadambari Nandkumar ROLL NO. ::
Practical Name :: Write a java program to display IP Address and Name of client
Practical no. ::03
Date :: Remark ::
/****************************************************************************/

import [Link].*;
import [Link].*;
public class ip_localmachine
{
public static void main(String args[]) throws Exception
{
InetAddress ipadd =[Link]();
[Link]("Host and Address :"+ipadd);
[Link]("Host name :"+[Link]());

String n=[Link]();
[Link]("IP address :"+[Link]([Link]("/")+1));
}
}

OutPut:-
Host and Address :vikasaut-57b9af/[Link]
Host name :vikasaut-57b9af
IP address :[Link]
Press any key to continue . . .
/****************************************************************************/
NAME :: Bhojane Kadambari Nandkumar 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. ::04
Date :: Remark ::
/****************************************************************************/
// [Link]
import [Link].*;
import [Link].*;
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 = [Link]();
fromClient = new BufferedReader(
new InputStreamReader(
[Link]()));
toClient = new PrintStream(
[Link]());
String fileName = [Link]();
file = new File(fileName);
if([Link]())
{
brFile = new BufferedReader(
new FileReader(fileName));
String line = null;
while((line=[Link]())!=null)
[Link](line); }
else
{
[Link]("File "+fileName+" Not Found.");
}
[Link]("----");
[Link]();
}
}
}
// [Link]
import [Link].*;
import [Link].*;
public class Client
{
public static void main(String args[])
throws Exception
{
Socket s = new Socket("localhost",4444);
BufferedReader fromServer = new BufferedReader(
new InputStreamReader(
[Link]()));
PrintStream toServer = new PrintStream(
[Link]());
BufferedReader br = new BufferedReader(
new InputStreamReader(
[Link]));
[Link]("Enter file name:");
String fileName = [Link]();
[Link](fileName);
String line = null;
while(true)
{
line = [Link]();
if([Link]("----")) break;
[Link](line);
}
[Link]();
}
}

OutPut:-
/****************************************************************************/
NAME :: Bhojane Kadambari Nandkumar 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. ::05
Date :: Remark ::
/****************************************************************************/

import [Link].*;
import [Link].*;
import [Link].*;
public class Server
{
public static void main(String args[])
throws Exception
{
ServerSocket ss = new ServerSocket(4444);
while(true)
{
Socket s = [Link]();
PrintStream ps = new PrintStream([Link]());
[Link](new Date());
[Link]();
}
}
}
import [Link].*;
import [Link].*;
import [Link].*;
public class Client
{
public static void main(String args[])
throws Exception
{
Socket s = new Socket("localhost",4444);
BufferedReader br = new BufferedReader( new
InputStreamReader( [Link]()));
[Link]("From Server:"+[Link]());
}
}
/****************************************************************************/
NAME :: Bhojane Kadambari Nandkumar 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. ::06


Date :: Remark ::
/****************************************************************************/

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

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 = [Link]();
[Link]("<h2>Welcome to [Link]</h2>");
[Link]("Hits on this servlet so far: "+ (++HitCounter));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
}

OutPut:-
/****************************************************************************/
NAME :: Bhojane Kadambari Nandkumar 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. ::07
Date :: Remark ::
/****************************************************************************/
[Link]
<%@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="[Link]" 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>
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8" errorPage="[Link]"%>
<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=[Link]("username").toString();
[Link]("Hello "+a);
%>
</h2>
<br/>
<br/>
<br/><br/><br/><br/><br/>
<a href="[Link]">Logout</a>
</center>

</body>
</html>
[Link]
<%@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=[Link]("username");
String password=[Link]("password");
if(([Link]("anurag") && [Link]("jain")))
{
[Link]("username",username);
[Link]("[Link]");
}
else
[Link]("[Link]");
%>
</body>
</html>
[Link]
<%@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>
<%
[Link]("username");
[Link]("password");
[Link]();
%>
<h1>Logout was done successfully.</h1>
</body>
</html>

OutPut:-
/****************************************************************************/
NAME :: Bhojane Kadambari Nandkumar 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. ::08
Date :: Remark ::
/****************************************************************************/
// [Link]
<form method='post' action='[Link]'>
Enter Number:<input type='text' name='no'><br>
<input type='submit'><input type='reset'>
</form>
// [Link]
<font color='red'>
<%
String no = [Link]("no");
String words[]={"Zero","One","Two","Three","Four",
"Five","Six","Seven","Eight","Nine"};
for(int i=0;i<[Link]();i++)
{
[Link](words[[Link](i)-'0']+" ");
}
%>
</font>
OutPut:-
/****************************************************************************/
NAME :: Bhojane Kadambari Nandkumar ROLL NO. :: 32
Practical Name :: Write a java program to simulate traffic signal using multithreading.
Practical no. ::09
Date :: Remark ::
/****************************************************************************/

import [Link].*;
import [Link].*;
/*
<applet code= "[Link]" height="600" width="600">
</applet>
*/

public class tsignal extends [Link] implements Runnable {

int r,g1,y,i;
Thread t;
public void init() {
r=0;g1=0;y=0;

t=new Thread(this);
[Link]();

}
public void run()
{
try
{ for (i=24;i>=1;i--)
{
[Link](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) {


[Link](100,100,100,100);
[Link](100,225,100,100);
[Link](100,350,100,100);
[Link]("start",200,200);
if (r==1)
{ [Link]([Link]);
[Link](100,100,100,100);
[Link](100,100,100,100);

[Link]("stop",200,200);
r=0;
}
if (g1==1)
{ [Link]([Link]);
[Link](100,225,100,100);
g1=0;

[Link](100,225,100,100);

[Link]("go",200,200);
}
if (y==1)
{ [Link]([Link]);
[Link](100,350,100,100);
y=0;

[Link](100,350,100,100);
[Link]("slow",200,200);
}

}
}

OutPut:-
/****************************************************************************/
NAME :: Bhojane Kadambari Nandkumar 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. ::10


Date :: Remark ::
/****************************************************************************/
import [Link].*;
import [Link].*;
class MoveText extends Frame implements Runnable
{
Label l1;
Thread t;
int x,y,side;
public MoveText()
{
setLayout(null);
l1=new Label(" Hello Java");
[Link](new Font("",[Link],14));
[Link]([Link]);
setSize(400,400);
setVisible(true);
t=new Thread(this);
[Link]();
x=5;
y=200;side=1;
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
[Link](0);
}
});
}
public void run()
{
try
{
if(side==1)
{
[Link](50);
[Link](x+=5,y-=5,70,15);
add(l1);
if(y==20)
side=2;
}
if(side==2)
{
[Link](50);
[Link](x+=5,y+=5,70,15);
add(l1);
if(y==200)
side=3;
}
if(side==3)
{
[Link](50);
[Link](x-=5,y+=5,70,15);
add(l1);
if(y==390)
side=4;
}
if(side==4)
{
[Link](50);
[Link](x-=5,y-=5,70,15);
add(l1);
if(x==0)
{
side=1;
x=0;y=200;
}
}
}catch(Exception e)
{
[Link](e);
}
run();
}
public static void main(String args[])
{
new MoveText();
}
}
OutPut:-
/****************************************************************************/
NAME :: Pingale Sagar Chourangnath ROLL NO. :: 32
Practical Name :: Write a Multithreading program in java for bouncing ball. For
each bounce, Change the color of ball randomly.
Practical no. ::11
Date :: Remark ::
/****************************************************************************/
import [Link].*;
/*<applet code="[Link]" height=400 width=350>
</applet>*/
public class BounsingBall extends [Link] implements Runnable
{
Thread t;
int f,y,f1,f2,f3;
public void init()
{
t=new Thread(this);
[Link]();
f=0;y=0;
f1=0;

public void run()


{
try{

if (f==0)
{
[Link](10);
y=y+5;

repaint();

if(f1==6)
f1=0;

}
if(f==1)
{
[Link](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)
[Link]([Link]);
if(f1==2)
[Link]([Link]);
if(f1==3)
[Link]([Link]);
if(f1==4)
[Link]([Link]);
if(f1==5)
[Link]([Link]);
[Link](150,y+10,20,20);

if(y==400)
{
f1++;

f=1;
}

}
if(f==1)
{
if(f1==1)
[Link]([Link]);
if(f1==2)
[Link]([Link]);
if(f1==3)
[Link]([Link]);
if(f1==4)
[Link]([Link]);
if(f1==5)
[Link]([Link]);
[Link](150,y-10,20,20);

if(y==0)
{
f1++;
f=0;
}

}
}

OutPut:-
/****************************************************************************/
NAME :: Pingale Sagar Chourangnath ROLL NO. :: 32
Practical Name :: Write a java program to display “Hello Java” message n times on the screen.
Practical no. ::12
Date :: Remark ::
/****************************************************************************/
import [Link].*;
import [Link].*;
public class slip4 extends Thread
{
slip4()
{
start();
}
public void run()
{
try
{
int i;
for(i=0;i<=25;i++)
{
[Link](1000);
[Link]("\nHello Java"+i);
}
}
catch(Exception e)
{
[Link](e);
}
}
public static void main(String args[])
{
slip4 th=new slip4();
slip4 th1=new slip4();
}
}
OutPut:-
/****************************************************************************/
NAME :: Pingale Sagar Chourangnath ROLL NO. :: 32
Practical Name :: Write a Multithreading program in java for Racing Car.(Use AWT)
Practical no. ::13
Date :: Remark ::
/****************************************************************************/

import [Link].*;
import [Link].*;
/*
<applet code= "[Link]" height="600" width="600">
</applet>
*/

public class CAR extends [Link] implements Runnable {

int aflag,x;
Thread t;
public void init() {

t=new Thread(this); aflag=0;x=0;


[Link]();

}
public void run()
{
try
{
x+=5;
[Link](200);

repaint();
run();
}
catch(Exception e)
{
}

public void paint(Graphics g) {

[Link](0,150,600,150);
if (aflag==0)
{ [Link](x,100,20,20);
[Link](x+10,125,20,20);
[Link](x,100,20,20);
[Link](x+10,125,20,20);
aflag=1;
}
else
{
[Link](x,100,20,20);
[Link](x,125,20,20);
[Link](x,100,20,20);
[Link](x,125,20,20);
aflag=0;
}

}
}

OutPut:-
/****************************************************************************/
NAME :: Pingale Sagar Chourangnath ROLL NO. :: 32
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. ::14
Date :: Remark ::
/****************************************************************************/
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
class slip20 extends JFrame implements ActionListener
{
Container cc;
JButton s,b;
JTextField t1,t2;
slip20()
{
super("Thread Program");
cc=getContentPane();
[Link](null);
t1=new JTextField("");
t2=new JTextField("");
s=new JButton("Start");
b=new JButton("Exit");
[Link](t1);
[Link](t2);
[Link](s);
[Link](b);
[Link](50,50,924,30);
[Link](50,100,924,30);
[Link](200,200,80,30);
[Link](330,200,80,30);
[Link](this);
[Link](this);
setSize(1024,768);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if([Link]().equalsIgnoreCase("Start"))
{
FirstThread f=new FirstThread();
SecondThread s=new SecondThread();
}
if([Link]().equalsIgnoreCase("Exit"))
{
[Link](0);
}
}
class FirstThread extends Thread
{
FirstThread()
{
super("FirstThread");
start();
}
public void run()
{
int i;
for(i=1;i<=100;i++)
{
[Link]([Link]()+""+i);
try
{
[Link](500);
}
catch(Exception e)
{
[Link](e);
}
}
}
}
class SecondThread extends Thread
{
SecondThread()
{
super("SecondThread");
start();
}
public void run()
{
int i;
for(i=1;i<=100;i++)
{
[Link]([Link]()+""+i);
try
{
[Link](500);
}
catch(Exception e)
{
[Link](e);
}
}
}
}
public static void main(String args[])
{
slip20 s1=new slip20();}}
OutPut:-
/****************************************************************************/
NAME :: Pingale Sagar Chourangnath ROLL NO. :: 32
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. ::15
Date :: Remark ::
/****************************************************************************/
import [Link].*;
import [Link].*;
/*
<applet code= "[Link]" height="600" width="600">
</applet>
*/

public class smileface extends [Link] implements Runnable {

int aflag;
Thread t;
public void init() {

t=new Thread(this); aflag=0;


[Link]();

}
public void run()
{
try
{
if (aflag==0)
{
[Link](1000);
aflag=1;

} else
{
[Link](1000);
aflag=0;

}
repaint();
run();
}
catch(Exception e)
{
}

public void paint(Graphics g) {


[Link](100,100,100,100);
[Link](120,125,20,20);
[Link](160,125,20,20);
[Link](150,135,150,165);
if (aflag==0)
{ [Link](140,160,20,20,0,-180);
aflag=1;
}
else
{
[Link](140,160,20,20,0,180);
aflag=0;
}

}
}

OutPut:-

You might also like