Unit 3: Fundamentals of Java ProgrammingOOP Class Design, NetBeans, JDBC & Exception Handling
Exhaustive, board-aligned notes based on Pages 81–143 of the CBSE Class XII Student Handbook. Master Java bytecode & JVM architecture, NetBeans IDE project creation, control flow statements, 1D arrays, OOP class construction, access modifiers, Scanner & String libraries, JDBC MySQL connectivity, multithreading, and solved programming lab exercises.
.class files), which any OS running a Java Virtual Machine (JVM) translates into native machine code.extends keyword. Polymorphism: Overloading & overriding. Constructors: Same name as class, no return type.DriverManager.getConnection(), 2) conn.createStatement(), 3) stmt.executeQuery(), 4) ResultSet.next(), wrapped in try-catch (SQLException).Java Architecture, Primitive Types & NetBeans Project Setup
In Java, all executable code is contained inside classes. The main method (public static void main(String[] args)) is the mandatory entry point for every standalone application.
8 Java Primitive Data Types (Table 1, Page 93)
| Data Type | Kind of Value | Memory Size | Example Value |
|---|---|---|---|
| byte | Integer | 8-bit | 100 |
| short | Integer | 16-bit | 5000 |
| int | Integer | 32-bit | 56789 |
| long | Integer | 64-bit | 9876543210L |
| float | Floating Point | 32-bit | 3.14f |
| double | Floating Point | 64-bit | 346.75 |
| char | Character (Unicode) | 16-bit | 'M' |
| boolean | Logical Truth | 1-bit | true / false |
Control Flow: Selection Structures & Loops
if-else evaluates boolean conditions. switch matches an expression against constant case values using break to terminate fall-through.
switch (today) {
case 1: day = "Monday"; break;
case 5: day = "Friday"; break;
default: day = "Incorrect Day!"; break;
}
| while LOOP | do-while LOOP |
|---|---|
| Entry-controlled loop (tests condition prior to body) | Exit-controlled loop (tests condition after body) |
| Runs 0 or more times (may never execute body) | Always runs once or more (at least once) |
| No semicolon after while condition | Requires semicolon after while (cond); |
Object-Oriented Programming (OOP) & Class Design (The Book Class)
A Class is a template/blueprint representing a real-world entity with state (attributes) and behavior (methods). An Object is a concrete instance instantiated using new.
// Complete Book.java Class with Encapsulation & Constructors (Pages 115-119)
package javaprograms;
public class Book {
// Private data members (Encapsulation)
private String title;
private String author;
private String publisher;
private String genre;
private double price;
// Parameterized Constructor
public Book(String t, String a, String pub, String g, double p) {
title = t; author = a; publisher = pub; genre = g; price = p;
}
// Controlled Getter & Setter methods
public double getPrice() { return price; }
public void setPrice(double newprice) {
if (newprice < 100) {
System.out.println("Price cannot be set lower than 100!");
} else {
price = newprice;
}
}
public void display() {
System.out.println("Title: " + title + " | Author: " + author + " | Price: " + price);
}
}
Java Database Connectivity (JDBC) with MySQL Server
JDBC (Java Database Connectivity) allows Java applications to execute SQL queries on relational databases. In NetBeans, configure the MySQL connection under the Services tab and add mysql-connector-java-bin.jar to the project libraries.
// MySQLDemo.java - Complete JDBC Query Pipeline (Page 136)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MySQLDemo {
public static void main(String[] args) {
String dbURL = "jdbc:mysql://localhost:3306/bookstore";
String username = "root";
String password = "password";
try {
// 1. Establish connection
Connection dbCon = DriverManager.getConnection(dbURL, username, password);
// 2. Create Statement
Statement stmt = dbCon.createStatement();
// 3. Execute query
String query = "SELECT * FROM book";
ResultSet rs = stmt.executeQuery(query);
// 4. Iterate over rows
while (rs.next()) {
for (int i = 1; i <= 5; i++) {
System.out.print(rs.getString(i) + " | ");
}
System.out.println();
}
dbCon.close();
} catch (SQLException ex) {
System.out.println("Database Error: " + ex.getMessage());
}
}
}
Advanced Java: Assertions, Threads & Wrapper Classes
States conditions that must be true during execution. Used for debugging. Throws AssertionError if false. Enabled via -ea VM flag.
assert age >= 18 : "Age not Valid";Executes tasks concurrently. Created either by extending Thread or by implementing Runnable and overriding run(). Started using t.start().
Wraps primitives into objects (Integer, Double, Boolean). Supports parsing: Integer.parseInt("345") and toString().
Official Unit 3 Practice Questions
Q1. Why are compiled Java programs platform independent?
Q2. What is the memory size and storage capability of the 'int' primitive data type in Java?
Q3. What is the fundamental difference in execution count between a 'while' loop and a 'do-while' loop?
Q4. What exception is thrown at runtime if a program attempts to access an array of size 5 at index 5?
Q5. Which of the following is true regarding a Constructor method in Java?
Q6. Which access modifier must be applied to class data members to enforce encapsulation and hide internal data from outside modification?
Q7. Before executing Arrays.binarySearch() to search for an element, what prerequisite condition must be met?
Q8. Which method of the DriverManager class is invoked to establish a connection to a MySQL database in JDBC?
Q9. What command-line VM flag must be passed to the Java runtime to enable assertions at execution time?
Q10. What are the two official ways to create a multi-threaded program in Java?
Unit 3 Java Programming FAQs (AEO & Board Focus)
Score 100/100 in Class 12 Java & Practical Lab
TeacherColab provides 1-on-1 personalized tutoring for Java OOP NetBeans projects, JDBC database connectivity, and board viva voce prep.