Test SQL Server JDBC Driver

This section provides a tutorial example on how to test the SQL Server JDBC driver on macOS systems.

Before using the SQL Server JDBC driver in Servlet pages, I wrote the following test Java program, ConnectionSqlServer.java:

/* ConnectionSqlServer.java
 - Copyright (c) 2006 HerongYang.com. All Rights Reserved.
 */
import java.sql.*;
public class ConnectionSqlServer {
  public static void main(String [] args) {
    Connection con = null;
    try {

// Obtaining a connection to SQL Server
      con = DriverManager.getConnection(
        "jdbc:sqlserver://localhost;"
        + "user=herong;password=Y@ng;database=HerongDB");

// Connection is ready to use
      DatabaseMetaData meta = con.getMetaData();
      System.out.println("Driver name: "
        + meta.getDriverName());
      System.out.println("Driver version: "
        + meta.getDriverVersion());
      System.out.println("Server name: "
        + meta.getDatabaseProductName());
      System.out.println("Server version: "
        + meta.getDatabaseProductVersion());
      System.out.println("Connection URL: "
        + meta.getURL());
      System.out.println("Login name: "
        + meta.getUserName());

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Compilation has no problem. There is no need to specify the JDBC driver JAR file, since the JDBC API is included in the JRE 13.

herong$ cp ConnectionSqlServer.java \
  /Library/Tomcat/apache-tomcat-9.0.26/webapps/herong/WEB-INF/classes

herong$ cd \
  /Library/Tomcat/apache-tomcat-9.0.26/webapps/herong/WEB-INF/classes

herong$ javac ConnectionSqlServer.java

Execution without the JDBC driver JAR file gives us an error.

herong$ java version
java version "13" 2019-09-17
Java(TM) SE Runtime Environment (build 13+33)
Java HotSpot(TM) 64-Bit Server VM (build 13+33, mixed mode, sharing)

herong$ java ConnectionSqlServer
java.sql.SQLException: No suitable driver found for
  jdbc:sqlserver://localhost;user=herong;password=Y@and;database=HerongDB
  at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
  at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:251)
  at ConnectionSqlServer.main(ConnectionSqlServer.java:15)

Execution with the JDBC driver JAR file works nicely.

herong$ java -cp .:../lib/mssql-jdbc-7.4.1.jre12.jar \
   ConnectionSqlServer

Driver name: Microsoft JDBC Driver 7.4 for SQL Server
Driver version: 7.4.1.0
Server name: Microsoft SQL Server
Server version: 11.00.3128
Connection URL: jdbc:sqlserver://localhost:1433;useFmtOnly=false;...
Login name: herong

Note that:

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat Installation on Windows Systems

 JSP Scripting Elements

 Java Servlet Introduction

 JSP Implicit Objects

 Syntax of JSP Pages and JSP Documents

 JSP Application Session

 Managing Cookies in JSP Pages

 JavaBean Objects and "useBean" Action Elements

 Managing HTTP Response Header Lines

 Non-ASCII Characters Support in JSP Pages

 Performance of JSP Pages

 EL (Expression Language)

 Overview of JSTL (JSP Standard Tag Libraries)

 JSTL Core Library

 JSP Custom Tags

 JSP Java Tag Interface

 Custom Tag Attributes

 Multiple Tags Working Together

 File Upload Test Application

 Using Tomcat on CentOS Systems

 Using Tomcat on macOS Systems

Connecting to SQL Server from Servlet

 SQL Server Connection Requirements

 Download and Install SQL Server JDBC Driver

Test SQL Server JDBC Driver

 Use JDBC Driver in Servlet

 Load JDBC Driver Class in Servlet

 Developing Web Applications with Servlet

 Archived Tutorials

 References

 Full Version in PDF/EPUB