UNIT V – Networking and JDBC
Program 1: Basic Socket Client
Statement: Write a Java program to create a TCP client socket that connects to a server.
import java.io.*;
import java.net.*;
class Client {
public static void main(String[] args) {
try {
Socket s = new Socket("localhost", 6666);
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
} catch (Exception e) {
System.out.println(e);
Explanation: This client sends a message to the server using a TCP socket.
Program 2: Basic Socket Server
Statement: Write a Java program to create a TCP server socket that listens and responds.
import java.io.*;
import java.net.*;
class Server {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(6666);
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = dis.readUTF();
System.out.println("Message: " + str);
ss.close();
} catch (Exception e) {
System.out.println(e);
Explanation: Accepts a connection and prints the message from the client.
Program 3: UDP Client
Statement: Write a Java program to send a message using UDP client.
import java.net.*;
class UDPClient {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket();
String str = "Hello";
InetAddress ip = InetAddress.getByName("localhost");
DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), ip, 3000);
ds.send(dp);
ds.close();
Explanation: Uses DatagramSocket and DatagramPacket to send data via UDP.
Program 4: UDP Server
Statement: Write a Java program to receive a message using UDP server.
java
CopyEdit
import java.net.*;
class UDPServer {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(3000);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, 1024);
ds.receive(dp);
String str = new String(dp.getData(), 0, dp.getLength());
System.out.println("Received: " + str);
ds.close();
Explanation: Receives a datagram and prints the message from the client.
Program 5: JDBC Connection
Statement: Connect to a MySQL database using JDBC.
import java.sql.*;
class JdbcConnect {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mydb", "root", "password");
System.out.println("Connection established");
con.close();
} catch (Exception e) {
System.out.println(e);
}
Explanation: Establishes a JDBC connection with MySQL.
Program 6: Insert Record using JDBC
Statement: Insert a new record into a table using JDBC.
import java.sql.*;
class JdbcInsert {
public static void main(String[] args) {
try {
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mydb", "root", "password");
Statement stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO student VALUES(1, 'John')");
System.out.println("Record inserted");
con.close();
} catch (Exception e) {
System.out.println(e);
Explanation: Demonstrates executing an INSERT SQL statement via JDBC.
Program 7: Select Records using JDBC
Statement: Fetch records from the database using JDBC.
import java.sql.*;
class JdbcSelect {
public static void main(String[] args) {
try {
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mydb", "root", "password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM student");
while (rs.next())
System.out.println(rs.getInt(1) + " " + rs.getString(2));
con.close();
} catch (Exception e) {
System.out.println(e);
Explanation: Fetches and prints all records from the "student" table.
Program 8: Update Record using JDBC
Statement: Update an existing record using JDBC.
import java.sql.*;
class JdbcUpdate {
public static void main(String[] args) {
try {
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mydb", "root", "password");
Statement stmt = con.createStatement();
stmt.executeUpdate("UPDATE student SET name='David' WHERE id=1");
System.out.println("Record updated");
con.close();
} catch (Exception e) {
System.out.println(e);
Explanation: Updates a student record based on ID.
Program 9: Delete Record using JDBC
Statement: Delete a record using JDBC.
import java.sql.*;
class JdbcDelete {
public static void main(String[] args) {
try {
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mydb", "root", "password");
Statement stmt = con.createStatement();
stmt.executeUpdate("DELETE FROM student WHERE id=1");
System.out.println("Record deleted");
con.close();
} catch (Exception e) {
System.out.println(e);
Explanation: Deletes a record from the table based on the condition.
Program 10: Prepared Statement Example
Statement: Use Prepared Statement to insert a record.
import java.sql.*;
class JdbcPrepared {
public static void main(String[] args) {
try {
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mydb", "root", "password");
PreparedStatement ps = con.prepareStatement("INSERT INTO student VALUES(?, ?)");
ps.setInt(1, 2);
ps.setString(2, "Alex");
ps.executeUpdate();
System.out.println("Record inserted with PreparedStatement");
con.close();
} catch (Exception e) {
System.out.println(e);