2011-06-13 247 views
1

我正嘗試使用Jasper報告生成一些報告給我的系統。我起訴基本JDBC連接方法來建立我的SQL Server 2000中的連接下面是我的代碼片段:JDBC | SQL Server連接錯誤

public static Connection getConnection()throws ClassNotFoundException, SQLException 
{ 
    Class.forName(driver); 
    Connection conn = DriverManager.getConnection(connectString, user, password); 
    return conn; 
} 

但我發現了一個SQL語法不正確異常。

Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Line 2: Incorrect syntax near '|'. 
    at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source) 
    at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source) 
    at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source) 
    at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source) 
    at com.microsoft.jdbc.sqlserver.tds.TDSExecuteRequest.processReplyToken(Unknown Source) 
    at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source) 
    at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source) 
    at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source) 
    at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source) 
    at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source) 
    at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source) 
    at com.microsoft.jdbc.base.BaseStatement.executeQueryInternal(Unknown Source) 
    at com.microsoft.jdbc.base.BasePreparedStatement.executeQuery(Unknown Source) 
    at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:478) 

謝謝。

+2

什麼是connectString? – Dave 2011-06-13 09:31:25

+1

我認爲您的摘錄之上的那一行會很有趣,因爲它是指示實際發生的異常的地方。 – 2011-06-13 09:37:01

回答

1

你得到的異常的第一行說:

​​

檢查,看看你有「|」在你的來源。如有疑問請試試這些:

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433", user, password); 

確保您的罐子放在正確的地方。

+0

我同意。我認爲海報有錯誤的連接字符串 – MJB 2011-06-14 07:35:30