Write a Java Program to display all the employee names whose initial character of a
name is ‘A’.
EmployeeNamesJDBC.java
import java.sql.*;
public class EmployeeNamesJDBC {
public static void main(String[] args) {
// JDBC connection details
String url = "jdbc:mysql://localhost:3306/company"; // change database name
String user = "root"; // your MySQL username
String password = "root"; // your MySQL password
try {
// 1. Load JDBC Driver
Class.forName("com.mysql.cj.jdbc.Driver");
// 2. Establish Connection
Connection con = DriverManager.getConnection(url, user, password);
// 3. Create Statement
Statement stmt = con.createStatement();
// 4. SQL Query to fetch names starting with 'A'
String sql = "SELECT emp_name FROM employee WHERE emp_name LIKE 'A%'";
ResultSet rs = stmt.executeQuery(sql);
System.out.println("Employee names starting with 'A':");
// 5. Process ResultSet
while (rs.next()) {
String name = rs.getString("emp_name");
System.out.println(name);
}
// 6. Close resources
rs.close();
stmt.close();
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}