2016-09-28 77 views
-1

我很難用Java連接到SQL服務器。這裏是我的代碼:在Java中連接到SQL?

package com.C2S.java; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 

public class C2S 
    { 


     public static void main(String[] args) throws SQLException, ClassNotFoundException { 

      try 
      { 
       Class.forName("com.txfb.bai.jtds.jdbc.Driver"); 

       String userName = "USEVL01"; 
       String password = "safevL01"; 
       String url = "jdbc:jtds:sqlserver://TXS9316110MISC;instance=MISC;"; 

       Connection conn = DriverManager.getConnection(url, userName, password); 

       Statement sta = conn.createStatement(); 

       String sql = "select * from OpenQuery(LINKEDDBTP, 'SELECT B.*" 
         + " FROM (SELECT *" 
         + " FROM SFBCDBTP.TXPROD.SUB_AGREEMENT" 
         + " WHERE POL_SYMBOL IN (''HOL'', ''PA6'', ''TXC'')" 
         + " AND TRANS_TYPE = ''NB''" 
         + " AND SUB_AGRMNT_TYPE = ''PL''" 
         + " AND SUB_AGMNT_STATUS = ''A''" 
         + " AND TRANS_EFF_DATE > ''2015-12-31''" 
         + " AND TRANS_EFF_DATE < ''2016-01-16''" 
         + " AND AGREEMENT > 0" 
         + " ORDER BY AGREEMENT" 
         + " FETCH FIRST 300 ROWS ONLY) AS A, TXPROD.COVERAGE_RATES AS B" 
         + " WHERE A.AGREEMENT = B.AGREEMENT" 
         + " AND A.POL_SYMBOL = B.POL_SYMBOL" 
         + " AND A.SUB_AGREEMENT = B.SUB_AGREEMENT;');"; 

       ResultSet rs = sta.executeQuery(sql); 

       while (rs.next()) { 
        System.out.println(rs.getString("txt_title")); 
       } 


      } catch (Exception e) 
      { 
       e.printStackTrace(); 
       System.out.println("DB Error: " + e); 
      } 
    } 


} 

當我跑我得到這個錯誤的應用程序:

了java.lang.RuntimeException:沒有消息資源在com.txfb.bai發現消息屬性prop.servertype 。 jtds.jdbc.Messages.get(Messages.java:120) at com.txfb.bai.jtds.jdbc.Messages.get(Messages.java:67) at com.txfb.bai.jtds.jdbc.Driver。 parseURL(Driver.java:374) at com.txfb.bai.jtds.jdbc.Driver.setupConnectProperties(Driver.java:239) at com.txfb.bai.jtds.jdbc.Driver.connect(Driver.java: 182) at com。 txfb.bai.jtds.jdbc.Driver.connect(Driver.java:1) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at com.C2S。數據庫錯誤:java.lang.RuntimeException:找不到消息屬性的消息資源prop.servertype

有人能告訴我我做錯了什麼嗎?

+0

嗯,這個例外是由於'prop.servertype'缺少消息資源,但是底層的問題可能是URL中的錯誤。 – Kayaman

+0

'jtds' Driver的類名是'net.sourceforge.jtds.jdbc.Driver' http://jtds.sourceforge.net/faq.html –

回答

0

將JTDS驅動程序的名稱更改爲net.sourceforge.jtds.jdbc.Driver修復了該問題。感謝來自用戶的不帶名稱的推薦!