The connection to the host server failed. Java

回眸只為那壹抹淺笑 提交于 2019-12-10 20:38:58

问题


I'm trying to run a query but I keep getting this error:

The connection to the host server=localhost, named instance sqlexpress failed. Error: "java.net.UnknownHostException: server=localhost". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.

Any ideas? Here's my code.

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

回答1:


There is one interesting site at http://msdn.microsoft.com/en-us/library/ms378428%28v=sql.90%29.aspx

Based on this, i think connect URL should be:

jdbc:sqlserver://Owner-PC;instanceName=SQLEXPRESS;DatabaseName=myDatabase;



回答2:


Per the answer for Connecting to SQL Server 2008 from Java by Venkatesh:

Try following connection,

 String Connectionurl="jdbc:sqlserver://localhost:1433;DatabaseName=myDatabase"


来源:https://stackoverflow.com/questions/8911376/the-connection-to-the-host-server-failed-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!