Java Programs with Wrapper Classes and JDBC
Q3. Develop a program using wrapper classes and demonstrate autoboxing and unboxing.
public class WrapperExample {
public static void main(String[] args) {
// Autoboxing: Converting primitive to wrapper object
int a = 10;
Integer obj = a; // Autoboxing
[Link]("Autoboxing:");
[Link]("Primitive int a = " + a);
[Link]("Wrapped Integer obj = " + obj);
// Unboxing: Converting wrapper object to primitive
Integer num = new Integer(20);
int b = num; // Unboxing
[Link]("\nUnboxing:");
[Link]("Wrapped Integer num = " + num);
[Link]("Primitive int b = " + b);
Output:
Autoboxing:
Primitive int a = 10
Wrapped Integer obj = 10
Unboxing:
Wrapped Integer num = 20
Primitive int b = 20
Q4. Design a Java application using JDBC to connect to a database, insert new records into a stude
Java Programs with Wrapper Classes and JDBC
Database Setup (MySQL):
CREATE DATABASE college;
USE college;
CREATE TABLE student (
id INT PRIMARY KEY,
name VARCHAR(100),
age INT
);
import [Link].*;
public class JDBCExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/college";
String user = "root";
String password = "your_password";
try {
[Link]("[Link]");
Connection con = [Link](url, user, password);
Statement stmt = [Link]();
String insertSQL = "INSERT INTO student (id, name, age) VALUES (1, 'Rizzvi', 21)";
[Link](insertSQL);
[Link]("Record inserted successfully!");
ResultSet rs = [Link]("SELECT * FROM student");
[Link]("\nStudent Table:");
while ([Link]()) {
Java Programs with Wrapper Classes and JDBC
[Link]("ID: " + [Link]("id") +
", Name: " + [Link]("name") +
", Age: " + [Link]("age"));
[Link]();
[Link]();
[Link]();
} catch (Exception e) {
[Link]();
Output:
Record inserted successfully!
Student Table:
ID: 1, Name: Rizzvi, Age: 21