简体   繁体   English

Java连接到SqlServer数据库

[英]Java Connect To SqlServer Database

我正在尝试使用Eclipse连接到SQL Server数据库,但这给了我错误:

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

Since you are using integratedSecurity=true it means that you windows account is not allowed to access AdventureWorks2012 database. 由于您正在使用integratedSecurity=true因此这意味着您的Windows帐户不允许访问AdventureWorks2012数据库。 You need to grant the Windows account you are logged in (or running Eclipse) the database access rights. 您需要向登录(或运行Eclipse)的Windows帐户授予数据库访问权限。 You can follow To configure database access docs to grant your Windows account access: 您可以按照配置数据库访问文档来授予Windows帐户访问权限:

  1. On the computer that is running SQL Server, start SQL Server Management Studio. 在运行SQL Server的计算机上,启动SQL Server Management Studio。 In the Registered Servers pane, double-click SQL Server. 在“注册的服务器”窗格中,双击“ SQL Server”。

  2. In the object explorer pane, expand SQL Server, expand the Security folder, right-click Logins, and then click New Login. 在对象资源管理器窗格中,展开“ SQL Server”,展开“安全性”文件夹,右键单击“登录名”,然后单击“新建登录名”。

  3. In the Login – New dialog box, specify either Windows Authentication or SQL Server Authentication mode. 在“登录-新建”对话框中,指定Windows身份验证或SQL Server身份验证模式。

  4. If you are using Windows Authentication, enter a logon name and select either the Grant Access or the Deny Access option. 如果使用Windows身份验证,请输入登录名,然后选择“授予访问权限”或“拒绝访问”选项。

  5. If you are using SQL Server Authentication, type a logon name and password, and then confirm the password. 如果使用的是SQL Server身份验证,请键入登录名和密码,然后确认密码。

  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. 在右窗格中,选择要授予访问权限的数据库的“允许”复选框,然后单击“确定”。

If you want to connect by specifying username and password use integratedSecurity=false and supply username and password as shown in the Establishing a Connection docs: 如果要通过指定用户名和密码进行连接,请使用integratedSecurity=false并提供用户名和密码,如建立连接文档中所示:

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)) {
  ...
}

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

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