How do I generate a JDBC database URL? How do I generate a JDBC database URL? database database

How do I generate a JDBC database URL?


  1. mysql is a completely different database server. Don't use it for 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. if you don't have one, you can try using dyndns.org or something like it. 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. I'm guessing the issue is with the TCP/IP listener, username, password, or database name settings.

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

Also some side notes:

  1. You don't need to include the 1433 port number.

  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 sureit gets closed. 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. 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:

"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. 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. 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.

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/