Wednesday, 24 January 2018

Advancedjava-Slip14

Slip14

 Write a Socket program in java which displays the server machine’s date and time on the client machine. 

import java.io.*;

import java.net.*;

 

class DateClient

{

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

{

Socket soc=new Socket(InetAddress.getLocalHost(),5217);      

BufferedReader in=new BufferedReader(new InputStreamReader(soc.getInputStream()

)

);

System.out.println(in.readLine());

}  

}

 

DateServer.java

  

import java.net.*;

import java.io.*;

import java.util.*;

 

class DateServer

{

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

{

ServerSocket s=new ServerSocket(5217);

while(true)

{

System.out.println("Waiting For Connection ...");

Socket soc=s.accept();

DataOutputStream out=new DataOutputStream(soc.getOutputStream());

out.writeBytes("Server Date" + (new Date()).toString() + "\n");

out.close();

soc.close();

}

 }

}

 

/*

C:\Users\Admin\Desktop\Javaprg>javac DateServer.java

 

C:\Users\Admin\Desktop\Javaprg>java DateServer

Waiting For Connection ...*/

 

/*

C:\Users\Admin\Desktop\Javaprg>java DateClient.java

Server DateSat May 14 02:09:13 PDT 2022


Q2. Write a JDBC program in java to display details of Book_Sales(SalesID, Date, Amount)  of selected month in JTable. Book_sales table is already created.  (Use JCombo component for Month selection)         


import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;

class MonthSales extends JFrame{
            private JTable tabSales;
            private DefaultTableModel dtm;
            private JLabel lblMonth;
            private JComboBox cmbMonth;
            private JPanel panNorth;

            private Connection con;
            private PreparedStatement ps;
            private ResultSet rs;

            public MonthSales(){
                        lblMonth = new JLabel("Month:");
                       
                        String months[] = {"---","JAN","FEB","MAR","APR","MAY",
                                                "JUN","JUL","AUG","SEP","OCT","NOV","DEC"};

                        cmbMonth = new JComboBox(months);

                        panNorth = new JPanel();
                        panNorth.add(lblMonth);
                        panNorth.add(cmbMonth);

                        dtm = new DefaultTableModel();
                        tabSales = new JTable(dtm);

                        dtm.addColumn("SalesID");
                        dtm.addColumn("SalesDate");
                        dtm.addColumn("Amount");

                        setTitle("Sales Report");
                        setSize(400,300);
                        setLocation(100,100);
                        add(panNorth,"North");
                        add(new JScrollPane(tabSales),"Center");
                        setVisible(true);
                        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                        try{
                                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con=DriverManager.getConnection("jdbc:odbc:lab.dsn");
                                   
                        }
                        catch(Exception e){
                                    JOptionPane.showMessageDialog(null,e);
                                    dispose();
                        }

                        cmbMonth.addItemListener(new ItemListener(){
                                    public void itemStateChanged(ItemEvent ie){
                                                try{
                                                            String month = cmbMonth.getSelectedItem().toString();

                                                            ps = con.prepareStatement("select * from Book_Sales where SalesDate like '%"+month+"%'");


                                                            rs = ps.executeQuery();

                                                            int n = dtm.getRowCount();
                                                            for(int i=0;i<n;i++)
                                                                        dtm.removeRow(0);

                                                            while(rs.next()){

                                                                        Vector row = new Vector();
                                                                        row.add(rs.getInt(1));
                                                                        row.add(rs.getString(2));
                                                                        row.add(rs.getFloat(3));
                                                                       
                                                                        dtm.addRow(row);
                                                            }
                                                }
                                                catch(Exception e){
                                                            JOptionPane.showMessageDialog(null,e);
                                                }
                                    }
                        });
            }

            public static void main(String args[]){
                        new MonthSales();
            }
}