Q.1.
Advanced Java:
A)
Write a java program to display IPAddress and name of client machine.
Answer :
import java.net.InetAddress;
public class ClientMachineInfo {
public static void main(String[] args) {
try {
InetAddress clientAddr = InetAddress.getLocalHost();
System.out.println("IP address of client machine: " + clientAddr.getHostAddress());
System.out.println("Name of client machine: " + clientAddr.getHostName());
} catch (Exception e) {
System.out.println("Error while getting client machine info: " + e.getMessage());
}
}
}
Output :
B)
Write a Java program to display sales details of Product (PID, PName, Qty,
Rate,
Amount)
between two selected dates. (Assume Sales table is already created).
Answer :
import java.sql.*;
import java.util.Scanner;
public class ProductSalesDetails {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Prompt user to enter start and end dates
System.out.print("Enter start date (YYYY-MM-DD): ");
String startDate = sc.next();
System.out.print("Enter end date (YYYY-MM-DD): ");
String endDate = sc.next();
// Define database connection variables
String url = "jdbc:mysql://localhost:3306/mydatabase";
String user = "root";
String password = "password";
// Define SQL query to fetch sales details of product between two dates
String query = "SELECT PID, PName, Qty, Rate, Amount FROM Sales WHERE SaleDate BETWEEN ? AND ?";
try {
// Establish database connection
Connection conn = DriverManager.getConnection(url, user, password);
// Prepare SQL statement with parameters for start and end dates
PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1, startDate);
pstmt.setString(2, endDate);
// Execute the SQL statement and get the result set
ResultSet rs = pstmt.executeQuery();
// Display the sales details in tabular form
System.out.println("PID\tPName\tQty\tRate\tAmount");
System.out.println("-------------------------------------------------");
while (rs.next()) {
int pid = rs.getInt("PID");
String pname = rs.getString("PName");
int qty = rs.getInt("Qty");
double rate = rs.getDouble("Rate");
double amount = rs.getDouble("Amount");
System.out.println(pid + "\t" + pname + "\t" + qty + "\t" + rate + "\t" + amount);
}
// Close the database connection
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Output :
Savitribai Phule Pune University T.Y.B.B.A.(C.A.) Advanced Java Practical Slip 11 Answers
Reviewed by technical_saurabh
on
September 21, 2022
Rating:
No comments:
Post a Comment