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 can I use Amazon Redshift to store and process unstructured data?
- How do I use Amazon Redshift RSQL to query data?
- How can I handle divide by zero errors when using Amazon Redshift?
- How do I set up Amazon RDS with Multi-AZ for high availability?
- How do I use Amazon Redshift with YouTube?
- How can I transfer data from Amazon Redshift to an Amazon S3 bucket?
- How do I convert an Amazon Redshift timestamp to a date?
- How can I monitor Amazon RDS using Zabbix?
- How do I use Amazon Redshift to store data in an S3 bucket?
See more codes...