1

I am C# developer and I don't know much about Java, normally in C# when I wanna connect to a database I use the following command:

static SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");

I read a tutorial about making database connection (Sql Server 2008) in java in MSDN saying that the address must be declared this way:

String connectionUrl = "jdbc:sqlserver://localhost:1433;" + "databaseName=JavaDB;user=UserName;password=*****";

I would like to if there's any way to declare the url the way I do in C#, I mean instead of

"jdbc:sqlserver://localhost:1433;" 

I directly point to the database

"AttachDbFilename=|DataDirectory|\Database.mdf;"

thanks

4
  • 1
    In the Java world, there isn't another way. Commented Mar 21, 2013 at 16:02
  • 1
    I think the connection string is driver specific, so the JDBC syntax will differ from .NET drivers. Commented Mar 21, 2013 at 16:03
  • Directly pointing to the database_name.mdf is not possible through jdbc api... Commented Mar 21, 2013 at 16:05
  • 3
    As a Java developer that began working with .net technologies, I can advice you to don't focus on I used to do it in this way and I want/prefer to keep it like that instead try learning the new things. Take note that if you're going to Java (or any other PL) you should be open to new concepts/ideas or else you'll be ranting and won't learn anything. Commented Mar 21, 2013 at 16:14

2 Answers 2

1

The first part of the URL is prescribed by the JDBC specification, so all drivers will follow the structure jdbc:<vendor-identifier>:<vendor-specific-url>.

In Java creating the connection (at least via java.sql.DriverManager) is independent of the actual Driver implementation that creates the connection (in C# you create a typed vendor-specific connection).

The first part, jdbc:<vendor-identifier> is used as a selection mechanism so a Driver can quickly decide if it will accept an URL or not. Technically multiple driver implementations could accept an URL and create the connection. The <vendor-identifier> is usually the name of the database or company.

The <vendor-specific-url> will usually follow normal URL conventions (MS SQL Server JDBC URLS are an exception to that).

The format of the Microsoft JDBC driver is:

jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]

See: Building the Connection URL.

Technically, Microsoft could have allowed only the database name in their <vendor-specific-url> and imply that it uses localhost but they decided not to do that.

Sign up to request clarification or add additional context in comments.

Comments

0

The official documentation of the SQL JDBC driver does not mention any such thing

http://msdn.microsoft.com/en-us/library/ms378428.aspx

http://msdn.microsoft.com/en-us/library/ms378672(v=sql.110).aspx

so I assume it is not possible

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.