My question is about how connection between MS SQL Server and Java application works. In web there are a lot of examples how to connect with database and run query, but a lot of this examples is build by only one method contains connecting to database funcionality and code which is running query on this connection. When i want to run few queries, application at every time is connecting to database by driver and login, then is running query.. My question is how to build application (in Java) where connection to database (MS Server) is created only once when application is starting and queries are running basign on this created connection? Please provide me some examples how to write client-database application with this logic.
Asked
Active
Viewed 46 times
0
-
Rather than a static connection(s), the best practice is to use connection pooling. See https://docs.oracle.com/javase/tutorial/jdbc/basics/sqldatasources.html and https://stackoverflow.com/questions/2835090/how-to-establish-a-connection-pool-in-jdbc – Dan Guzman Sep 04 '17 at 09:37
-
Thank you, but does this will work with MS Server? – keru Sep 04 '17 at 10:33
-
https://learn.microsoft.com/en-us/sql/connect/jdbc/data-source-sample – Dan Guzman Sep 04 '17 at 10:55
-
Just create your connection as a global variable (it can even be a static variable) and reuse it. For mutltiple users, it's best to use a connection pool, as already indicated by Dan Guzman. – dsp_user Sep 04 '17 at 11:18
-
Thank you guys, now it's clear and enough for me - I'm a beginner in Java. – keru Sep 04 '17 at 11:49