简体   繁体   English

为什么即使与数据库的连接正常,查询也不起作用?

[英]Why queries won't work even though connection with database is ok?

I created database in SQL Server Management Studio.我在 SQL Server Management Studio 中创建了数据库。 My connection with database is ok, but query I try to execute is causing problems.我与数据库的连接正常,但我尝试执行的查询导致问题。 It throws me "Invalid object name 'tw__towar'".它向我抛出“无效的 object 名称 'tw__towar'”。 Even though the query is working fine when I execute it in SQL Server Management Studio.即使当我在 SQL Server Management Studio 中执行查询时查询工作正常。

Here is code fragment responsible for connection.这是负责连接的代码片段。

try {
        Class.forName(ssmsDriver);
        connection = DriverManager.getConnection(url, userName, userPassword);
        Log.d(TAG, "onCreate: connected to the database");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        Log.e(TAG, "onCreate: class not found", e);
    } catch (SQLException e) {
        e.printStackTrace();
        Log.e(TAG, "onCreate: sql exception", e);
    }

And here is code responsible for executing query.这是负责执行查询的代码。 Double underscore intended.双下划线的意思。

try {
                statement = connection.createStatement();
                ResultSet resultSet = statement.executeQuery("SELECT tw_nazwa FROM tw__towar" +
                        " WHERE tw_ean='5903206038608';");
                mItemNameTextView.setText("SUCCESS");   // DELETE THIS LATER

            } catch (SQLException e) {
                e.printStackTrace();
                mItemNameTextView.setText(e.getMessage());  // DELETE THIS LATER
            }

Does anybody have any idea what may be causing problem?有人知道可能导致问题的原因吗?

I managed to solve this problem, so I'll post answer here for anyone that would have the same issue in the future.我设法解决了这个问题,所以我会在这里为将来遇到同样问题的任何人发布答案。

The problem was that I tried to operate on the sa account (system administrator).问题是我试图对sa帐户(系统管理员)进行操作。 I could connect to database but it cut off my access to tables.我可以连接到数据库,但它切断了我对表的访问。

To solve this problem you need to create new login by:要解决此问题,您需要通过以下方式创建新登录:

  • right-click on "Security" folder in object explorer右键单击 object 资源管理器中的“安全”文件夹
  • select New -> Login select 新 -> 登录
  • enter your new login输入您的新登录名
  • check "SQL Server authentication" and set your password检查“SQL Server 身份验证”并设置您的密码
  • I unchecked "Enforce password expiration"我取消选中“强制密码过期”
  • Go to "Server Roles" which is on the left side of this window and check "dbcreator" Go 到此 window 左侧的“服务器角色”并检查“dbcreator”
  • Click OK, and restart program.单击确定,然后重新启动程序。
  • Login with new credentials and check if everything works.使用新凭据登录并检查是否一切正常。

I hope it will help someone in the future.我希望它会在未来对某人有所帮助。

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

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