0% found this document useful (0 votes)
2 views3 pages

Java Programs Wrapper JDBC

The document provides Java programs demonstrating the use of wrapper classes with autoboxing and unboxing, as well as a JDBC application for connecting to a MySQL database. It includes code examples for converting primitive types to wrapper objects and vice versa, along with a database setup and record insertion into a student table. The output showcases successful operations for both autoboxing/unboxing and database interactions.

Uploaded by

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

Java Programs Wrapper JDBC

The document provides Java programs demonstrating the use of wrapper classes with autoboxing and unboxing, as well as a JDBC application for connecting to a MySQL database. It includes code examples for converting primitive types to wrapper objects and vice versa, along with a database setup and record insertion into a student table. The output showcases successful operations for both autoboxing/unboxing and database interactions.

Uploaded by

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

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

You might also like