JtdsSqlServerJdbcConnection.java - jTDS JDBC Connection Example

Q

How to create a JDBC connection to SQL Server database using the jTDS JDBC driver? I want to see a Java program example.

✍: FYIcenter.com

A

If you have a SQL Server running on your local computer with TCP/IP protocol turned on and listening on port 1433, you can use the following example, JtdsSqlServerJdbcConnection.java, to learn how to create a JDBC connection:

// Copyright (c) 2016 FYIcenter.com
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.DatabaseMetaData;

// Example of jTDS JDBC connection to SQL Server
public class JtdsSqlServerJdbcConnection {
   public static void main(String [] args) throws Exception {

      // Build the jTDS JDBC connection URL
      String url = "jdbc:jtds:sqlserver://localhost;user=sa;password=fyicenter";
      Connection con = DriverManager.getConnection(url);
      
      System.out.println("JDBC connection and database server information:"); 
      DatabaseMetaData meta = con.getMetaData();
      System.out.println("Database Product Name: "
        + meta.getDatabaseProductName());
      System.out.println("Database Product Version: "
        + meta.getDatabaseProductVersion());
      System.out.println("Driver Name: "
        + meta.getDriverName());
      System.out.println("Driver Version: "
        + meta.getDriverVersion());
      System.out.println("JDBC Major Version: "
        + meta.getJDBCMajorVersion());
      System.out.println("JDBC Minor Version: "
        + meta.getJDBCMinorVersion());

      // Close the connection
      con.close();
   }
}

You can compile and run the above example in a command window as shown below:

C:\fyicenter>\local\jdk-1.8.0\bin\javac 
   -cp C:\local\jtds-1.3.1\jtds-1.3.1.jar 
   JtdsSqlServerJdbcConnection.java

C:\fyicenter>\local\jdk-1.8.0\bin\java
   -cp .;C:\local\jtds-1.3.1\jtds-1.3.1.jar 
   JtdsSqlServerJdbcConnection
   
JDBC connection and database server information:
Database Product Name: Microsoft SQL Server
Database Product Version: 12.00.2000
Driver Name: jTDS Type 4 JDBC Driver for MS SQL Server and Sybase
Driver Version: 1.3.1
JDBC Major Version: 3
JDBC Minor Version: 0   

The output confirms that:

  • The database server is SQL Server 2014 (Version 12).
  • The JDBC driver is jTDS 1.3.1.
  • The JDBC API supported is JDBC 3.0.

 

JtdsSqlServerJdbcUrl.java - jTDS JDBC Connection URL Example

SQL Server SQLEXPRESS Service for jTDS Test

Examples for jTDS - JDBC Driver for SQL Server

⇑⇑ FAQ for jTDS - JDBC Driver for SQL Server

2017-01-29, 2694🔥, 0💬