{"id":78923,"date":"2026-11-10T19:40:22","date_gmt":"2026-11-10T19:40:22","guid":{"rendered":"http:\/\/bangla.sitestree.com\/?p=78923"},"modified":"2026-11-10T19:55:09","modified_gmt":"2026-11-10T19:55:09","slug":"rewritten-sql-server-76673","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=78923","title":{"rendered":"Connecting Java to SQL Server with JDBC: A Practical Guide"},"content":{"rendered":"<p>Java applications can communicate with Microsoft SQL Server through the Microsoft JDBC Driver. A connection string identifies the server, database, authentication method, and encryption settings.<\/p>\n<h2>Before you start<\/h2>\n<p>Install the supported Microsoft JDBC Driver and make it available on the application&#8217;s classpath or build dependency. Confirm the database engine is listening on the expected endpoint and the login has only the permissions it needs.<\/p>\n<h2>Example: a parameterized query<\/h2>\n<p>Create the connection and SQL statement inside try-with-resources blocks so JDBC closes them even when an exception occurs.<\/p>\n<h2>Example code<\/h2>\n<pre><code>import java.sql.*;\npublic class SqlServerDemo {\n  public static void main(String[] args) throws SQLException {\n    String url = \"jdbc:sqlserver:\/\/localhost:1433;\"\n               + \"databaseName=TrainingDB;encrypt=true;\"\n               + \"trustServerCertificate=false;\";\n    String sql = \"SELECT ProductID, ProductName \"\n               + \"FROM dbo.Products WHERE CategoryID = ?\";\n    try (Connection c = DriverManager.getConnection(\n             url, System.getenv(\"DB_USER\"), System.getenv(\"DB_PASSWORD\"));\n         PreparedStatement p = c.prepareStatement(sql)) {\n      p.setInt(1, 3);\n      try (ResultSet rs = p.executeQuery()) {\n        while (rs.next())\n          System.out.println(rs.getInt(1) + \": \" + rs.getString(2));\n      }\n    }\n  }\n}<\/code><\/pre>\n<h2>Troubleshooting and safety<\/h2>\n<p>The sample assumes TrainingDB and the sample table exist. A production server must use a certificate trusted by the client; do not silently turn off encryption validation. Avoid hard-coded passwords, catch SQL exceptions at an application boundary, and use prepared statements for user-supplied values.<\/p>\n<hr\/>\n<p><strong>About this article:<\/strong> This AI-generated educational rewrite was prepared from an existing Open Education article, with new explanations and illustrative examples where helpful. <a href=\"https:\/\/bangla.sitestree.com\/?p=76673\">Read the earlier article (post 76673)<\/a>. The new text may be freely copied, edited, and reused with or without attribution to this site. Third-party material, brand names, and linked resources retain their own rights. <strong>AI can make mistakes:<\/strong> verify syntax, version support, security practices, and outcomes against official documentation before use, especially in production.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java applications can communicate with Microsoft SQL Server through the Microsoft JDBC Driver. A connection string identifies the server, database, authentication method, and encryption settings.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[20,182],"tags":[466],"class_list":["post-78923","post","type-post","status-publish","format-standard","hentry","category-information-technology","category---blog","tag-sql-server","item-wrap"],"jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":24463,"url":"http:\/\/bangla.sitestree.com\/?p=24463","url_meta":{"origin":78923,"position":0},"title":"Java Spring: Components to Deal with Database (SQL Aspect) #Root","author":"Author-Check- Article-or-Video","date":"April 10, 2021","format":false,"excerpt":"Java Spring: Components to Deal with Database (SQL Aspect) JPA: Java Persistence API including spring-data-jpa, spring-orm and Hibernate JOOQ: Persistence support using Java Object Oriented Querying MyBatis: Persistence support using MyBatis JDBC: JDBC databases H2: H2 database (with embedded support) HSQLDB: HSQLDB database (with embedded support) Apache Derby: Apache Derby\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":65942,"url":"http:\/\/bangla.sitestree.com\/?p=65942","url_meta":{"origin":78923,"position":1},"title":"JDBC: Stored Procedure #Java Short Notes","author":"Author-Check- Article-or-Video","date":"July 18, 2021","format":false,"excerpt":"Sample stored procedure call using JDBC:Call stored procedure to change\/set a value in the database\/\/set birthday - supply professor nametry{ String professor= \"dylan thomas\"; CallableStatement proc = connection.prepareCall(\"{ call set_birth_date(?, ?) }\"); proc.setString(1, professor); proc.setString(2, '1950-01-01'); cs.execute();}catch (SQLException e){ \/\/ ....}Stored procedures can also return result\/data to the caller: like\/\/return\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26830,"url":"http:\/\/bangla.sitestree.com\/?p=26830","url_meta":{"origin":78923,"position":2},"title":"DBResults.java: Class to store completed results of a JDBC Query. Differs from a ResultSet in several ways #Programming Code Examples #Java\/J2EE\/J2ME #JDBC","author":"Author-Check- Article-or-Video","date":"May 2, 2021","format":false,"excerpt":"# DBResults.java Class to store completed results of a JDBC Query. Differs from a ResultSet in several ways: * ResultSet doesn?t necessarily have all the data; reconnection to database occurs as you ask for later rows. * This class stores results as strings, in arrays. * This class includes DatabaseMetaData\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10116,"url":"http:\/\/bangla.sitestree.com\/?p=10116","url_meta":{"origin":78923,"position":3},"title":"DBResults.java: Class to store completed results of a JDBC Query. Differs from a ResultSet in several ways","author":"","date":"August 5, 2015","format":false,"excerpt":"# DBResults.java\u00a0 Class to store completed results of a JDBC Query. Differs from a ResultSet in several ways: \u00a0\u00a0\u00a0 * ResultSet doesn?t necessarily have all the data; reconnection to database occurs as you ask for later rows. \u00a0\u00a0\u00a0 * This class stores results as strings, in arrays. \u00a0\u00a0\u00a0 * This\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":76673,"url":"http:\/\/bangla.sitestree.com\/?p=76673","url_meta":{"origin":78923,"position":4},"title":"Connect to Sql Server FROM JAVA","author":"Sayed","date":"April 3, 2025","format":false,"excerpt":"https:\/\/learn.microsoft.com\/en-us\/sql\/connect\/jdbc\/connection-url-sample?view=sql-server-ver16 import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class ConnectURL { public static void main(String[] args) { \/\/ Create a variable for the connection string. String connectionUrl = \"jdbc:sqlserver:\/\/<server>:<port>;encrypt=true;databaseName=AdventureWorks;user=<user>;password=<password>\"; try (Connection con = DriverManager.getConnection(connectionUrl); Statement stmt = con.createStatement();) { String SQL = \"SELECT TOP 10 *\u2026","rel":"","context":"In &quot;Root&quot;","block_context":{"text":"Root","link":"http:\/\/bangla.sitestree.com\/?cat=1"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78433,"url":"http:\/\/bangla.sitestree.com\/?p=78433","url_meta":{"origin":78923,"position":5},"title":"Java : Code to Connect to Database","author":"Sayed","date":"August 28, 2025","format":false,"excerpt":"Ref: Internet How to Query to Database: You can read the steps from: -- https:\/\/www.geeksforgeeks.org\/java\/establishing-jdbc-connection-in-java\/ Connect to SQL Server","rel":"","context":"In &quot;Anything JAVA&quot;","block_context":{"text":"Anything JAVA","link":"http:\/\/bangla.sitestree.com\/?cat=1975"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/08\/image-31.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/08\/image-31.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/08\/image-31.png?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/08\/image-31.png?resize=700%2C400 2x"},"classes":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78923","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=78923"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78923\/revisions"}],"predecessor-version":[{"id":78985,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78923\/revisions\/78985"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=78923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=78923"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=78923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}