【问题标题】:pgjdbc-ng Ill-formed region:pgjdbc-ng 格式错误的区域:
【发布时间】:2019-02-25 09:55:09
【问题描述】:

目前我正在尝试制作一个模块,该模块将通过 Postgres 上的触发器监听任何更改。我正在使用 pgjdbc-ng ver 0.8.2,从 maven repo central 下载 JAR 并将其添加为项目参考。

以下是我使用的代码:

public class ListenNotify
{
// Create the queue that will be shared by the producer and consumer
private BlockingQueue queue = new ArrayBlockingQueue(10);

    // Database connection
    PGConnection connection;

    public ListenNotify()
    {
        // Get database info from environment variables
        /*
        String DBHost = System.getenv("DBHost"); 
        String DBName = System.getenv("DBName");
        String DBUserName = System.getenv("DBUserName");
        String DBPassword = System.getenv("DBPassword");
        */
        String DBHost = "127.0.0.1"; 
        String DBName = "dbname";
        String DBUserName = "postgres";
        String DBPassword = "postgres";
        // Create the listener callback
        PGNotificationListener listener = new PGNotificationListener()
        {
            @Override
            public void notification(int processId, String channelName, String payload)
            {
            // Add event and payload to the queue
            queue.add("/channels/" + channelName + " " + payload);
            }
        };

        try
        {
            // Create a data source for logging into the db
            PGDataSource dataSource = new PGDataSource();
            dataSource.setHost(DBHost);
            dataSource.setPort(5432);
            dataSource.setDatabaseName(DBName);
            dataSource.setUser(DBUserName);
            dataSource.setPassword(DBPassword);

            // Log into the db
            connection = (PGConnection) dataSource.getConnection();

            // add the callback listener created earlier to the connection
            connection.addNotificationListener(listener);

            // Tell Postgres to send NOTIFY q_event to our connection and listener
            Statement statement = connection.createStatement();
            statement.execute("LISTEN q_event");
            statement.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
    * @return shared queue
    */
    public BlockingQueue getQueue()
    {
        return queue;
    }

    /**
    *
    * main entry point
    *
    * @param args
    */
    public static void main(String[] args)
    {
    // Create a new listener
        ListenNotify ln = new ListenNotify();

        // Get the shared queue
        BlockingQueue queue = ln.getQueue();

        // Loop forever pulling messages off the queue
        while (true)
        {
            try
            {
                // queue blocks until something is placed on it
                String msg = queue.take().toString();

                // Do something with the event
                System.out.println(msg);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
}

运行时出现异常:

结构不正确的区域:印度尼西亚 [在索引 0]

我看过官方的git,说应该在某个版本号内修复。

我如何应用这些修复?

谢谢

【问题讨论】:

    标签: postgresql pg-jdbc


    【解决方案1】:

    我知道有点晚了;)

    我遇到了同样的问题,并且读到问题已解决。但似乎并非如此。 无论如何,问题是在创建 postgres 数据库时,LC_COLLATE 可能设置为 Indonesia_Indonesia.1252。尝试建立连接时,会将此值与 java 语言环境进行比较。在 Java Locales 类中,该值可能是您的语言,因此无法找到该条目。但是,要解决此问题,您可以将 Java 语言环境的默认值设置为英语。这当然不是解决问题的最佳方法,但它确实有效。为了安全起见,我会在建立连接后放回去

    您可以按如下方式设置默认值:

    Locale.setDefault(Locale.ENGLISH)

    【讨论】:

      猜你喜欢
      • 2021-11-16
      • 2019-10-28
      • 2022-12-23
      • 1970-01-01
      • 1970-01-01
      • 2015-06-28
      • 2020-10-27
      • 1970-01-01
      • 2023-03-22
      相关资源
      最近更新 更多