将SQL Server 2008连接到Java:登录失败,表示用户错误 [英] Connecting SQL Server 2008 to Java: Login failed for user error

查看:228
本文介绍了将SQL Server 2008连接到Java:登录失败,表示用户错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的Java代码连接到Microsoft SQL Server 2008 R2 Express数据库。我已下载Microsoft SQL Server JDBC驱动程序3.0并将sqljdbc4.jar添加到我的类路径中。我正在使用Netbeans,并且在我的项目中也包含了sqljdbc4.jar。

I am trying to connect my Java code to a Microsoft SQL Server 2008 R2 Express database. I have downloaded the Microsoft SQL Server JDBC Driver 3.0 and added the sqljdbc4.jar to my classpath. I am using Netbeans and have included the sqljdbc4.jar in my project also.

我在SQL Server Management Studio中创建了一个名为TestDB1的数据库,并添加了一些列和值我将用于测试。我通过右键单击服务器JACOB = PC \SQLEXPRESS-> Properties-> Secuity并从Windows身份验证模式更改为SQL Server和Windows身份验证模式,从Windows身份验证模式更改。

I created a database in the SQL Server Management Studio called TestDB1 and added some columns and values that I will use for testing. I changed from Windows Authentication Mode by right clicking on the server JACOB=PC\SQLEXPRESS->Properties->Secuity and changing from Windows Authentication Mode to SQL Server and Windows Authentication Mode.

然后我通过右键单击JACOB-PC / SQLEXPRESS-> Secuity Folder-> Logins Folder下的窗口浏览器中的Login文件夹创建了一个新的登录,并添加了一个新的登录。我给它命名为jhaip2,切换到SQL Server身份验证并将密码设置为jacob。未选中强制执行密码策略和强制密码过期。默认数据库设置为TestDB1。然后在TestDB1-> Secuity-> Users-> jhaip2->数据库角色成员身份我将jhaip2设置为db_owner(我无法在没有这样做的情况下登录管理工作室中的数据库,可能不是正确的做法?)。然后我重新启动了服务器。

I then created a new login by right clicking on the Login folder in the window explorer under JACOB-PC/SQLEXPRESS->Secuity Folder->Logins Folder and added a new login. I gave it the name jhaip2, switched to SQL Server authentication and the set the password to jacob. Enforce password policy and enforce password expiration are unchecked. The default database is set to TestDB1. Then under TestDB1->Secuity->Users->jhaip2->Database role membership I set jhaip2 to db_owner (I couldn't log in to the database in the management studio without doing this, probably not the right thing to do?). I then restarted the server.

现在我的java代码,它基本上是JDBC Driver 3.0示例代码的直接副本,除了没有Windows身份验证。

Now for my java code, it is basically a direct copy of the JDBC Driver 3.0 Sample code except without windows authentication.

package databasetest1;

import java.sql.*;

public class connectURL {

   public static void main(String[] args) {

      // Create a variable for the connection string.
      String connectionUrl = "jdbc:sqlserver://localhost:1433;" + "databaseName=TestDB1;";

      // Declare the JDBC objects.
      Connection con = null;
      Statement stmt = null;
      ResultSet rs = null;

      try {
         // Establish the connection.
         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
         System.out.println("Driver okay");
         con = DriverManager.getConnection(connectionUrl,"jhaip2","jacob");
         System.out.println("Connection Made");

      }

      // Handle any errors that may have occurred.
      catch (Exception e) {
         e.printStackTrace();
      }
      finally {
         if (rs != null) try { rs.close(); } catch(Exception e) {}
         if (stmt != null) try { stmt.close(); } catch(Exception e) {}
         if (con != null) try { con.close(); } catch(Exception e) {}
      }
   }
}

当我运行时,它打印出Driver okay,所以我假设我的驱动程序设置正确。然后它打印错误:

When I run, it prints out "Driver okay" so I am assuming my driver is set up correctly. Then it prints the error:

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'jhaip2'.

我使用的用户名无关紧要,总是失败。我有一种感觉,我设置用户错了。如果有人能给我一些关于如何正确设置用户的帮助或者如何简单地用Java连接到SQL Server数据库的任何指导,我将不胜感激。

It does not matter what username I use, it always fails. I have a feeling I am setting up the user wrong. If anyone could give me some help on how to properly set up a user or any guidance in how to simply connect to a SQL Server database in Java, I would appreciate it.

推荐答案

当我查看SQL Server日志文件时,它说我无法登录,因为SQL Server处于Windows身份验证模式,即使在程序中它设置为混合验证模式。

When I looked in the SQL Server log files, it was saying that I couldn't log in because SQL Server was in Windows Authentication mode, even though in the program it was set to Mixed Authentication Mode.

安装时,我已将其设置为Windows身份验证模式,但更改程序中的设置不会将其从Windows身份验证模式更改。

When installing, I had set it up in Windows Authentication mode but changing the settings in the program would not change it from Windows Authentication mode.

我从我的计算机上删除了SQL Server 2008和所有相关程序,并安装了一份新的副本,这次是使用SQL Server身份验证。现在一切正常。我不知道为什么SQL Server在更改身份验证模式时出现问题,但它现在可以正常工作,所以我很高兴。

I removed SQL Server 2008 and all related programs from my computer and installed a fresh copy, this time with SQL Server Authentication. Everything works correctly now. I don't know why SQL Server had a problem with changing the authentication mode, but it works now so I am happy.

这篇关于将SQL Server 2008连接到Java:登录失败,表示用户错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆