Program – 4
Client
import java.io.*;
import java.net.*;
class cli
{
public static void main(String ar[])
{
try
{
Socket s=new Socket("localhost",7777);
DataInputStream in=new DataInputStream(System.in);
PrintStream p=new PrintStream(s.getOutputStream());
DataInputStream din=new DataInputStream(s.getInputStream());
String str,str1;
System.out.println("Enter 6 digit Account Number");
str=in.readLine();
p.println(str);
System.out.println("====Server Reply=====");
str1=din.readLine();
System.out.println(str1);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Server-1
import java.io.*;
import java.net.*;
importjava.sql.*;
class ser1
{
public static void main(String ar[])
{
try
{
ServerSocketss=new ServerSocket(7777);
Socket s=ss.accept();
DataInputStream din=new DataInputStream(s.getInputStream());
PrintStream p=new PrintStream(s.getOutputStream());
String str,str1="",str2;
str=din.readLine();
System.out.println(str);
int flag=0;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:db1");
Statement smt=con.createStatement();
ResultSetrs=smt.executeQuery("select * from lab71 where AccuntNo="+str+"");
while(rs.next())
{
str1=rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3);
flag=1;
}
if(flag==1)
{
System.out.println("Record Found");
p.println(str1);
}
else
{
Socket s1=new Socket("localhost",7771);
PrintStream p1=new PrintStream(s1.getOutputStream());
DataInputStream din1=new DataInputStream(s1.getInputStream());
System.out.println("Record is Not Available in server1.So Account Number sending to server2");
p1.println(str);
str2=din1.readLine();
System.out.println(str2);
p.println(str2);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Server-2
import java.io.*;
import java.net.*;
importjava.sql.*;
class ser2
{
public static void main(String ar[])
{
try
{
ServerSocketss=new ServerSocket(7771);
Socket s=ss.accept();
DataInputStream din=new DataInputStream(s.getInputStream());
PrintStream p=new PrintStream(s.getOutputStream());
String str,str1="",str2="";
str=din.readLine();
System.out.println(str);
int flag=0;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:db2");
Statement smt=con.createStatement();
ResultSetrs=smt.executeQuery("select * from lab72 where AccuntNo="+str+"");
while(rs.next())
{
str1=rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3);
flag=1;
}
if(flag==1)
{
System.out.println("Record Found In server2");
p.println(str1);
}
else
{
System.out.println("Record Also not Found in Server2");
p.println("Record Also not Found in Server2");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
No comments:
Post a Comment