安装所有软件包:
# apt-get install libpostgresql-jdbc-java libpostgresql-jdbc-java-doc
要为所有用户设置 Java 环境,请添加/编辑 /etc/environment:
JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"
CLASSPATH=".:/usr/share/java/mysql.jar:/usr/share/java/postgresql-jdbc4.jar"
注意:用你的 JDK 更改 /usr/lib/jvm/java-8-openjdk-amd64
注意:如果您更喜欢使用 postgresql-jdbc3,请将 /usr/share/java/postgresql-jdbc4.jar 替换为 /usr/share/java/postgresql.jar
使用以下代码测试您的连接:
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());
}
}
}
注意:使用您的配置更改数据库、用户和密码
http://www.garasiku.web.id/web/joomla/index.php/java/112-debian-jessie-installing-openjdk-8-mysql-jdbc-and-postgresql-jdbc