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