简体   繁体   English

如何使用Java连接到远程桌面上的MS SQL Server(需要用户登录名和密码)?

[英]How to connect to ms sql server on remote desktop( requires user login and password ) using java?

I'am making a tool which requires me to connect ms sql server on remote desktop connection . 我正在制作一个工具,要求我在远程桌面连接上连接ms sql服务器。 I have tried the establishing the connection but got stuck in figuring out the code for how to access remote desktop with user id and password . 我曾尝试建立连接,但陷入了如何使用用户ID和密码访问远程桌面的代码中。 sample code for that particular part would very helpful 该特定部分的示例代码将非常有帮助

As mentioned Remote Desktop is a client application which you use to connect to the Windows Server on which your SQL Server resides. 如前所述,远程桌面是一个客户端应用程序,可用于连接到SQL Server所在的Windows Server。 You do not need this in your code. 您的代码中不需要此。

The following code will connect you to a SQL Server: 以下代码将您连接到SQL Server:

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connection = DriverManager.getConnection("jdbc:sqlserver://hostname:1433","username", "password");

And if you want to query the SQL Server you can continue with: 如果要查询SQL Server,可以继续执行以下操作:

statement = connection.createStatement();
statement.executeQuery("select * from my_table");

I recommend you read some tutorials on JDBC such as these 我建议你读JDBC一些教程,如这些

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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