Java SQL Server 2012 无法打开数据库错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23974017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 09:28:31  来源:igfitidea点击:

Java SQL Server 2012 Cannot open database error

javasqlsql-servertcp

提问by user1397978

I seem to be running into the error: Cannot open database requested by the login. The login failed.when connecting my SQL Server database to a Java application written in netbeans.

我似乎Cannot open database requested by the login. The login failed.遇到了错误:将我的 SQL Server 数据库连接到用 netbeans 编写的 Java 应用程序时。

I have read numerous articles and done the following:

我阅读了大量文章并完成了以下操作:

  • Allowed the 1433 port to the firewall under UDP and TCP

  • Set SQL Server Configuration Manager TCP/IP ports to 1433

  • Changed the server authentication in SQL Server Management Studio to mixed authentication

  • Allowed remote connections in SQL Server Management Studio

  • 在UDP和TCP下允许1433端口到防火墙

  • 将 SQL Server 配置管理器 TCP/IP 端口设置为 1433

  • 将 SQL Server Management Studio 中的服务器身份验证更改为混合身份验证

  • SQL Server Management Studio 中允许的远程连接

My connection string is as follows:

我的连接字符串如下:

jdbc:sqlserver://localhost\SQLEXPRESS:1433;DatabaseName = SASS; IntegratedSecurity=true; Trusted_Connection=true; 

and I am still getting the following error:

我仍然收到以下错误:

Cannot open database "SASS" requested by the login. The login failed. ClientConnectionId:894b97b9-54d6-47cc-afd7-1473a460c09d

After reading for 3 days straight, I am at my wits end.

连续读了 3 天后,我的心智已尽。

采纳答案by user1397978

I seemed to have figured out what was going on. I had changed my pc's settings and updated so many things, I must've broken something along the way.

我似乎已经明白是怎么回事了。我改变了我的电脑设置并更新了很多东西,我一定是在这个过程中弄坏了一些东西。

Below are a set of instructions that I've worked out in the event that anyone else has problems with Java connections. This was done on a newly installed machine:

以下是我在其他人遇到 Java 连接问题时制定的一组说明。这是在新安装的机器上完成的:

Connecting a SQL Server Database (2008 or 2012) to a Java Netbeans Application:

将 SQL Server 数据库(2008 或 2012)连接到 Java Netbeans 应用程序:

  1. Install SQL Server Management Studio 2008 or 2012
  2. Install SQL Server a. Name the Server Instance SQLExpress/SQLEXPRESS b. Make sure that the server accepts mixed methods of authentication (SQL Server and Windows Authentication Mode) c. Give the System Administrator the password: admin
  3. Install the latest JRE and JDKs
  4. Install Netbeans
  5. Create a test database in SQL Server Management Studio to test later
  6. Update the firewall to accept the incoming and outgoing port 1434 for TCP AND UDP
  7. Open SQL Server Configuration Manager

    a. Stop both the SQL Server (SQLEXPRESS) and SQL Server Browser services

    b. Open the Protocols for SQLEXPRESS under SQL Server Network Configuration and enable TCP/IP

    c. Enable all ports (ESPECIALLY IPAll) and set the ports to 1434

    d. Enable the services again (especially the SQL Server Browser)

  8. Unzip sqljdbc_4.0.2206.100_enu to C:\ drive
  9. Copy and paste the DLL file from C:\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\auth\x86 or C:\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\auth\x64 (depending on whether you're running a 64bit Java installation or 32bit – DO NOT COPY BOTH).
  10. Copy and paste the DLL file into C:\Program Files\Java\jdk1.8.0_05\bin
  11. Add the sqljdbc driver to your Java Netbeans application

    a. Add the sqljdbc driver to the application library (right click ? add)

    b. Under the Services tab, expand “Databases” and right click on “Drivers”

    c. Add the sqljdbc4 driver to the service (Right click ? New Driver…)

    d. Make sure the connection string is similar to: jdbc:sqlserver://localhost\SQLEXPRESS; database=[Database Name]; integratedSecurity = true; where localhost\SQLEXPRESS is changed to the server you are running your database from

  1. 安装 SQL Server Management Studio 2008 或 2012
  2. 安装 SQL Server 将服务器实例命名为 SQLExpress/SQLEXPRESS b。确保服务器接受混合身份验证方法(SQL Server 和 Windows 身份验证模式) c. 给系统管理员密码:admin
  3. 安装最新的 JRE 和 JDK
  4. 安装 Netbeans
  5. 在 SQL Server Management Studio 中创建一个测试数据库以供稍后测试
  6. 更新防火墙以接受 TCP 和 UDP 的传入和传出端口 1434
  7. 打开 SQL Server 配置管理器

    一种。停止 SQL Server (SQLEXPRESS) 和 SQL Server Browser 服务

    湾 在 SQL Server 网络配置下打开 SQLEXPRESS 的协议并启用 TCP/IP

    C。启用所有端口(特别是 IPAll)并将端口设置为 1434

    d. 再次启用服务(尤其是 SQL Server 浏览器)

  8. 将 sqljdbc_4.0.2206.100_enu 解压到 C:\ 盘
  9. 从 C:\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\auth\x86 或 C:\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\auth\x64(取决于关于您运行的是 64 位 Java 安装还是 32 位 - 不要同时复制两者)。
  10. 将 DLL 文件复制并粘贴到 C:\Program Files\Java\jdk1.8.0_05\bin
  11. 将 sqljdbc 驱动程序添加到 Java Netbeans 应用程序

    一种。将 sqljdbc 驱动程序添加到应用程序库中(右键单击?添加)

    湾 在“服务”选项卡下,展开“数据库”并右键单击“驱动程序”

    C。将 sqljdbc4 驱动程序添加到服务中(右键单击?新建驱动程序...)

    d. 确保连接字符串类似于: jdbc:sqlserver://localhost\SQLEXPRESS; 数据库=[数据库名称]; 集成安全=真;其中 localhost\SQLEXPRESS 更改为您运行数据库的服务器

回答by Matthew at Critical Cognition

Your connection string indicates that you are using Integrated Security over a Trusted Connection. This means that whatever user that is running your process that calls the database must have access to SQL Server and your individual database. I recommend that you use mixed authentication in SQL with a named user. Read more about it on MSDN SQL Server 2012 documentation:

您的连接字符串表明您正在通过可信连接使用集成安全性。这意味着运行调用数据库的进程的任何用户都必须有权访问 SQL Server 和您的个人数据库。我建议您在 SQL 中对指定用户使用混合身份验证。在 MSDN SQL Server 2012 文档中阅读更多相关信息:

Create a Login

创建登录

Create a Database User

创建数据库用户

Then you can create a connection string like this:

然后你可以像这样创建一个连接字符串:

Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;Password=myPassword;

SQL Server Connection Strings

SQL Server 连接字符串