PostgreSQL - installing the JDBC driver

I find it difficult to figure out how I should install the JDBC driver for PostgreSQL on my debian 6.0 server. I moved the .jar driver to the following directory:

/usr/local/pgsql/share/java/postgresql.jar. 

Then the tutorials talk about using this code:

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

However, since I am new to postgreSQL, I have no idea where I should put this row, or even if it is correct.

My question is, without moving the jar file to this place, what do I really need to do to install the JDBC driver in my postgreSQL installation?


EDIT: This is my setup:

Server 1: Tomcat + SOLR

Server 2: PostgreSQL with JDBC Driver

SOLR on server 1 queries postgreSQL on server 2 through the JDBC driver

+7
source share
4 answers

It is best to install the PostgreSQL driver in the tomcat \ lib folder. Just copy the driver jar to PATH_TO_TOMCAT \ lib

You should not add things to the CLASSPATH system, because you can finish the addon of the loader class. Here is an example of how you go to hell.

  • Suppose the current application uses postgres 9.1 and you install the driver on the CLASSPATH system
  • You decide to run another application in this field that speaks of a newer version of postgres, say version 9.2
  • Since you are using the system classpath, application 2 will end up using the old driver, because the SYSTEM system path tends to take precedence over the application class path, unless the launcher script application sets CLASSPATH = "" to remove the system class path or uses a custom class loader that does not load the parent class.

See http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html

+5
source

This driver should be in your classpath. You can use this command

 java -cp /usr/local/pgsql/share/java/postgresql.jar my.app.MainClass 

or you can copy the library to your project structure.

Then you can create connections, as the textbooks say ...

+3
source

In your IDE (Idea, Eclipse, etc.) you need to add this path as a library.

Alternatively, you can compile and execute from the command line if you define a CLASSPATH variable that enables this.

 export CLASSPATH=/usr/local/pgsql/share/java/postgresql.jar javac -classpath $CLASSPATH MyDBApp.java java -cp $CLASSPATH MyDBApp 
+1
source

Install all packages:

 # apt-get install libpostgresql-jdbc-java libpostgresql-jdbc-java-doc 

To install the Java environment for all users, add / modify / etc / environment:

 JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64" CLASSPATH=".:/usr/share/java/mysql.jar:/usr/share/java/postgresql-jdbc4.jar" 

Note : change / usr / lib / jvm / java -8-openjdk-amd64 with your JDK

Note If you prefer to use postgresql-jdbc3, replace / usr / share / java / postgresql -jdbc4.jar with / usr / share / java / postgresql.jar

Test your connection with this code:

 import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; class TestDB { /* /usr/share/java http://dev.mysql.com/doc/connector-j/5.1/en/ https://jdbc.postgresql.org/documentation/documentation.html */ static Connection conn = null; public static void main(String[] args) { // PostgreSQL try { System.out.println("Loading Class org.postgresql.Driver"); Class.forName("org.postgresql.Driver"); System.out.println("Loading org.postgresql.Driver Successful"); String url = "jdbc:postgresql://localhost/database"; Properties props = new Properties(); props.setProperty("user","user"); props.setProperty("password","password"); props.setProperty("ssl","true"); conn = DriverManager.getConnection(url, props); // or url = "jdbc:postgresql://localhost/database?user=user&password=password&ssl=true"; Connection conn = DriverManager.getConnection(url); // Do something with the Connection System.out.println("Test Connection Successful"); } catch (SQLException ex) { // handle any errors System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); } catch (ClassNotFoundException ex) { System.out.println("Class Not Found: " + ex.getMessage()); } } } 

Note : change database, user and password with your configuration

http://www.garasiku.web.id/web/joomla/index.php/java/112-debian-jessie-installing-openjdk-8-mysql-jdbc-and-postgresql-jdbc

+1
source

All Articles