Connecting to PostgreSQL in the java application engine

存储架构 2018-01-14 阅读原文

I'm trying to connect to PostgreSQL server from java app engine. On my local machine code works perfectly, but when I deploy code to appengine, I get this exception:

Uncaught exception from servlet
    at java.util.Hashtable.put(
    at java.util.Properties.setProperty(
    at org.postgresql.Driver.loadDefaultProperties(
    at org.postgresql.Driver.access$000(
    at org.postgresql.Driver$
    at org.postgresql.Driver.getDefaultProperties(
    at org.postgresql.Driver.connect(
    at java.sql.DriverManager.getConnection(
    at java.sql.DriverManager.getConnection(
    at guestbook.PostgreServlet.doGet(
    at javax.servlet.http.HttpServlet.service(
    at javax.servlet.http.HttpServlet.service(
    at org.mortbay.jetty.servlet.ServletHolder.handle(
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(
    at org.mortbay.jetty.servlet.ServletHandler.handle(
    at org.mortbay.jetty.servlet.SessionHandler.handle(
    at org.mortbay.jetty.handler.ContextHandler.handle(
    at org.mortbay.jetty.webapp.WebAppContext.handle(
    at org.mortbay.jetty.handler.HandlerWrapper.handle(
    at org.mortbay.jetty.Server.handle(
    at org.mortbay.jetty.HttpConnection.handleRequest(
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(
    at org.mortbay.jetty.HttpConnection.handle(

I am using postgresql-9.2-1003.jdbc4.jar and I have this jdbc as library and also inside my "war/WEB-INF/lib" folder. My java code is here:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PostgreServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        PrintWriter pw = resp.getWriter();

        Connection conn = null;
        String result = "";
        try {
            conn = DriverManager.getConnection("jdbc:postgresql://", "xxx", "xxxx");
            if(conn == null){
                pw.println("Connection null");
            ResultSet rs = conn.createStatement().executeQuery(
                    "SELECT * FROM pg_catalog.pg_tables");
            while ( {
                result += rs.getString("tablename") + "
"; } pw.println(result); } catch (SQLException e) { e.printStackTrace(); } finally { if(conn != null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }

Google don't allow jdbc out, only http out.

Look at Google Secure Data Connector (which is deprecated, but works until 2015).

Connections out from the Google App Engine are only HTTP based!

Google Cloud SQL

There is some functionality in Google Cloud SQL that you might wanna look at:


There are solutions to reach databases over http (e.g.

The URLFetch API

App Engine applications can communicate with other applications or access other resources on the web by fetching URLs. An app can use the URL Fetch service to issue HTTP and HTTPS requests and receive responses. The URL Fetch service uses Google's network infrastructure for efficiency and scaling purposes.

Socket API

App Engine supports regular outbound Java sockets such as and There is currently no support for sockets via java.nio.SocketChannel or other java.nio classes.
Sockets are only available for paid apps!

(another way to use outbounds, mentioned by Peter Knego)

But I would not recommend (accessing external databases easily over internet, with public IPs) , this solution are no-no for me, it is a high security issue - my opinion!
Hello, buddy!

责编内容by:Hello, buddy!阅读原文】。感谢您的支持!


Spartan: A ”Forking” Java Program Laun... The series so far: Part 1 Part 2 Requirements for Building Spartan Spartanh...
Create an instance in the abstract class using ref... Is it possible to create an instance of a derived class in abstract ancestor class using reflection Lets say: abstract class Base {public Base cre...
Incompatible types Java error I keep receiving an error that says that there are incompatible types. I copied this directly out of a book because we are supposed to make changes...
关于Go语言,你可能会讨厌的五件事 关于 Go 语言,你有什么要吐槽的? 近年来,Go 从新出现的编程语言中脱颖而出。不过要把 Go 称为“新晋者”似乎并不合适,因为谷歌早在 2009 年就推出了 Go,并于 2012 年发布了第一个最终版(Go 1.0)。到现在为止,Go 已经发展到了 1.10 版本,这个版本令人印象深刻...
解读Java静态代码块和构造代码块 一、对静态代码块和非静态代码块进行简单的总结 1.静态代码块: 静态代码块是在当类被载入(内存)时最先被调用的,且之被执行一次,静态块常用来执行类属性的初始化。执行 类 的载入之前就会调用。 2.非静态代码块: 非静态代码块是在当类的对象被创建载入(内存)时最先被调用...