Using JDBC to access Greenplum

Source: Internet
Author: User
Tags connect postgresql

The JDBC is the driver used to access a database with Java. Greenplum has a full working JDBC implementation.

In that short article we'll have a.

# # Download and install

It is possible to download the JDBC for Greenplum directly from the Greenplum Community Edition site (http://www.greenplum . com/community/downloads/database-ce/).

Look for the * "Connectivity Tools" * file.

You'll receive a link to download the archive file.

Extract the archive and run the binary extracted. Then Follow the instructions on screens and in less than a minute you have installed JDBC.

# # Prepare The Greenplum server

After a successful installation, make sure this server accepts TCP connections from the desired hosts. Check that *listen_addresses* are properly set in postgresql.conf.

**note:** by default and Greenplum listens to any address.

Another aspect you have to consider are the user authentication, which is delegated to the pg_hba.conf file (please refer T o page of Greenplum adminguide for more information).

After you have verified the "user is able" to "the" database, you can go on and test JDBC.

Connecting to a Greenplum Database and JDBC is a three steps procedure:

* Import JDBC

* Load the driver

* Connect to the database

Remember that the *forname* function can throw a *classnotfoundexception* if the driver are not available. We don't try to catch this exception in the simple example below. You are should in your production environment.

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

To connect to a database using JDBC, you have to use a connection URL. It can be in one of these three forms:

* Jdbc:postgresql:databasename

* Jdbc:postgresql://host/databasename

* Jdbc:postgresql://host:port/databasename

Build a URL that is suits your needs and use it with the getconnection function. For example:

Package com.gp.dbtest;

Import java.sql.Connection;

Import Java.sql.DriverManager;

Import Java.sql.ResultSet;

Import java.sql.SQLException;

Import java.sql.Statement;

public class Gpdbtest {

public static void Main (string[] args) {

try {

Class.forName ("Org.postgresql.Driver");

Connection db = Drivermanager.getconnection ("jdbc:postgresql://192.168.1.23:5432/zwcdb", "ZHONGWC", "ZHONGWC");

Statement st = Db.createstatement ();

ResultSet rs = st.executequery ("SELECT * from TAB_GP limit 1 offset 0");

while (Rs.next ()) {

System.out.println (rs.getstring (1));

}

Rs.close ();

St.close ();

catch (ClassNotFoundException e) {

E.printstacktrace ();

catch (SQLException e) {

E.printstacktrace ();

}

}

}

Author: csdn Blog Zhongweicheng

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.