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
-
1In the Java world, there isn't another way.Luiggi Mendoza– Luiggi Mendoza2013年03月21日 16:02:14 +00:00Commented Mar 21, 2013 at 16:02
-
1I think the connection string is driver specific, so the JDBC syntax will differ from .NET drivers.Mike Christensen– Mike Christensen2013年03月21日 16:03:19 +00:00Commented Mar 21, 2013 at 16:03
-
Directly pointing to the database_name.mdf is not possible through jdbc api...Piyas De– Piyas De2013年03月21日 16:05:28 +00:00Commented Mar 21, 2013 at 16:05
-
3As 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.Luiggi Mendoza– Luiggi Mendoza2013年03月21日 16:14:11 +00:00Commented Mar 21, 2013 at 16:14
2 Answers 2
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.
Comments
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
Explore related questions
See similar questions with these tags.