我有一个 Java 应用程序,我使用 "com.mysql.jdbc.Driver"
连接到 mySQL Server。
建立连接后,我需要Client本地端口。这意味着 Java 应用程序从 MySQL 服务器接收数据的端口。
有没有办法在我建立连接后在我自己的源代码中接收它?
我尝试了java.sql.DatabaseMetaData
中的所有信息。
最佳答案
对于MySQL Connector/J there's sources on github .
实际连接到MySQL Server的Socket在MysqlIO
中定义为
public Socket mysqlConnection = null;
ConnectionImpl
类包含一个 getter:
public MysqlIO getIO() throws SQLException
因此,如果您设法将 Connection
转换为 ConnectionImpl
(尚未检查),您可以:
ConnectionImpl impl = (ConnectionImpl)con;
Socket sock = impl.getIO().mysqlConnection;
int localPort = sock.getLocalPort();
祝你好运。
关于java - 如何从 MySQL JDBC 驱动程序获取客户端本地套接字端口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34402697/