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

Slip 16 Java

The document contains two Java programs: the first creates a TreeSet to store and display colors in ascending order, while the second accepts teacher details and displays those teaching 'JAVA' using a PreparedStatement with a MySQL database. The first program demonstrates basic data structure usage, and the second illustrates database interaction and data retrieval. Both programs include necessary imports and exception handling.

Uploaded by

vthombare174
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 views4 pages

Slip 16 Java

The document contains two Java programs: the first creates a TreeSet to store and display colors in ascending order, while the second accepts teacher details and displays those teaching 'JAVA' using a PreparedStatement with a MySQL database. The first program demonstrates basic data structure usage, and the second illustrates database interaction and data retrieval. Both programs include necessary imports and exception handling.

Uploaded by

vthombare174
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

Slip 16

Q1. Java program to create a TreeSet, add colors and display in ascending order

Java Program

Copy code

Java

import [Link].*;

public class TreeSetColors {

public static void main(String[] args) {

TreeSet<String> colors = new TreeSet<String>();

[Link]("Red");

[Link]("Blue");

[Link]("Green");

[Link]("Yellow");

[Link]("Orange");

[Link]("Colors in Ascending Order:");

for (String c : colors) {

[Link](c);

Q2. Java program to accept Teacher details and display teachers teaching “JAVA” (Using
PreparedStatement)
Database Table (Teacher)

Copy code

Sql

CREATE TABLE Teacher (

TNo INT,

TName VARCHAR(30),

Subject VARCHAR(20)

);

Java Program: [Link]

Copy code

Java

import [Link].*;

import [Link];

public class TeacherDetails {

public static void main(String[] args) {

try {

[Link]("[Link]");

Connection con = [Link](

"jdbc:mysql://localhost:3306/test", "root", "root");

Scanner sc = new Scanner([Link]);

String insertQuery = "INSERT INTO Teacher VALUES (?, ?, ?)";


PreparedStatement ps = [Link](insertQuery);

[Link]("Enter 5 Teacher Records:");

for (int i = 1; i <= 5; i++) {

[Link]("Enter TNo: ");

[Link](1, [Link]());

[Link]("Enter TName: ");

[Link](2, [Link]());

[Link]("Enter Subject: ");

[Link](3, [Link]());

[Link]();

String selectQuery = "SELECT * FROM Teacher WHERE Subject=?";

PreparedStatement ps1 = [Link](selectQuery);

[Link](1, "JAVA");

ResultSet rs = [Link]();

[Link]("\nTeachers teaching JAVA:");

while ([Link]()) {
[Link](

[Link](1) + " " +

[Link](2) + " " +

[Link](3));

[Link]();

} catch (Exception e) {

[Link](e);

You might also like