简体   繁体   中英

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

I created database in 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'". Even though the query is working fine when I execute it in 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). 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
  • select New -> Login
  • enter your new login
  • check "SQL Server authentication" and set your password
  • I unchecked "Enforce password expiration"
  • Go to "Server Roles" which is on the left side of this window and check "dbcreator"
  • Click OK, and restart program.
  • Login with new credentials and check if everything works.

I hope it will help someone in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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