1
我正在測試MSSQL。 「如何在連接後長時間創建會話信息?」如何從jdbc連接知道客戶端ip端口
因此,嘗試連接MSSQL JDBC,然後選擇與客戶端IP和端口的sys.dm_exec_session和sys.dm_exec_connection。
但是,我沒有任何想法,如何從JDBC連接獲取客戶端IP和端口。
或者是否有可能使用套接字創建JDBC連接?
顯示我的測試代碼。
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()) {
...
感謝您閱讀我奇怪的英語。