与主机服务器的连接失败。 Java [英] The connection to the host server failed. Java

查看:615
本文介绍了与主机服务器的连接失败。 Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试运行查询,但我一直收到此错误:

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

与主机服务器的连接= localhost,命名实例sqlexpress失败。错误:java.net.UnknownHostException:server = localhost。验证服务器和实例名称,并检查没有防火墙阻止到端口1434的UDP流量。对于SQL Server 2005或更高版本,请验证主机上是否正在运行SQL Server浏览器服务。

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


推荐答案

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;

这篇关于与主机服务器的连接失败。 Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆