Savitribai Phule Pune University T.Y.B.B.A.(C.A.) Advanced Java Practical Slip 25 Answers


A) Write a Java program to accept a number through client terminal, send it to the

Server, Server calculates its factors and sends it to the client.

Answer :

//Client

import java.io.*;

import java.net.*;

 

class Slip25A {

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

 

        Socket s = new Socket("localhost", 2121);

 

        DataInputStream din = new DataInputStream(System.in);

        System.out.print("Enter the number : ");

        String n = din.readLine();

 

        DataOutputStream dos = new DataOutputStream(s.getOutputStream());

 

        dos.writeBytes(n + "\n");

        System.out.println("<<<<Factors>>>>");

        int arr[] = new int[15];

 

        DataInputStream dis = new DataInputStream(s.getInputStream());

 

        for (int i = 0; i < 15; i++) {

            arr[i] = dis.readInt();

            System.out.println(arr[i] + " ");

 

        }

 

    }

 

}

 

 

//Server

import java.io.*;

import java.net.*;

import java.util.Date;

 

class Slip25A1 {

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

 

        ServerSocket ss = new ServerSocket(2121);

        Socket s = ss.accept();

 

        DataInputStream dis = new DataInputStream(s.getInputStream());

 

        int n = Integer.parseInt(dis.readLine());

        int arr[] = new int[15];

        int i, j = 0;

 

        DataOutputStream dos = new DataOutputStream(s.getOutputStream());

 

        for (i = 1; i <= n; i++) {

            if (n % i == 0) {

                arr[j] = i;

                dos.writeInt(arr[j]);

                j++;

 

            } else {

                arr[j] = arr[j - 1];

            }

        }

 

        s.close();

 

    }

 

}


B) Write a Java Program for the following: Assume database is already created.

Answer :       

import java.sql.*;

public class DatabaseProgram {
    public static void main(String[] args) {
        try {
            // Step 1: Load the JDBC driver
            Class.forName("com.mysql.jdbc.Driver");
           
            // Step 2: Establish the connection to the database
            String url = "jdbc:mysql://localhost/mydatabase";
            String username = "root";
            String password = "password";
            Connection connection = DriverManager.getConnection(url, username, password);
           
            // Step 3: Perform some database operations
            // Example: Execute a SQL query to retrieve all records from a table
            String query = "SELECT * FROM mytable";
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(query);
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                double salary = resultSet.getDouble("salary");
                String department = resultSet.getString("department");
                System.out.println("Record: " + id + ", " + name + ", " + salary + ", " + department);
            }
           
            // Step 4: Close the database resources
            resultSet.close();
            statement.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            System.out.println("JDBC driver not found.");
        } catch (SQLException e) {
            System.out.println("SQL exception occurred: " + e.getMessage());
        }
    }
}

Output :


Savitribai Phule Pune University T.Y.B.B.A.(C.A.) Advanced Java Practical Slip 25 Answers Savitribai Phule Pune University T.Y.B.B.A.(C.A.) Advanced Java Practical Slip 25 Answers Reviewed by technical_saurabh on December 15, 2022 Rating: 5

No comments:

Powered by Blogger.