2017-09-27 21 views
0

我试图连接到一个名为“Shipworks1”的Sql Server数据库。 SQL Server的这种安装由一个称为Shipworks的发货软件安装。由于某种原因,我无法使身份验证正常工作。这里是我的代码:身份验证失败SQL Server JDBC Java 8

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package daily.sales.report; 

/** 
* 
* @author Line Computer 
*/ 

import java.sql.*; 

public class DailySalesReport { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 

      Connection con = null; 
      String conUrl = "jdbc:sqlserver://LINECOMPUTER\\SHIPWORKS; databaseName=ShipWorks1; user=sa; password=XXXXXXXXXXXXXXX;"; 

     try { 
      // ... 
     con = DriverManager.getConnection(conUrl); 
     // ... 
     } catch (Exception e) { e.printStackTrace(); } 
      finally { 
       if (con != null) try { con.close(); } catch(Exception e) {} 
      } 
    } 

} 

而且我的错误:

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'sa'. ClientConnectionId:916f2fe6-49c3-475f-bb09-f2ff14cd92c9 
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:217) 
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:279) 
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:99) 
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:4346) 
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:3160) 
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$100(SQLServerConnection.java:43) 
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:3123) 
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7505) 
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2445) 
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1981) 
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1628) 
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1459) 
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:773) 
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1168) 
at java.sql.DriverManager.getConnection(DriverManager.java:664) 
at java.sql.DriverManager.getConnection(DriverManager.java:270) 
at daily.sales.report.DailySalesReport.main(DailySalesReport.java:27) 
BUILD SUCCESSFUL (total time: 0 seconds) 

我可以连接到数据库在我的数据库管理器,像这样:

enter image description here

任何帮助,将不胜感激。谢谢!

+0

你尝试通过conUrl没有空间? :) –

回答

0

您应该决定使用哪种类型的身份验证:Windows身份验证或SQL身份验证。

也许这会为你工作:

String userName ="username"; 
String password ="password"; 

String url ="jdbc:sqlserver://myDB\\SQLServer;databaseName=name"; 

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
Connection conn = DriverManager.getConnection(url, userName, password); 

另外,就这个一看 - https://thusithamabotuwana.wordpress.com/2012/07/19/connecting-to-sql-server-from-java/