2012-01-18 81 views
3

我在嘗試運行查詢,但不斷收到此錯誤:到主機服務器的連接失敗。 Java

與主機服務器= localhost,命名實例sqlexpress的連接失敗。錯誤:「java.net.UnknownHostException:server = localhost」。驗證服務器和實例名稱,並檢查是否沒有防火牆將UDP通信阻止到端口1434.對於SQL Server 2005或更高版本,請驗證SQL Server Browser Service是否正在主機上運行。

任何想法?這是我的代碼。

public static void main(String[] args) { 
     try { 
      try { 
       // TODO code application logic here 
       Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 

      // Connection dbConnection = DriverManager.getConnection("jdbc:jtds:sqlserver://127.0.0.1:1433/myDatabase; instance = SQLEXPRESS","sa","password"); 
      String Connectionurl="jdbc:sqlserver://server=Owner-PC\\SQLEXPRESS; DatabaseName=myDatabase;"; 
      Connection dbConnection = DriverManager.getConnection(Connectionurl,"sa","password"); 
      Statement myStmt = dbConnection.createStatement(); 

       String query = "INSERT INTO People(ID, Name, Surname, Age, Contact, Location, Course) VALUES" 
         + " (1007, 'Elroy', 'Smith', 33, 21366688, 'Somewhere', 'somecourse')"; 

       myStmt.executeUpdate(query); 


       myStmt.executeQuery(query); 

       } catch (ClassNotFoundException ex) { 
       Logger.getLogger(JavaApplication2.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } catch (SQLException ex) { 
      Logger.getLogger(JavaApplication2.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
+0

你可以連接到你的本地數據庫嗎? – 2012-01-18 14:03:32

+0

我認爲這是問題所在。我可以從C#中完成,但不能從這裏完成。我一直在玩連接字符串整天 – user1028408 2012-01-18 14:06:52

+0

可能的重複:http://stackoverflow.com/q/2606653/2291 – 2012-03-30 18:06:35

回答