amazon-redshiftHow do I connect to Amazon Redshift using JDBC?
- Download the Amazon Redshift JDBC driver from the Amazon Redshift JDBC driver download page
- Add the driver to your classpath.
- Create a JDBC connection URL using the following syntax:
jdbc:redshift://<hostname>:<port>/<dbname>
where <hostname>
is the endpoint of your cluster, <port>
is the port your cluster is using, and <dbname>
is the name of your database.
- Establish a connection using the
DriverManager.getConnection()
method, passing in the connection URL, username, and password as parameters.
String url = "jdbc:redshift://examplecluster.1234567890.us-west-2.redshift.amazonaws.com:5439/dev";
String user = "awsuser";
String password = "mypassword";
Connection con = DriverManager.getConnection(url, user, password);
- Once the connection is established, you can execute SQL statements using the
Statement
orPreparedStatement
interfaces.
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM mytable");
while (rs.next()) {
System.out.println(rs.getString("column1"));
}
- When you are finished, close the connection using the
Connection.close()
method.
con.close();
- For more information, refer to the Amazon Redshift JDBC Driver Developer Guide.
More of Amazon Redshift
- How do I use the Amazon Redshift YEAR function?
- How do I use Amazon Redshift with YouTube?
- How can I use Amazon Redshift UNION to combine data from multiple tables?
- How can I handle divide by zero errors when using Amazon Redshift?
- How do I set up Amazon RDS with read replicas?
- How do I use Amazon Redshift window functions?
- How can I monitor Amazon RDS using Zabbix?
- How can I set up an Amazon RDS MySQL instance?
- How do I set up Amazon RDS with Multi-AZ for high availability?
- How can I use Amazon Redshift to store and process unstructured data?
See more codes...