简体   繁体   中英

how to know client ip port from jdbc connection

I am testing about MSSQL. "how to long session information created after after connection?"

So, try to connection with MSSQL JDBC, then select the sys.dm_exec_session and sys.dm_exec_connection with client Ip and port.

But, I don't have any idea, how to get Client Ip and port from JDBC connection.

or is it possible create JDBC connection using socket?

show my test code.

try {
        DriverManager.registerDriver(
                new com.microsoft.sqlserver.jdbc.SQLServerDriver());
        DriverManager.setLoginTimeout(10);
    } catch (Exception ex) {
        System.out.println("Fail to register sqlserver driver.");
        return;
    }

    String url = "jdbc:sqlserver://";
    String ip = "10.1.1.1";
    String port = "1433";
    String catalog = "master";
    String user = "jtlee";
    String pwd = "123456789";

    url = url + ip + ":" + port + ";databasename=" + catalog;

    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;

    try {
        long startTime = System.nanoTime();

        conn = DriverManager.getConnection(url, user, pwd);
        pstmt = conn.prepareStatement(
                "SELECT "
                + "     count(*) "
                + "FROM "
                + "     sys.dm_exec_connections connections,"
                + "     sys.dm_exec_sessions sessions "
                + "WHERE "
                + "     connections.session_id = sessions.session_id "
                + "     and connections.client_net_address= ? "
                + "     and connections.client_tcp_port= ? ");

        /* I need this information... */
        String clientIp;
        int clientPort;

        pstmt.setString(1, clientIp);
        pstmt.setInt(2, clientPort);
        rs = pstmt.executeQuery();

        if (rs.next()) {
            ...

thanks for reading my strange English.

If you want to get the IP and port for an active connection you can just query sys.dm_exec_connections with WHERE session_id=@@SPID , eg,

sql = 
        "SELECT client_net_address, client_tcp_port, local_net_address, local_tcp_port " + 
        "FROM sys.dm_exec_connections " + 
        "WHERE session_id=@@SPID";
try (
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery(sql)) {
    while (rs.next()) {
        System.out.printf("Connection is between %s:%d on the server and %s:%d as the client.%n", 
                rs.getString("local_net_address"),
                rs.getInt("local_tcp_port"),
                rs.getString("client_net_address"),
                rs.getInt("client_tcp_port")
                );
    }
}

producing

Connection is between 192.168.1.12:1433 on the server and 192.168.1.101:49744 as the client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM