0% found this document useful (0 votes)
6 views10 pages

Java 2026

The document outlines key object-oriented programming concepts used in a medical store application, including inheritance, polymorphism, encapsulation, abstraction, and exception handling. It details the use of JDBC for database connectivity with MySQL, demonstrating how to insert and display medicine records. Additionally, it provides code examples for class structures and methods to manage medicine data effectively.

Uploaded by

varunacharv4
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

Java 2026

The document outlines key object-oriented programming concepts used in a medical store application, including inheritance, polymorphism, encapsulation, abstraction, and exception handling. It details the use of JDBC for database connectivity with MySQL, demonstrating how to insert and display medicine records. Additionally, it provides code examples for class structures and methods to manage medicine data effectively.

Uploaded by

varunacharv4
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

🧠 Concepts Used in Medical Store Application

1️⃣ Inheritance (OOP)


 Meaning: One class acquires properties of another
class
 Used as:
o Medicine → Parent class
o Tablet, Syrup → Child classes
👉 This avoids code duplication.
class Medicine { ... }

class Tablet extends Medicine { ... }


class Syrup extends Medicine { ... }

2️⃣ Polymorphism
 Meaning: Same method, different behavior
 Used as: Method overriding (display())
@Override
public void display() {
[Link]("Tablet: " + name);
}
👉 Same method display() behaves differently for Tablet
& Syrup.

3️⃣ Encapsulation
 Meaning: Wrapping data + restricting access
 Used as: Variables inside class
protected int id;
protected String name;
protected double price;
👉 Data is controlled within the class.

4️⃣ Abstraction (Conceptual)


 Meaning: Hiding internal details, showing only
necessary parts
 Used as: User interacts with methods like:
insertMedicine()
displayMedicines()
👉 User doesn't see database logic internally.

5️⃣ Exception Handling


 Meaning: Handling runtime errors safely
 Used as: try-catch blocks
try {
Connection con = [Link](...);
} catch (Exception e) {
[Link]("Error: " + e);
}
👉 Prevents program crash if:
 DB not connected
 Wrong SQL
 Driver issue

6️⃣ JDBC (Java Database Connectivity)


From your file, these steps are used:
🔹 a) Load Driver
[Link]("[Link]");
🔹 b) Create Connection
Connection con = [Link](url,
user, password);
🔹 c) Create Statement
Statement stmt = [Link]();
🔹 d) Execute Query
ResultSet rs = [Link]("SELECT * FROM
medicine");
🔹 e) Process Result
while([Link]()) {
[Link]([Link](1));
}

7️⃣ MySQL (Database Concept)


 Database used: medical_db
 Table used: medicine
👉 Stores:
 Medicine ID
 Name
 Price
 Type

8️⃣ PreparedStatement (Advanced JDBC)


 Used for safe SQL execution
PreparedStatement pst =
[Link](query);
👉 Prevents SQL Injection + improves performance

9️⃣ Class & Object Concept


 Class: Blueprint → Medicine
 Object: Real instance
Tablet t1 = new Tablet(1, "Paracetamol", 50);

CODE :
import [Link].*;

// Parent Class
class Medicine {
protected int id;
protected String name;
protected double price;

public Medicine(int id, String name, double price) {


[Link] = id;
[Link] = name;
[Link] = price;
}

public void display() {


[Link](id + " " + name + " " + price);
}
}

// Child Class 1
class Tablet extends Medicine {
public Tablet(int id, String name, double price) {
super(id, name, price);
}

@Override
public void display() {
[Link]("Tablet: " + id + " " + name + " " +
price);
}
}

// Child Class 2
class Syrup extends Medicine {
public Syrup(int id, String name, double price) {
super(id, name, price);
}

@Override
public void display() {
[Link]("Syrup: " + id + " " + name + " " +
price);
}
}

// Main Class
public class MedicalStoreApp {

static final String url =


"jdbc:mysql://localhost:3306/medical_db";
static final String user = "root";
static final String password = "your_password"; // change
this

// Insert Method
public static void insertMedicine(Medicine m, String type) {
try {
[Link]("[Link]");

Connection con = [Link](url,


user, password);

String query = "INSERT INTO medicine VALUES


(?, ?, ?, ?)";
PreparedStatement pst =
[Link](query);

[Link](1, [Link]);
[Link](2, [Link]);
[Link](3, [Link]);
[Link](4, type);

[Link]();

[Link]("Medicine inserted successfully!");

[Link]();

} catch (Exception e) {
[Link]("Error inserting data: " + e);
}
}

// Display Method
public static void displayMedicines() {
try {
Connection con = [Link](url,
user, password);

Statement stmt = [Link]();


ResultSet rs = [Link]("SELECT * FROM
medicine");

[Link]("\n--- Medicine Records ---");

while ([Link]()) {
[Link](
[Link](1) + " | " +
[Link](2) + " | " +
[Link](3) + " | " +
[Link](4)
);
}

[Link]();

} catch (SQLException e) {
[Link]("Error fetching data: " + e);
}
}

// Main Method
public static void main(String[] args) {

// Creating objects (Polymorphism)


Medicine t1 = new Tablet(1, "Paracetamol", 50);
Medicine s1 = new Syrup(2, "Cough Syrup", 80);

// Display
[Link]();
[Link]();

// Insert into DB
insertMedicine(t1, "Tablet");
insertMedicine(s1, "Syrup");

// Display from DB
displayMedicines();
}
}

You might also like