0

I am getting the following SQLServer exception after trying to establish connection: The server principal "SQLServer" is not able to access the database "TMO" under the current security context.

Here's my code:

public java.sql.ResultSet dbConnect(String db_connect_string,
        String db_userid,
        String db_password,
        String query)
  {
  try {
     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
     Connection conn = DriverManager.getConnection(db_connect_string,
              db_userid, db_password);
     System.out.println("connected");
     Statement statement = conn.createStatement();
     String queryString = query;
     ResultSet rs = statement.executeQuery(queryString);
     /*while (rs.next()) {
        System.out.println(rs.getString(1));
     }*/
     return rs;
  } catch (Exception e) {
     e.printStackTrace();
     return null;
  }

and this is where I call this class:

String query = "select TOP 45000 MEMO_MANUAL_TXT,RAND() from TMO.dbo.merged_memo where       MEMO_MANUAL_TXT <> 'CCIH_NULL' and MEMO_MANUAL_TXT not like '%QuikView%' order by RAND()";
    ConnectMSSQLServer connServer = new ConnectMSSQLServer();
    java.sql.ResultSet m_ResultSet = connServer.dbConnect("jdbc:sqlserver://localhost", "SQLServer",
               "****", query);

I am using SQLServer 2008R2 on a Windows Server 2008 R2 OS machine

user4045430
  • 207
  • 1
  • 6
  • 13

1 Answers1

0

update your db_connect_string with below:

jdbc:sqlserver://localhost:<port>;instance=<sql_server_instance_name>;databaseName=<your_database_name>;integratedSecurity=false;

and where are you closing your database connection ?? I suggest you to add finally block and add the code to close the connection object.

Prasad Khode
  • 6,602
  • 11
  • 44
  • 59
  • It doesn't work, there's an error message saying "This driver is not configured for integrated authentication". – user4045430 Nov 17 '14 at 12:08
  • Also I get a warning message "Failed to load the sqljdbc_auth.dll" – user4045430 Nov 17 '14 at 12:08
  • integreatedSecurity value should be false, I have updated the answer. Test once with this change – Prasad Khode Nov 17 '14 at 12:25
  • I'm not clear why integreatedSecurity should be set to false (in that case a user/pss should be supplied, no?). Anyway, I tested this change and get 'Login failed for user '' – user4045430 Nov 17 '14 at 12:39
  • check [this](http://stackoverflow.com/questions/1229691/difference-between-integrated-security-true-and-integrated-security-sspi) for more details on integrated security in sql sever – Prasad Khode Nov 17 '14 at 12:54