JAVA 16
16 TreeSet, add some colors && details of Teacher
Q1]
import java.util.*;
public class TreeSetExample {
public static void main(String[] args) {
TreeSet<String> colors = new TreeSet<>();
colors.add("Red");
colors.add("Green");
colors.add("Blue");
colors.add("Yellow");
colors.add("Orange");
System.out.println("Colors in ascending order:");
for (String color : colors) {
System.out.println(color);
}
}
}
Q2]
import java.sql.*;
public class TeacherDetails {
static final String JDBC_URL = "jdbc:mysql://localhost:3306/your_database_name";
static final String USER = "your_username";
static final String PASSWORD = "your_password";
public static void main(String[] args) {
try (Connection conn = DriverManager.getConnection(JDBC_URL, USER, PASSWORD)) {
insertTeachers(conn);
displayJavaTeachers(conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void insertTeachers(Connection conn) throws SQLException {
String insertQuery = "INSERT INTO Teacher (TNo, TName, Subject) VALUES (?, ?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(insertQuery)) {
for (int i = 1; i <= 5; i++) {
pstmt.setInt(1, i);
pstmt.setString(2, "Teacher" + i);
pstmt.setString(3, "Subject" + i);
pstmt.executeUpdate();
}
System.out.println("Records inserted successfully into Teacher table.");
}
}
private static void displayJavaTeachers(Connection conn) throws SQLException {
String selectQuery = "SELECT * FROM Teacher WHERE Subject = ?";
try (PreparedStatement pstmt = conn.prepareStatement(selectQuery)) {
pstmt.setString(1, "JAVA");
ResultSet rs = pstmt.executeQuery();
System.out.println("Details of teachers teaching 'JAVA' subject:");
System.out.println("=============================================");
while (rs.next()) {
System.out.println("Teacher Number: " + rs.getInt("TNo"));
System.out.println("Teacher Name: " + rs.getString("TName"));
System.out.println("Subject: " + rs.getString("Subject"));
System.out.println("---------------------------------------------");
}
}
}
}