无法使用 Java 访问 SQL Server 中的一个数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32882975/
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-11-02 20:55:42  来源:igfitidea点击:

Not able to access one database in SQL Server using Java

javasql-server-2012

提问by sachinb

I have an SQL Server 2012 edition. I have two databases on this server. One of these databases named BANKTEST was restored from a backup a few months ago. I have recently restored another database named NEWBANK.

我有一个 SQL Server 2012 版。我在这台服务器上有两个数据库。其中一个名为 BANKTEST 的数据库是几个月前从备份中恢复的。我最近恢复了另一个名为 NEWBANK 的数据库。

The following is the java program which I had written to access NEWBANK

以下是我编写的用于访问 NEWBANK 的 java 程序

package com.example.myproject.client;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ConnectMSSQL {

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
        Connection conn = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1;user=sa;password=sa123;Database=NEWBANk");
        System.out.println("test");
        Statement sta = conn.createStatement();
        String Sql = "select TOP 10 CodeType from D001002";
        ResultSet rs = sta.executeQuery(Sql);
        while (rs.next()) {
            System.out.println(rs.getString("CodeType"));
        }
    }
}

Unfortunately, I am getting an error which I have listed below

不幸的是,我收到了下面列出的错误

> Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: Cannot open database "NEWBANK" requested by the login. The login failed. ClientConnectionId:69c57dd7-38b1-413c-b8ea-8d53824f74e1
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
    at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:254)
    at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:84)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2908)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2234)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.access##代码##0(SQLServerConnection.java:41)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at com.example.myproject.client.ConnectMSSQL.main(ConnectMSSQL.java:13)

If I replace the name of the database with BANKTEST, I do not get any errors. Can someone advice me on this?

如果我用 BANKTEST 替换数据库的名称,则不会出现任何错误。有人可以就此给我建议吗?

Regards

问候

Sachin

萨钦

回答by Rahul Tripathi

The error shows that the username and password which you are using to login to your database NEWBANKis not valid or does not have the permision to access it.

该错误表明您用于登录数据库的用户名和密码NEWBANK无效或无权访问它。

You need to check your login details with your DBA and then try to login again. You can also check if you can access the database using the SQL Server Management Studio using the login credentails you are using in code.(You will probably find the same error over there as well)

您需要与您的 DBA 检查您的登录详细信息,然后再次尝试登录。您还可以检查是否可以使用 SQL Server Management Studio 使用您在代码中使用的登录凭据访问数据库。(您可能也会在那里发现相同的错误

You can also follow the steps given here: SQL SERVER – FIX : ERROR : Cannot open database requested by the login.

您还可以按照此处给出的步骤操作:SQL SERVER – FIX : ERROR : 无法打开登录请求的数据库。

Go to SQL Server >> Security >> Logins and right click on NT AUTHORITY\NETWORK SERVICE and select Properties

转到 SQL Server >> Security >> Logins 并右键单击 NT AUTHORITY\NETWORK SERVICE 并选择属性

enter image description here

在此处输入图片说明

In newly opened screen of Login Properties, go to the “User Mapping” tab. Then, on the “User Mapping” tab, select the desired database – especially the database for which this error message is displayed. On the lower screen, check the role db_owner. Click OK.

在新打开的登录属性屏幕中,转到“用户映射”选项卡。然后,在“用户映射”选项卡上,选择所需的数据库——尤其是显示此错误消息的数据库。在下方屏幕上,检查角色 db_owner。单击确定。

enter image description here

在此处输入图片说明