Wednesday, 24 January 2018

Advancedjava-Slip6




Slip6

Q1. 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.*;
import javax.sql.*;

class slip6
{
    public static void main(String args[])
    {
        Connection con;
        Statement state;
        ResultSet rs;
        int ch;
      
            boolean flag=true;
            String decision;
            int no;
      
                       
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:lab.dsn");

            System.out.println("Statement object created");

            do
            {
                System.out.println("\n");
                System.out.println("Menu:");
                System.out.println("1.create Table");
                System.out.println("2.Insert Record into the Table");
               System.out.println("3.Display all the Records from the Table");
                System.out.println("4.Exit");
                System.out.println("Enter your choice: ");

                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                ch=Integer.parseInt(br.readLine());

                switch(ch)
                {
                         case 1:
                                                            state=con.createStatement();
                                                            String query="create table customer"+"(cno int ,"+" cname varchar(20),"+" phone int,"+" address varchar(20))";
                                                            state.executeUpdate(query);
                                                            System.out.println("Table created");


                    case 2:
                    System.out.println("Enter customer Number: ");
                     int cno=Integer.parseInt(br.readLine());
                   
                   System.out.println("Enter customer Name: ");
                    String cname=br.readLine();
                   
                                                            System.out.println("Enter customer phone: ");
                    int phone=Integer.parseInt(br.readLine());

                    System.out.println("Enter customer address: ");
                    String address=br.readLine();

                    String sql="insert into customer values(?,?,?,?)";
                    PreparedStatement p=con.prepareStatement(sql);
                    p.setInt(1,cno);
                    p.setString(2,cname);
                    p.setInt(3,phone);
                    p.setString(4,address);

                    p.executeUpdate();
                    System.out.println("Record Added");
                   
                    break;
               

                    case 3:
                    state=con.createStatement();
                    sql="select * from customer";
                    rs=state.executeQuery(sql);
                    while(rs.next())
                    {
                        System.out.println("\n");
                        System.out.print("\t" +rs.getInt(1));
                        System.out.print("\t" +rs.getString(2));
                        System.out.print("\t" +rs.getInt(3));
                        System.out.print("\t" +rs.getString(4));
                    }
                    break;

                    case 4:
                    System.exit(0);

                    default:
                    System.out.println("Invalid Choice");
                    break;
                }
            }while(ch!=4);
        }catch(Exception e)
        {
            System.out.println(e);
        }
    }
}




Q2. 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.





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

class Client{
public static void main(String args[])
throws Exception{
Socket s = new Socket("localhost",7080);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter file name:");
String fileName = br.readLine();
DataInputStream fromServer = new DataInputStream(s.getInputStream());
DataOutputStream toServer = new DataOutputStream(s.getOutputStream());
toServer.writeBytes(fileName+"\n");
String str="";
while(true)
{
str = fromServer.readLine();
if(str.equals("END")) break;
System.out.println(str);
}                                             
}
}

server.java

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

class Server{
public static void main(String args[])
throws Exception{
ServerSocket ss = new ServerSocket(7080);
while(true)
{
Socket s = ss.accept();
DataOutputStream toClient = new DataOutputStream(
s.getOutputStream());
DataInputStream fromClient = new DataInputStream(
s.getInputStream());
String fileName = fromClient.readLine();
File f = new File(fileName);
if(f.exists()){
String str=null;
DataInputStream dis = new DataInputStream(
new FileInputStream(f));
while((str=dis.readLine())!=null)
{
toClient.writeBytes(str+"\n");
}
}
else{
toClient.writeBytes("File "+fileName+" not found.\n");
}
toClient.writeBytes("END\n");
}
}
}