[postgis-users] Connecting to database<<<java

Nicolas RIBOT nri at scot.cnes.fr
Fri Aug 13 09:52:23 PDT 2004


Hello, 
you can try something like the following code

You have to put the Postgresql Java driver in your classpath
If you want to use postgis java types, also include postgis.jar in your
classpath.

As far as I know, the postgresql Java driver is created when building
postgresql from sources if you set the right java option.

Nicolas


------------- begin code------------------


import java.sql.*;
import org.postgis.*;

public class Test_jdbc {
    public Test_jdbc() {
    }
    // executes single SQL query
    public static void test(java.sql.Connection conn) {
        String query = "select nom_com from communes ";
        Statement stmt = null;

        try {
            stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(query);
            String s = "";
            while (rs.next()) {
                // do something with the result
                s = rs.getString(1);
            }
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }
        finally {
            try {stmt.close();} catch (Exception
e){e.printStackTrace();}
        }
    }

	public static void main(String[] argv) {
		try {
            Class driver=Class.forName("org.postgresql.Driver");
			java.sql.Connection
conn=DriverManager.getConnection(dbURL,"postgres", "postgres");
 
((org.postgresql.Connection)conn).addDataType("geometry","org.postgis.PG
geometry");
 
((org.postgresql.Connection)conn).addDataType("box3d","org.postgis.PGbox
3d");
            test(conn);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

-------------end code-------------------------


Hi !
I am a fresher in postgresql and postgis.But i have to implement this
and connect to databse from java.So can you please let me know how we
can connect to databse from postgre sql.
Looking forward to hear soon.Becasue its very important for me.Than ing
you in advance.
Sincerely
AshokKuppala


Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!



More information about the postgis-users mailing list