简体   繁体   English

如何生成JDBC数据库URL?

[英]How do I generate a JDBC database URL?

I have been researching this for about 3 hours today, and I feel like I'm close, but I have a few questions. 我今天已经研究了大约3个小时,我觉得我很接近,但我有几个问题。 The best source for information I have found so far is here: https://stackoverflow.com/a/2840358 , but it doesn't answer all my questions. 我到目前为止找到的最好的信息来源是: https//stackoverflow.com/a/2840358 ,但它没有回答我的所有问题。

A little background: I am using Microsoft SQL Server 2014 and I have verified that the IP Address, 127.0.0.1, is active and enabled with the port 1433. I have no problems connecting to the database through the SQL Server program on the same machine, but I am trying to connect through a Java program that I am writing, and it is currently giving this error: com.microsoft.sqlserver.jdbc.SQLServerException: The port number 1433/R2M_Database is not valid. 一点背景:我正在使用Microsoft SQL Server 2014,并且我已经验证了IP地址127.0.0.1处于活动状态并使用端口1433启用。我在同一台计算机上通过SQL Server程序连接到数据库没有问题,但我正在尝试通过我正在编写的Java程序进行连接,并且它当前正在发出此错误:com.microsoft.sqlserver.jdbc.SQLServerException:端口号1433 / R2M_Database无效。 I am confused by this because I know that the port 1433 is correct, and I know that the database I want to connect to is called R2M_Database. 我对此感到困惑,因为我知道端口1433是正确的,我知道我要连接的数据库叫做R2M_Database。 Now, here are my questions along with my source code. 现在,这是我的问题以及我的源代码。

1.) What is the difference between the "mysql" and "sqlserver" subprotocols, and how do I know which to use? 1.)“mysql”和“sqlserver”子协议之间有什么区别,我怎么知道使用哪个? (I'm pretty sure I'm supposed to use sqlserver, but just in case) (我很确定我应该使用sqlserver,但以防万一)

2.) How do I ensure that I am using the right hostname? 2.)我如何确保使用正确的主机名? (I'm pretty sure 127.0.0.1 / localhost is the one I'm supposed to use, but how will I be able to run this program from other machines to access the database?) (我很确定127.0.0.1 / localhost是我应该使用的那个,但是我怎样才能从其他机器运行这个程序来访问数据库?)

3.) Are there any external issues that could be causing this like firewalls or password requirements? 3.)是否存在任何可能导致防火墙或密码要求的外部问题? (I am fairly certain that the user name and password are correct as they are the ones I use to successfully get a connection in Microsoft SQL Server 2014) (我相当确定用户名和密码是正确的,因为它们是我用来在Microsoft SQL Server 2014中成功获得连接的用户名和密码)

EDIT: 编辑:

import java.sql.DriverManager;
import java.io.BufferedReader;
import java.io.FileReader;
import org.apache.ibatis.jdbc.ScriptRunner;

public class SQLTest {
    public static void main(String[] args){
        String script = "CreatePersons.sql";
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            new ScriptRunner(DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databaseName=R2M_Database", userName, password))
                    .runScript(new BufferedReader(new FileReader(script)));
        } catch (Exception e) {
            System.err.println(e);
        }
    }
}

I made a slight syntax change (;databaseName= instead of /), and now I am getting this error: 我做了一个轻微的语法更改(; databaseName =而不是/),现在我收到此错误:

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'RSquaredMacro'. com.microsoft.sqlserver.jdbc.SQLServerException:用户'RSquaredMacro'登录失败。 ClientConnectionId:35281a40-0f87-42e4-bc46-b9a81a371529 ClientConnectionId:35281a40-0f87-42e4-BC46-b9a81a371529

  1. mysql is a completely different database server. mysql是一个完全不同的数据库服务器。 Don't use it for SQL Server. 不要将它用于SQL Server。

  2. you will need to provide a public address for your database server. 您需要为数据库服务器提供公共地址。 either an ip or a hostname is fine, as long as all of your clients can see it. 只要所有客户都可以看到它,就可以使用ip或主机名。 if you don't have one, you can try using dyndns.org or something like it. 如果你没有,你可以尝试使用dyndns.org或类似的东西。 Note your clients will still need to be able to get to your server on the network (potential firewall issues). 请注意,您的客户仍需要能够访问网络上的服务器(潜在的防火墙问题)。 Hard to say more without knowing more about your situation. 在不了解您的情况的情况下很难说更多。

  3. A firewall could possibly be an issue but this is unlikely for localhost. 防火墙可能是一个问题,但对于localhost来说这不太可能。 I'm guessing the issue is with the TCP/IP listener, username, password, or database name settings. 我猜测问题在于TCP / IP侦听器,用户名,密码或数据库名称设置。

Here is a link with more information about building a JDBC connection string: http://technet.microsoft.com/en-us/library/ms378428%28v=sql.110%29.aspx 以下链接提供了有关构建JDBC连接字符串的更多信息: http//technet.microsoft.com/en-us/library/ms378428%28v=sql.110%29.aspx

Also some side notes: 还有一些附注:

  1. You don't need to include the 1433 port number. 您不需要包含1433端口号。

  2. printing the entire stack trace will help you with debugging. 打印整个堆栈跟踪将帮助您进行调试。 (see below code) (见下面的代码)

  3. you should declare a variable for your Connection and make sure it gets closed. 您应该为Connection声明一个变量并确保它已关闭。 Example: 例:

     Connection c = null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); c = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433/R2M_Database", userName, password); new ScriptRunner(c).runScript(new BufferedReader(new FileReader(script))); } catch (Exception e) { e.printStackTrace(); } finally { if (c != null) try { c.close(); } catch (Exception e) { } } 

It turns out that the issue was all in the formatting of my JDBC URL. 事实证明,问题在于我的JDBC URL的格式化。 The problem I had was that different subprotocols have different syntaxes for their URL-s, but the syntax for the sqlserver protocol (and how to find each part) is listed below: 我遇到的问题是不同的子协议具有不同的URL-s语法,但sqlserver协议的语法(以及如何查找每个部分)如下所示:

"jdbc:sqlserver://" + serverName + ";database=" + databaseName

1.) serverName: To find your server's name, connect to Microsoft SQL Server (these instructions are based on SQL Server 2014), and look at the "Properties" pane on the right. 1.)serverName:要查找服务器名称,请连接到Microsoft SQL Server(这些说明基于SQL Server 2014),然后查看右侧的“属性”窗格。 Choose "Current connection parameters" in the combo box at the top, and look under "Connection Details" for the "Server name" field. 在顶部的组合框中选择“当前连接参数”,然后在“服务器名称”字段的“连接详细信息”下查找。

2.) databaseName: This one is fairly straightforward, as it is the name of the database that you want to access data from. 2.)databaseName:这个是相当简单的,因为它是您要从中访问数据的数据库的名称。 Even though the user you log in to is listed under the "master" database, you do not have to log into "master," but rather, you can use any database you want, as long as the user you are logging into has permission to do the tasks you want to accomplish. 即使您登录的用户列在“主”数据库下,您也不必登录“master”,而是可以使用您想要的任何数据库,只要您登录的用户具有权限即可完成你想要完成的任务。

Finally: here is the link that helped me finally solve my problem, which goes into greater detail: http://www.programcreek.com/2010/05/java-code-for-connecting-ms-sql-server-by-using-sql-server-authentication/ 最后:这是帮助我最终解决问题的链接,更详细地说明了这一点: http//www.programcreek.com/2010/05/java-code-for-connecting-ms-sql-server-by-使用-SQL服务器的认证/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM