获取mysql本地端口

文章展示了如何创建一个继承自BasicHttpClientConnectionManager的自定义连接管理器,该管理器涉及DNS解析和连接信息记录。同时,文中还演示了如何使用JDBC连接到MySQL数据库,获取数据库连接并访问Socket属性。
摘要由CSDN通过智能技术生成
public class CustomHttpConnectionManager extends BasicHttpClientConnectionManager {


    private static Logger logger = LoggerFactory.getLogger(CustomHttpConnectionManager.class);

    private DnsResolver dnsResolver;

    private Info info;

    public CustomHttpConnectionManager(DnsResolver dnsResolver, Info info) {
        super(RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", SSLConnectionSocketFactory.getSocketFactory())
                .build(), (HttpConnectionFactory)null, (SchemePortResolver)null, dnsResolver);
        this.dnsResolver = dnsResolver;
        this.info =info;
    }

    public void connect(HttpClientConnection conn, HttpRoute route, int timeout, HttpContext context){
        InetAddress address = null;
        HttpHost host;
        if (route.getProxyHost() != null) {
            host = route.getProxyHost();
        } else {
            host = route.getTargetHost();
        }

        address = host.getAddress();
        if (address == null) {
            try {
                InetAddress[] addresses = this.dnsResolver.resolve(host.getHostName());
                if (addresses != null && addresses.length > 0) {

                }
            } catch (UnknownHostException var10) {
            }
        }
        try {
            super.connect(conn, route, timeout, context);
        } catch (IOException e) {
            logger.error("执行失败!");
        }
        String srcDestInfo = conn.toString();
        logger.info("srcDestInfo:{}",srcDestInfo);
        if (!srcDestInfo.isEmpty()) {
            String port = srcDestInfo.substring(srcDestInfo.indexOf(":")+1, srcDestInfo.indexOf("<"));
            String ip = srcDestInfo.substring(srcDestInfo.indexOf(">") + 1, srcDestInfo.lastIndexOf(":"));
            String destinationPort = srcDestInfo.substring(srcDestInfo.lastIndexOf(":")+1);
            info.setDestinationIp(ip);
            info.setDestinationPort(Integer.parseInt(destinationPort));
            info.setSourcePort(Integer.parseInt(port));
            logger.info("info:{}",info.toString());
        }

    }

     

String url = "jdbc:mysql://127.0.0.1:3306/xxxxx";
        // jdbc:mysql://  :表示规定好协议,按照jdbc的方式连接到mysql数据库
        // 127.0.0.1  :主机,也可以是localhost,表示连接ip地址
        // 3306  :表示监听3306端口
        // db01  :表示连接到db01数据库
// 将数据库的用户名、密码放在properties对象中
        Properties properties = new Properties();
        // 其中,“user”、“password”是规定好的,后面的value值根据实际填写即可。
        properties.setProperty("user", "root"); // 用户名
        properties.setProperty("password", "123456"); // 密码
// 建立连接
        ConnectionImpl connection =(ConnectionImpl)DriverManager.getConnection(url, properties) ;
        MysqlIO mysqlIO= connection.getIO();

        Class aclass=mysqlIO.getClass();
        Field[] fields=aclass.getDeclaredFields();
        for (Field field:fields){

            if (field.getName().equals("mysqlConnection"))
            {
                field.setAccessible(true);
                Socket mySQLConnection=(Socket)field.get(mysqlIO);
                System.out.println(mySQLConnection.getLocalPort());
            }

            System.out.println(field.getName());
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值