Figure 5.1: Concept of Input Stream and Output Stream in Java
Figure 5.1: Concept of Input Stream and Output Stream in Java
Data SourceFile / Keyboard / Network
→ InputStream / Reader (Read) →
Java ProgramProcesses Data in RAM
→ OutputStream / Writer (Write) →
DestinationFile / Monitor / Socket
Explanation
- An input stream carries bytes from a source into the program.
- An output stream carries bytes from the program to a destination.
Figure 5.2: Hierarchy of Java I/O Stream Classes (java.io)
Figure 5.2: Hierarchy of Java I/O Stream Classes (java.io)
1. Byte Streams (8-Bit Raw Binary)
- Root Classes:
InputStream&OutputStream - Reads/writes data 1 byte (8 bits) at a time
- Used for Images, Audio, Video, PDFs, & Binary files
- Examples:
FileInputStream,FileOutputStream,PrintStream,ObjectInputStream
2. Character Streams (16-Bit Unicode Text)
- Root Classes:
Reader&Writer - Reads/writes data 1 character (2 bytes / 16 bits) at a time
- Automatically handles Unicode character encoding
- Examples:
FileReader,FileWriter,BufferedReader,BufferedWriter,PrintWriter
Explanation
- InputStream and OutputStream are the roots of byte-oriented streams.
- Reader and Writer handle character data and can use an encoding at the byte boundary.
Figure 5.3: Sequential Stream vs RandomAccessFile (seek() Pointer)
Figure 5.3: Sequential Stream vs RandomAccessFile (seek() Pointer)
Sequential Stream (FileInputStream)
- Reads strictly in one direction: Byte 0 → 1 → 2 → EOF
- Separate classes for Reading and Writing
- Cannot move backwards or jump to middle of file
RandomAccessFile
- Maintains a movable cursor called the File Pointer
- Can jump to ANY byte instantly using
seek(long pos) - Can BOTH Read and Write using the same object!
Explanation
- A sequential stream advances as bytes are read or written.
- RandomAccessFile can move its file pointer to a chosen byte offset with seek.
Figure 5.4: How BufferedReader Speeds Up File Reading Using a RAM Buffer
Figure 5.4: How BufferedReader Speeds Up File Reading Using a RAM Buffer
Hard Disk File
notes.txt→
FileReader →BufferedReader (8 KB RAM Buffer)Fetches large chunk at once & provides
readLine()→ Fast Read →
Java ProgramReads entire lines instantly!
Explanation
- A buffer reads a group of characters from the underlying reader.
- Small application reads can then be served from memory until the buffer needs refilling.
Figure 5.5: Object Serialization and Deserialization in Java
Figure 5.5: Object Serialization and Deserialization in Java
Live Java Object
Student s1 (Implements Serializable)→
ObjectOutputStream.writeObject() →Byte Stream File
student.ser (On Disk / Network)→
ObjectInputStream.readObject() →Restored Object
Student s2 in RAMExplanation
- Serialization writes supported object state into a byte representation.
- Deserialization reconstructs that state from a compatible, trusted stream.
Figure 5.6: The JDBC Connectivity Model (Architecture)
Figure 5.6: The JDBC Connectivity Model (Architecture)
Java ApplicationCalls
java.sql methods↔
JDBC DriverManagerLoads & selects the right Driver
↔
JDBC DriverType-1 (ODBC) / Type-4 (Thin Driver)
↔
Database ServerMySQL / Oracle / SQL Server
Explanation
- Application code uses the JDBC interface to issue database operations.
- A compatible driver translates those calls for the chosen database.
Figure 5.7: How the Type-1 JDBC-ODBC Bridge Driver Works
Figure 5.7: How the Type-1 JDBC-ODBC Bridge Driver Works
Java AppJDBC Calls
→
Type-1 JDBC-ODBC BridgeTranslates JDBC → ODBC
→
OS ODBC DriverNative C/C++ Library (DSN)
→
DatabaseMS Access / SQL
Explanation
- The historical bridge translated JDBC calls into ODBC calls.
- The ODBC driver then communicated with the database, adding a local dependency.
Figure 5.9: Hierarchy of JDBC Exception Classes (java.sql.SQLException)
Figure 5.9: Hierarchy of JDBC Exception Classes (java.sql.SQLException)
Exception → java.sql.SQLException (Base class for all database errors)
SQLWarning & DataTruncation
•
•
•
SQLWarning: Non-fatal database warnings retrieved via getWarnings()•
DataTruncation: Data value was unexpectedly cut off
BatchUpdateException
Thrown when one of the SQL commands fails during a batch update (
Thrown when one of the SQL commands fails during a batch update (
executeBatch())
Explanation
- SQLException carries database error information and can link related exceptions.
- Its subclasses help distinguish problems such as a temporary failure from an invalid operation.
Anatomy of a JDBC Connection URL
Anatomy of a JDBC Connection URL
Protocol & Driver: jdbc:mysql
Identifies Java Database Connectivity API and the target MySQL driver.
Remote Server IP: 192.168.1.50
Network IP address or host domain where database daemon is running.
DB Port: 3306
TCP port listening for queries (MySQL default: 3306, Oracle: 1521).
Database Name: davv_db
Specific database schema containing the tables to query and update.
Explanation
- A JDBC URL begins with jdbc and a driver-specific subprotocol.
- The remaining details identify the database and connection settings for that driver.