开发者

Using JDBC driver to connect with Sql Server 2008

I'm using following code to connect with 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();
    ...

It gives me following error:

Login failed for user 'televic-edu3-dev'. The user is not associated with a trusted SQL Server connection.

Mixed mode is enabled on my SqlServer instance. I already tried connecting to my SqlServer instance with the same credentials, which works. In .NET, it does work with a connectionString which has the same credentials... So what am I doing wrong?

This is the connectionString from .N开发者_如何学CET:

TLV-EDU-LIC\MSSQLSERVER2008;Password=internal;Persist Security Info=True;User ID=televic-edu3-dev;Initial Catalog=televic-edu3-dev

I also tried this by the way, which gives me the same error (which is logical):

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);


Well, omitting the ds.setPortNumber(1433); makes it work. Don't know why... my sqlserver instance is running on port 1433.


You're using domain (Windows username) authentication rather than DB authentication. Ensure that the JVM is running with the same Windows username and thus not as a system service. If that's not an option, use DB authentication instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜