使用JDBC驱动程序连接Sql Server 2008

时间:2010-04-27 12:15:02

标签: sql-server jdbc

我正在使用以下代码连接Sql Server 2008:

Connection con = null;
CallableStatement stmt = null;
ResultSet rs = null;

try
{
    SQLServerDataSource ds = new SQLServerDataSource();
    ds.setIntegratedSecurity(false);
    ds.setServerName("localhost");
    ds.setInstanceName("MSSQLSERVER2008");
    ds.setPortNumber(1433);  
    ds.setUser("televic-edu3-dev");
    ds.setPassword("internal");
    ds.setDatabaseName("televic-edu3-dev");
    con = ds.getConnection();
    ...

它给了我以下错误:

  

用户登录失败   'Televic的-edu3-dev的'。用户不是   与受信任的SQL Server关联   连接。

在我的SqlServer实例上启用了混合模式。我已经尝试使用相同的凭据连接到我的SqlServer实例,这是有效的。在.NET中,它确实与具有相同凭据的connectionString一起工作......那么我做错了什么?

这是来自.NET的connectionString:

  

TLV-EDU-LIC \ MSSQLSERVER2008;密码=内部;坚持   安全信息=真;用户   ID = Televic的-edu3-dev的;初始   目录= Televic的-edu3-dev的

顺便说一下,我也尝试了这个,这给了我同样的错误(这是合乎逻辑的):

Connection con = null;
Statement stmt = null;
ResultSet rs = null;

try
{
    String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
    "instanceName=MSSQLSERVER2008;databaseName=televic-edu3-dev;
        userName=televic-edu3-dev;password=internal;";

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    con = DriverManager.getConnection(connectionUrl);

2 个答案:

答案 0 :(得分:1)

好吧,省略ds.setPortNumber(1433);使它工作。不知道为什么...我的sqlserver实例正在端口1433上运行。

答案 1 :(得分:0)

您正在使用域(Windows用户名)身份验证而不是数据库身份验证。确保JVM使用相同的Windows用户名运行,因此不能作为系统服务运行。如果这不是一个选项,请改用DB身份验证。

相关问题