You are on page 1of 16

1.Write a program in Java on Network Programming i.e. Client Server Architecture.

// Server.java

import java.io.*;

import java.net.*;

public class Server {

public static void main(String[] args) {

try {

ServerSocket serverSocket = new ServerSocket(12345);

System.out.println("Server listening on port 12345...");

// Wait for a client to connect

Socket clientSocket = serverSocket.accept();

System.out.println("Client connected.");

// Set up streams for communication

BufferedReader reader = new BufferedReader(new


InputStreamReader(clientSocket.getInputStream()));

BufferedWriter writer = new BufferedWriter(new


OutputStreamWriter(clientSocket.getOutputStream()));

// Receive message from client

String clientMessage = reader.readLine();

System.out.println("Client: " + clientMessage);

// Send a response back to the client

writer.write("Hello from the server!\n");

writer.flush();

// Close resources

reader.close();
writer.close();

clientSocket.close();

serverSocket.close();

} catch (IOException e) {

e.printStackTrace();

// Client.java

import java.io.*;

import java.net.*;

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(socket.getInputStream()));

BufferedWriter writer = new BufferedWriter(new


OutputStreamWriter(socket.getOutputStream()));

// Send a message to the server

writer.write("Hello from the client!\n");

writer.flush();

// Receive response from the server

String serverMessage = reader.readLine();

System.out.println("Server: " + serverMessage);


// Close resources

reader.close();

writer.close();

socket.close();

} catch (IOException e) {

e.printStackTrace();

}
8.Write a program in Java to develop an RMI application.

import java.rmi.Remote;

import java.rmi.RemoteException;

import java.rmi.registry.LocateRegistry;

import java.rmi.registry.Registry;

import java.rmi.server.UnicastRemoteObject;

// 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)


UnicastRemoteObject.exportObject(remoteObject, 0);

// Create and bind the registry


Registry registry = LocateRegistry.createRegistry(1099);

registry.rebind("MyRemoteObject", stub);

System.out.println("Server ready");

} catch (Exception e) {

System.err.println("Server exception: " + e.toString());

e.printStackTrace();

}
6.Write a program in Java to get information about particular table using result set Meta
Data.

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

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 = DriverManager.getConnection(url, username, password)) {

String query = "SELECT * FROM " + tableName + " WHERE 1 = 0"; // Fetch no data, just
metadata

try (PreparedStatement preparedStatement = connection.prepareStatement(query);

ResultSet resultSet = preparedStatement.executeQuery()) {

ResultSetMetaData metaData = resultSet.getMetaData();

int columnCount = metaData.getColumnCount();


System.out.println("Table: " + tableName);

System.out.println("Columns:");

for (int i = 1; i <= columnCount; i++) {

System.out.println(" Column Name: " + metaData.getColumnName(i));

System.out.println(" Data Type: " + metaData.getColumnTypeName(i));

System.out.println(" Nullable: " + (metaData.isNullable(i) ==


ResultSetMetaData.columnNullable ? "YES" : "NO"));

System.out.println();

} catch (SQLException e) {

e.printStackTrace();

}
5.Write a program in Java to get information about database using Database Meta Data.

import java.sql.Connection;

import java.sql.DatabaseMetaData;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

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 = DriverManager.getConnection(jdbcUrl, username, password)) {

DatabaseMetaData metaData = connection.getMetaData();

// Display database information

System.out.println("Database Product Name: " + metaData.getDatabaseProductName());

System.out.println("Database Product Version: " + metaData.getDatabaseProductVersion());

// Retrieve and display tables

System.out.println("\nTables:");

ResultSet tables = metaData.getTables(null, null, "%", null);

while (tables.next()) {

System.out.println(tables.getString("TABLE_NAME"));

} catch (SQLException e) {

e.printStackTrace();
4.Write a program in Java to show connectivity with database using JDBC/ODBC driver.

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

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 = DriverManager.getConnection(url, username, password)) {

if (connection != null) {

System.out.println("Connected to the database!");

// Perform database operations here if needed

} else {

System.out.println("Failed to connect to the database.");

} catch (SQLException e) {

e.printStackTrace();

}
9.Write a program in Java, Servlets to get and display value from an HTML page.

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@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 = request.getParameter("inputName");

// Set the response content type

response.setContentType("text/html");

// Create PrintWriter object to send HTML response

PrintWriter out = response.getWriter();

// Display the received value

out.println("<html><body>");

out.println("<h2>Received value from HTML page:</h2>");

out.println("<p>" + inputValue + "</p>");

out.println("</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++) {

System.out.println(Thread.currentThread().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

thread1.start();

thread2.start();

}
3. Write a program in Java to Create a New Data Source for MS Access.
import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.Statement;

public class CreateDataSource {

public static void main(String[] args) {

// JDBC connection parameters

String url = "jdbc:ucanaccess://C:/path/to/your/database.accdb";

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 = DriverManager.getConnection(url, user, password);

// Create a statement for executing SQL queries

Statement statement = connection.createStatement()

){

// 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))";

statement.execute(createTableSQL);

System.out.println("Table created successfully.");

} catch (Exception e) {

e.printStackTrace();

}
7. Write a program in Java to implement the concept of swings.
import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class ExampleForSwing {

public static void main(String[] args) {

JFrame frame = new JFrame("Swing Example"); // Create a JFrame

// Set the default close operation

frame.setDefaultCloseOperation(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

panel.add(label);

panel.add(button);

// Add the panel to the frame

frame.add(panel);

// Set the size of the frame

frame.setSize(300, 200);

// Set the frame to be visible

frame.setVisible(true);

}
10. Write a program in Java, JSP to get and display value from an HTML page.
1. Create an HTML file (index.html):

<!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="display.jsp" method="post">

Enter your name: <input type="text" name="userName">

<input type="submit" value="Submit">

</form>

</body>

</html>

2. Create a JSP file (display.jsp):

<%@ 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, <%= request.getParameter("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

You might also like