Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. How to establish connection between JAVA & MS SQL Server ?

How to establish connection between JAVA & MS SQL Server ?

Scheduled Pinned Locked Moved Java
databasejavasql-servercomsysadmin
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Amol Lendave
    wrote on last edited by
    #1

    // For establishing connection between JAVA and MS SQL Server, first you should have to add SQLJDBC.jar.


    Click here for .jar file.

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    public class ConnectMSSQL {
    public static void main(String[] args) {
    try {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    Connection connection = DriverManager.getConnection(
    "jdbc:sqlserver://","", "");

            System.out.println("DATABASE NAME IS:"  
                    + connection.getMetaData().getDatabaseProductName());  
            Statement statement = connection.createStatement();  
            ResultSet resultSet = statement  
                    .executeQuery("select \* from dbo.try");  
            while (resultSet.next()) {  
                System.out.println(resultSet.getString("Name"));  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
    

    }

    L S 2 Replies Last reply
    0
    • A Amol Lendave

      // For establishing connection between JAVA and MS SQL Server, first you should have to add SQLJDBC.jar.


      Click here for .jar file.

      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.ResultSet;
      import java.sql.Statement;
      public class ConnectMSSQL {
      public static void main(String[] args) {
      try {
      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
      Connection connection = DriverManager.getConnection(
      "jdbc:sqlserver://","", "");

              System.out.println("DATABASE NAME IS:"  
                      + connection.getMetaData().getDatabaseProductName());  
              Statement statement = connection.createStatement();  
              ResultSet resultSet = statement  
                      .executeQuery("select \* from dbo.try");  
              while (resultSet.next()) {  
                  System.out.println(resultSet.getString("Name"));  
              }  
          } catch (Exception e) {  
              e.printStackTrace();  
          }  
      }  
      

      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      And what is your question?

      Veni, vidi, abiit domum

      A 1 Reply Last reply
      0
      • L Lost User

        And what is your question?

        Veni, vidi, abiit domum

        A Offline
        A Offline
        Amol Lendave
        wrote on last edited by
        #3

        excuse me sir, ;) It's only article for information. :-D

        S L 2 Replies Last reply
        0
        • A Amol Lendave

          excuse me sir, ;) It's only article for information. :-D

          S Offline
          S Offline
          Shubhashish_Mandal
          wrote on last edited by
          #4

          Very poor info. Lots of glitch : 1. What happen if Connection return as null. 2. What happen if ResultSet return as null. 3. Finally if all run good, then where is the code to close the resources.

          Regards Shubhashish

          Y 1 Reply Last reply
          0
          • A Amol Lendave

            excuse me sir, ;) It's only article for information. :-D

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Well, this is not the place to post it. Please read http://www.codeproject.com/Messages/3137511/Forum-Guidelines-PLEASE-READ.aspx[^], and if you want to post an article then please read http://www.codeproject.com/info/Submit.aspx[^]. But do not submit a code dump as an article as it will most likely be rejected.

            Veni, vidi, abiit domum

            A 1 Reply Last reply
            0
            • L Lost User

              Well, this is not the place to post it. Please read http://www.codeproject.com/Messages/3137511/Forum-Guidelines-PLEASE-READ.aspx[^], and if you want to post an article then please read http://www.codeproject.com/info/Submit.aspx[^]. But do not submit a code dump as an article as it will most likely be rejected.

              Veni, vidi, abiit domum

              A Offline
              A Offline
              Amol Lendave
              wrote on last edited by
              #6

              Ok, Thank you so much for assist.

              1 Reply Last reply
              0
              • S Shubhashish_Mandal

                Very poor info. Lots of glitch : 1. What happen if Connection return as null. 2. What happen if ResultSet return as null. 3. Finally if all run good, then where is the code to close the resources.

                Regards Shubhashish

                Y Offline
                Y Offline
                yadagirirao aileni
                wrote on last edited by
                #7

                import java.sql.Connection ; import java.sql.DriverManager ; import java.sql.ResultSet ; import java.sql.Statement ; import java.sql.SQLException; class JdbcTestMssql { public static void main (String args[]) { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (ClassNotFoundException e) { System.err.println (e) ; System.exit (-1) ; } try { Connection connection = DriverManager.getConnection( "jdbc:sqlserver://192.168.100.68:1433;databaseName=master;user=ezadmin;password=ezadmin;"); String query = "SELECT * From sys.databases" ; Statement statement = connection.createStatement () ; ResultSet rs = statement.executeQuery (query) ; while ( rs.next () ) System.out.println ("MS-SQL Query result: " + rs.getString ("name")) ; connection.close () ; } catch (java.sql.SQLException e) { System.err.println (e) ; System.exit (-1) ; } } }

                1 Reply Last reply
                0
                • A Amol Lendave

                  // For establishing connection between JAVA and MS SQL Server, first you should have to add SQLJDBC.jar.


                  Click here for .jar file.

                  import java.sql.Connection;
                  import java.sql.DriverManager;
                  import java.sql.ResultSet;
                  import java.sql.Statement;
                  public class ConnectMSSQL {
                  public static void main(String[] args) {
                  try {
                  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                  Connection connection = DriverManager.getConnection(
                  "jdbc:sqlserver://","", "");

                          System.out.println("DATABASE NAME IS:"  
                                  + connection.getMetaData().getDatabaseProductName());  
                          Statement statement = connection.createStatement();  
                          ResultSet resultSet = statement  
                                  .executeQuery("select \* from dbo.try");  
                          while (resultSet.next()) {  
                              System.out.println(resultSet.getString("Name"));  
                          }  
                      } catch (Exception e) {  
                          e.printStackTrace();  
                      }  
                  }  
                  

                  }

                  S Offline
                  S Offline
                  Sherin_Mathew
                  wrote on last edited by
                  #8

                  Step 1: Connect Use the connection class to connect to SQL Database.

                  import java.sql.Connection;
                  import java.sql.DriverManager;
                  import java.sql.SQLException;

                  public class SQLDatabaseConnection {
                  // Connect to your database.
                  // Replace server name, username, and password with your credentials
                  public static void main(String[] args) {
                  String connectionUrl =
                  "jdbc:sqlserver://yourserver.database.windows.net:1433;"
                  + "database=AdventureWorks;"
                  + "user=yourusername@yourserver;"
                  + "password=yourpassword;"
                  + "encrypt=true;"
                  + "trustServerCertificate=false;"
                  + "loginTimeout=30;";

                      try (Connection connection = DriverManager.getConnection(connectionUrl);) {
                          // Code here.
                      }
                      // Handle any errors that may have occurred.
                      catch (SQLException e) {
                          e.printStackTrace();
                      }
                  }
                  

                  }

                  Step 2: Execute a query

                  import java.sql.Connection;
                  import java.sql.DriverManager;
                  import java.sql.ResultSet;
                  import java.sql.SQLException;
                  import java.sql.Statement;

                  public class SQLDatabaseConnection {

                  // Connect to your database.
                  // Replace server name, username, and password with your credentials
                  public static void main(String\[\] args) {
                      String connectionUrl =
                              "jdbc:sqlserver://yourserver.database.windows.net:1433;"
                              + "database=AdventureWorks;"
                              + "user=yourusername@yourserver;"
                              + "password=yourpassword;"
                              + "encrypt=true;"
                              + "trustServerCertificate=false;"
                              + "loginTimeout=30;";
                  
                      ResultSet resultSet = null;
                  
                      try (Connection connection = DriverManager.getConnection(connectionUrl);
                              Statement statement = connection.createStatement();) {
                  
                          // Create and execute a SELECT SQL statement.
                          String selectSql = "SELECT TOP 10 Title, FirstName, LastName from SalesLT.Customer";
                          resultSet = statement.executeQuery(selectSql);
                  
                          // Print results from select statement
                          while (resultSet.next()) {
                              System.out.println(resultSet.getString(2) + " " + resultSet.getString(3));
                          }
                      }
                      catch (SQLException e) {
                          e.printStackTrace();
                      }
                  
                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups