JAVA调用IPV6 postgre数据库

//配置支持ipv6,这步不知道在哪里复制来的了。好像应该是java.net.preferIPv6Stack
        System.setProperty("java.net.preferIPv4Stack", "true");
// 定义连接信息4的方式jdbc:postgresql://10.11.12.13:5432/cmcs?currentSchema=cmcs_balance&useUnicode=true&autoReconnect=true
        String url = "jdbc:postgresql://[2409:8018:5a1d::13:0:AC]:5432/cmcs?currentSchema=cmcs_balance&useUnicode=true&autoReconnect=true";
        String username = "cmcs_balance";
        String password = "postgres@202oAa12345%";
        try {
            Class clazz = Class.forName("org.postgresql.Driver");
            clazz.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 建立连接
        try (Connection connection = DriverManager.getConnection(url, username, password)) {
            System.out.println("2连接成功!");
            // 加载PG数据库的JDBC驱动程序
            // 执行查询
            try  {
                Statement statement = connection.createStatement();
                //循环调用 取随机数
                for(int i=0;i<10000;i++){
                    int suiji = getRandomNum(0,1);
                    if(suiji>0){
                        ResultSet resultSet = statement.executeQuery("SELECT * FROM balance_ratio limit 1");
                        while (resultSet.next()) {
                            System.out.println(resultSet);
                            String flow_rt = resultSet.getString("flow_rt");
                            System.out.println("flow_rt: " + flow_rt );
                        }
                    }
                    try {
                        Thread.sleep(880);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("2i:"+i);
                }

            }catch (SQLException e) {
                System.err.println("sql执行失败: " + e.getMessage());
            }
        } catch (SQLException e) {
            System.err.println("连接失败: " + e.getMessage());
        }

你可能感兴趣的:(java,数据库,开发语言)