简体   繁体   English

Java Eclipse SQL Server连接

[英]Java Eclipse SQL Server Connection

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. 尝试建立连接后,我收到以下SQLServer异常:服务器主体“ SQLServer”在当前安全上下文下无法访问数据库“ TMO”。

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 我在Windows Server 2008 R2 OS计算机上使用SQLServer 2008R2

update your db_connect_string with below: 使用以下命令更新您的db_connect_string:

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. 我建议您添加finally块并添加代码以关闭连接对象。

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

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