gpt4 book ai didi

Java 连接到 SqlServer 数据库

转载 作者:太空宇宙 更新时间:2023-11-04 10:01:49 25 4
gpt4 key购买 nike

我尝试使用 Eclipse 连接到 SQL Server 数据库,但出现错误:

Cannot open database "AdventureWorks2012" requested by the login. The login failed. 

最佳答案

由于您使用的是integratedSecurity=true,这意味着您的Windows帐户不允许访问AdventureWorks2012数据库。您需要授予您登录(或运行Eclipse)的Windows帐户数据库访问权限。您可以关注To configure database access授予您的 Windows 帐户访问权限的文档:

  1. On the computer that is running SQL Server, start SQL Server Management Studio. In the Registered Servers pane, double-click SQL Server.

  2. In the object explorer pane, expand SQL Server, expand the Security folder, right-click Logins, and then click New Login.

  3. In the Login – New dialog box, specify either Windows Authentication or SQL Server Authentication mode.

  4. If you are using Windows Authentication, enter a logon name and select either the Grant Access or the Deny Access option.

  5. If you are using SQL Server Authentication, type a logon name and password, and then confirm the password.

  6. In the left pane, click Database Access.

  7. In the right pane, select the Permit check box for the databases you are granting access to, and then click OK.

如果您想通过指定用户名和密码进行连接,请使用 integratedSecurity=false 并提供用户名和密码,如 Establishing a Connection 中所示。文档:

String jdbcUrl = "jdbc:sqlserver:...";
Properties props = new Properties();
props.put("user", <database username>);
props.put("password", <database password>);
try (Connection conn = DriverManager.getConnection(jdbcUrl, props)) {
...
}

关于Java 连接到 SqlServer 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53365224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com