0% found this document useful (0 votes)
3 views

Java_Assignment_Answers

The document provides answers to Java assignment questions covering differences between AWT and Swing, Scrollbar and ScrollPane, and TextField and TextArea. It explains various layout managers, methods for creating multithreading, and includes a basic JDBC program example. Additionally, it discusses socket programming and the distinction between byte streams and character streams.

Uploaded by

hiyaaank
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java_Assignment_Answers

The document provides answers to Java assignment questions covering differences between AWT and Swing, Scrollbar and ScrollPane, and TextField and TextArea. It explains various layout managers, methods for creating multithreading, and includes a basic JDBC program example. Additionally, it discusses socket programming and the distinction between byte streams and character streams.

Uploaded by

hiyaaank
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Assignment Answers

1. What is the difference between:i) AWT and Swingii) Scroll bar and Scrollpaneiii) Textfield and Textarea

AWT and Swing:


AWT (Abstract Window Toolkit) is older and uses native OS resources, whereas Swing is newer, platform-independent,
and provides richer features.

Scrollbar vs ScrollPane:
Scrollbar is a component, ScrollPane is a container that wraps around components and adds scrolling automatically.

TextField vs TextArea:
TextField is for single-line input, TextArea for multi-line input.

2. Explain different types of Layout Manager with example.What are the two ways of creating Multithreading

Types of Layout Managers:


1. FlowLayout: Left to right
2. BorderLayout: North, South, East, West, Center
3. GridLayout: Grid of rows/columns
4. CardLayout: Stack of cards

Multithreading:
1. Extending Thread class
2. Implementing Runnable interface
Synchronization is used to control access to shared resources.
Daemon thread is a low-priority background thread.

3. Write a program for basic JDBC program that connects to a database and fetches some data?

JDBC Program:
import java.sql.*;
public class JDBCExample {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname", "root", "pass");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
while(rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2));
}
con.close();
}
}

4. What is socket Programming in Java explained with example.What is the difference between a byte stream

Socket Programming:
Used for communication between machines.
Server:
ServerSocket ss = new ServerSocket(6666);
Java Assignment Answers

Socket s = ss.accept();
Client:
Socket s = new Socket("localhost", 6666);

Byte vs Character Stream:


Byte stream handles binary data using InputStream/OutputStream.
Character stream handles text using Reader/Writer.

You might also like