import java.sql.*;
public class RedshiftJDBCExample {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.amazon.redshift.jdbc.Driver");
String url = "jdbc:redshift://redshift-cluster-1.xxxxxx.us-east-1.redshift.amazonaws.com:5439/dbname";
String username = "your-username";
String password = "your-password";
conn = DriverManager.getConnection(url, username, password);
stmt = conn.createStatement();
String sql = "SELECT * FROM table_name";
rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println("ID: " + id + ", Name: " + name);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}