[Link] a program in Java on Network Programming i.e. Client Server Architecture.
// [Link]
import [Link].*;
import [Link].*;
public class Server {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(12345);
[Link]("Server listening on port 12345...");
// Wait for a client to connect
Socket clientSocket = [Link]();
[Link]("Client connected.");
// Set up streams for communication
BufferedReader reader = new BufferedReader(new
InputStreamReader([Link]()));
BufferedWriter writer = new BufferedWriter(new
OutputStreamWriter([Link]()));
// Receive message from client
String clientMessage = [Link]();
[Link]("Client: " + clientMessage);
// Send a response back to the client
[Link]("Hello from the server!\n");
[Link]();
// Close resources
[Link]();
[Link]();
[Link]();
[Link]();
} catch (IOException e) {
[Link]();
// [Link]
import [Link].*;
import [Link].*;
public class Client {
public static void main(String[] args) {
try {
// Connect to the server
Socket socket = new Socket("localhost", 12345);
// Set up streams for communication
BufferedReader reader = new BufferedReader(new
InputStreamReader([Link]()));
BufferedWriter writer = new BufferedWriter(new
OutputStreamWriter([Link]()));
// Send a message to the server
[Link]("Hello from the client!\n");
[Link]();
// Receive response from the server
String serverMessage = [Link]();
[Link]("Server: " + serverMessage);
// Close resources
[Link]();
[Link]();
[Link]();
} catch (IOException e) {
[Link]();
}
[Link] a program in Java to develop an RMI application.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
// Define the remote interface
interface MyRemoteInterface extends Remote {
String sayHello() throws RemoteException;
// Implement the remote interface
class MyRemoteImplementation implements MyRemoteInterface {
@Override
public String sayHello() throws RemoteException {
return "Hello from the server!";
public class RMIServer {
public static void main(String[] args) {
try {
// Create an instance of the remote implementation
MyRemoteInterface remoteObject = new MyRemoteImplementation();
// Export the remote object
MyRemoteInterface stub = (MyRemoteInterface)
[Link](remoteObject, 0);
// Create and bind the registry
Registry registry = [Link](1099);
[Link]("MyRemoteObject", stub);
[Link]("Server ready");
} catch (Exception e) {
[Link]("Server exception: " + [Link]());
[Link]();
}
[Link] a program in Java to get information about particular table using result set Meta
Data.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class TableInfo {
public static void main(String[] args) {
// Replace with your database URL, username, and password
String url = "jdbc:mysql://your_database_url";
String username = "your_username";
String password = "your_password";
// Replace with the table name you want to get information about
String tableName = "your_table_name";
try (Connection connection = [Link](url, username, password)) {
String query = "SELECT * FROM " + tableName + " WHERE 1 = 0"; // Fetch no data, just
metadata
try (PreparedStatement preparedStatement = [Link](query);
ResultSet resultSet = [Link]()) {
ResultSetMetaData metaData = [Link]();
int columnCount = [Link]();
[Link]("Table: " + tableName);
[Link]("Columns:");
for (int i = 1; i <= columnCount; i++) {
[Link](" Column Name: " + [Link](i));
[Link](" Data Type: " + [Link](i));
[Link](" Nullable: " + ([Link](i) ==
[Link] ? "YES" : "NO"));
[Link]();
} catch (SQLException e) {
[Link]();
}
[Link] a program in Java to get information about database using Database Meta Data.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class DatabaseInfo {
public static void main(String[] args) {
String jdbcUrl = "jdbc:mysql://your_database_host:your_port/your_database";
String username = "your_username";
String password = "your_password";
try (Connection connection = [Link](jdbcUrl, username, password)) {
DatabaseMetaData metaData = [Link]();
// Display database information
[Link]("Database Product Name: " + [Link]());
[Link]("Database Product Version: " + [Link]());
// Retrieve and display tables
[Link]("\nTables:");
ResultSet tables = [Link](null, null, "%", null);
while ([Link]()) {
[Link]([Link]("TABLE_NAME"));
} catch (SQLException e) {
[Link]();
[Link] a program in Java to show connectivity with database using JDBC/ODBC driver.
import [Link];
import [Link];
import [Link];
public class DatabaseConnectionExample {
public static void main(String[] args) {
// Database URL, username, and password
String url = "jdbc:odbc:Your_ODBC_DSN";
String username = "your_username";
String password = "your_password";
// Attempt to establish a connection
try (Connection connection = [Link](url, username, password)) {
if (connection != null) {
[Link]("Connected to the database!");
// Perform database operations here if needed
} else {
[Link]("Failed to connect to the database.");
} catch (SQLException e) {
[Link]();
}
[Link] a program in Java, Servlets to get and display value from an HTML page.
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// Get the value from the HTML form
String inputValue = [Link]("inputName");
// Set the response content type
[Link]("text/html");
// Create PrintWriter object to send HTML response
PrintWriter out = [Link]();
// Display the received value
[Link]("<html><body>");
[Link]("<h2>Received value from HTML page:</h2>");
[Link]("<p>" + inputValue + "</p>");
[Link]("</body></html>");
}
html
<!DOCTYPE html>
<html>
<head>
<title>Form Page</title>
</head>
<body>
<form action="/your-web-app-context/HelloServlet" method="post">
<label for="inputName">Enter Value:</label>
<input type="text" id="inputName" name="inputName" required>
<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
2. Write a program in Java on Multithreading using runnable interface.
class MThread implements Runnable {
public void run() {
for (int i = 1; i <= 5; i++) {
[Link]([Link]().getId() + " Value " + i);
public class MultiThreadExample {
public static void main(String args[]) {
// Create two instances of the MThread class
MThread mThread1 = new MThread();
MThread mThread2 = new MThread();
// Create two threads and pass the instances of MThread to their constructors
Thread thread1 = new Thread(mThread1);
Thread thread2 = new Thread(mThread2);
// Start the threads
[Link]();
[Link]();
}
3. Write a program in Java to Create a New Data Source for MS Access.
import [Link];
import [Link];
import [Link];
public class CreateDataSource {
public static void main(String[] args) {
// JDBC connection parameters
String url = "jdbc:ucanaccess://C:/path/to/your/[Link]";
String user = ""; // For MS Access, you can usually leave this empty
String password = ""; // For MS Access, you can usually leave this empty
try (
// Establish a connection to the MS Access database
Connection connection = [Link](url, user, password);
// Create a statement for executing SQL queries
Statement statement = [Link]()
){
// SQL statement to create a new table (you can modify this part based on your needs)
String createTableSQL = "CREATE TABLE MyData (ID INT PRIMARY KEY, Value VARCHAR(255))";
[Link](createTableSQL);
[Link]("Table created successfully.");
} catch (Exception e) {
[Link]();
}
7. Write a program in Java to implement the concept of swings.
import [Link];
import [Link];
import [Link];
import [Link];
public class ExampleForSwing {
public static void main(String[] args) {
JFrame frame = new JFrame("Swing Example"); // Create a JFrame
// Set the default close operation
[Link](JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(); // Create a JPanel
JLabel label = new JLabel("Hello, Swing!"); // Create a JLabel
JButton button = new JButton("Click Me!"); // Create a JButton
// Add components to the panel
[Link](label);
[Link](button);
// Add the panel to the frame
[Link](panel);
// Set the size of the frame
[Link](300, 200);
// Set the frame to be visible
[Link](true);
}
10. Write a program in Java, JSP to get and display value from an HTML page.
1. Create an HTML file ([Link]):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Form</title>
</head>
<body>
<form action="[Link]" method="post">
Enter your name: <input type="text" name="userName">
<input type="submit" value="Submit">
</form>
</body>
</html>
2. Create a JSP file ([Link]):
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Display Name</title>
</head>
<body>
<h2>Hello, <%= [Link]("userName") %>!</h2>
</body>
</html>
ASSIGNMENT
th
B.C.A : - 5 SEMESTER
SESSION: - 2023 -2024
NAME : - ROHAN RAO
ROLL NO :- 2 414050010022
SUBJECT : - JAVA
SUBMITTED TO
COMPUTER S C I ENCE DEPARTMENT